Question

Ansible playbook error: mapping values are not allowed in this context

Posted August 7, 2019 52.5k views
AnsibleUbuntu 18.04

I am trying to play with Ansible a little bit and I’ve created a test playbook but it is failing for some reason and I can’t figure out why. I’ve tested the connectivity between my control node and the worker node but when I try to run my playbook I get this error:

ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to have been in '/root/ansible/setup.yaml': line 9, column 12, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Apache started
    service:
           ^ here

I can not see anything wrong with the service part in my playbook. Here’s what I currently have:

---
- hosts: webservers
  vars:
    my_service: apache2
    desired_state: started

  tasks:
- name: Apache started
    service:
      name:  "{{ my_service }}"
      state: "{{ desired_state }}"

Has anyone seen this error before?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
5 answers

Hello,

The playbook looks good, there’s only a slight indentation error, Yaml is very strict about the indentations and the spacing.

You need to make sure that you have two spaces before the - name part of your playbook. So it would look something like this:

---
- hosts: webservers
  vars:
    my_service: apache2
    desired_state: started

  tasks:
  - name: Apache started
    service:
      name:  "{{ my_service }}"
      state: "{{ desired_state }}"

Hope that this helps!
Regards,
Bobby

I am getting error with the below yaml file.please help

vi nginx-install.yml


  • name: This is my first Ansible playbook to install nginx hosts: all become: true

tasks:
-name: install nginx
yum:
name: nginx
state: latest

service:
name: nginx
state: started

  • Hello,

    You need to adjust this a little bit to:

    ---
    - hosts: webservers
      become: yes
      tasks:
      - name: Install Nginx Latest
        yum:
          name: nginx
          state: latest
      - name: Start Nginx
        service:
          name: nginx
          state: started
    

    This should work.

    If you get any errors, feel free to share them here.

    Regards,
    Bobby

Hello, I need help. I am also getting this issue:

error: error parsing /Users/m_calestino/Desktop/getambass.yml: error converting YAML to JSON: yaml: line 3: mapping values are not allowed in this context

when I put in:

apiVersion: getambassador.io/v0
kind:Host
metadata:
name: minimal-host
spec:
hostname: host.example.com
acmeProvider: some-ambassador
requestPolicy:
insecure:
action: Redirect
additionalPort: 8080

Here is the actual issue and the link below:

ISSUE:
When I go /ambassador/v0/diag/ in 1.4.2 I see a ton of

NOTICE: -global-: A future Ambassador version will change the GRPC protocol version for AuthServices and RateLimitServices. See the CHANGELOG for details.
NOTICE: -global-: A future Ambassador version will change the GRPC protocol version for AuthServices and RateLimitServices. See the CHANGELOG for details.
NOTICE: -global-: A future Ambassador version will change the GRPC protocol version for AuthServices and RateLimitServices. See the CHANGELOG for details.

at the start. This is silly.

LINK:
https://github.com/datawire/ambassador/issues/2634

Can someone help me to fix it?

Thank you

Hello, I am getting a similar error with td-agent file. I checked on various pages but not clear yet.

ERROR:
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
did not find expected <document start>

The error appears to be in ’/address/to/path/log-aggregator.yml:
be elsewhere in the file depending on the exact syntax problem.

<store>
    @type s3
    s3_bucket {{ s3_bucket_name }}
    s3_region {{ s3_bucket_region }}
    s3_object_key_format %{path}/name%{time_slice}.json.%{file_extension}
    path platform/core_name_log_v1_1m/%Y/%m/%d/%H
    time_slice_format %Y%m%d_%H%M
    store_as gzip
    include_tag_key true
    tag_key tag
    buffer_type file
    buffer_path /var/log/td-agent/cello/core/name
    buffer_queue_limit 512
    buffer_chunk_limit 2m
    flush_interval 10s
    utc
    grant_full_control id=jh11qfhj67676le1p092f2xxuxxxxxxxxxxxxx3b5609t2ff6d77771020129356
    check_object false
  </store>

check the ansible version and upgrade it and check python version also as stack require higher version of ansible and python

    • name: test hosts: localhost tasks: -name: launching AWS instance using Ansible EC2 EC2: keyname: suriaws instancetype: t2.micro image: ami-0c2b8ca1dad447f8a region: us-east-1a wait: yes count: 1 assignpublicip: yes

    i am also getting same Error