Syntax error while loading yaml mapping values are not allowed in this context

I am trying to configure a YAML file in this format: jobs: - name: A - schedule: "0 0/5 * 1/1 * ? *" - type: mongodb.cluster - config: - host: mongodb://localhost:27017/admin?

I am trying to configure a YAML file in this format:

jobs:
 - name: A
   - schedule: "0 0/5 * 1/1 * ? *"
   - type: mongodb.cluster
    - config:
       - host: mongodb://localhost:27017/admin?replicaSet=rs
       - minSecondaries: 2
       - minOplogHours: 100
       - maxSecondaryDelay: 120
 - name: B
   - schedule: "0 0/5 * 1/1 * ? *"
   - type: mongodb.cluster
    - config:
       - host: mongodb://localhost:27017/admin?replicaSet=rs
       - minSecondaries: 2
       - minOplogHours: 100
       - maxSecondaryDelay: 120

The idea is that I can read the contents inside the job element, and have a series of different job configs which can be parsed.

however, yamllint.com tells me that this is illegal YAML due to mapping values are not allowed in this context at line 2 where line 2 is the jobs: line.

What am I doing wrong?

mpromonet's user avatar

mpromonet

10.9k43 gold badges57 silver badges91 bronze badges

asked Jul 9, 2015 at 9:28

Chris Edwards's user avatar

Chris EdwardsChris Edwards

1,4982 gold badges13 silver badges23 bronze badges

This is valid YAML:

jobs:
 - name: A
   schedule: "0 0/5 * 1/1 * ? *"
   type: mongodb.cluster
   config:
     host: mongodb://localhost:27017/admin?replicaSet=rs
     minSecondaries: 2
     minOplogHours: 100
     maxSecondaryDelay: 120
 - name: B
   schedule: "0 0/5 * 1/1 * ? *"
   type: mongodb.cluster
   config:
     host: mongodb://localhost:27017/admin?replicaSet=rs
     minSecondaries: 2
     minOplogHours: 100
     maxSecondaryDelay: 120

Note, that every ‘-‘ starts new element in the sequence. Also, indentation of keys in the map should be exactly same.

answered Jul 10, 2015 at 7:37

Tsyvarev's user avatar

The elements of a sequence need to be indented at the same level. Assuming you want two jobs (A and B) each with an ordered list of key value pairs, you should use:

jobs:
 - - name: A
   - schedule: "0 0/5 * 1/1 * ? *"
   - - type: mongodb.cluster
     - config:
       - host: mongodb://localhost:27017/admin?replicaSet=rs
       - minSecondaries: 2
       - minOplogHours: 100
       - maxSecondaryDelay: 120
 - - name: B
   - schedule: "0 0/5 * 1/1 * ? *"
   - - type: mongodb.cluster
     - config:
       - host: mongodb://localhost:27017/admin?replicaSet=rs
       - minSecondaries: 2
       - minOplogHours: 100
       - maxSecondaryDelay: 120

Converting the sequences of (single entry) mappings to a mapping as @Tsyvarrev does is also possible, but makes you lose the ordering.

answered Aug 13, 2016 at 7:49

Anthon's user avatar

AnthonAnthon

65.7k29 gold badges179 silver badges236 bronze badges

“mapping values are not allowed in this context” is a common error originated from the Python module pyyaml (https://pyyaml.org/). The library is widely used among Python community, so you may come across this error message while working with another tool, such as Ansible or yamllint.com.

The error usually occurs when there is an unexpected indentation or special character in your YAML file. In this article, we will show you how to debug and fix mapping values are not allowed in this context error in YAML.

Also check out: Fix Uncaught ReferenceError: require is not defined in JavaScript

Debugging your YAML file

Most of the time, the tool you’re using to parse YAML files will give you mapping values are not allowed in this context along with a few other details. The error output from PyYAML may look like below:

Traceback (most recent call last): File "/home/user/main.py", line 4, in <module> yaml.load(foofile, Loader=yaml.CLoader) File "/usr/lib/python3/dist-packages/yaml/__init__.py", line 114, in load return loader.get_single_data() File "/usr/lib/python3/dist-packages/yaml/constructor.py", line 49, in get_single_data node = self.get_single_node() File "ext/_yaml.pyx", line 707, in _yaml.CParser.get_single_node File "ext/_yaml.pyx", line 725, in _yaml.CParser._compose_document File "ext/_yaml.pyx", line 774, in _yaml.CParser._compose_node File "ext/_yaml.pyx", line 851, in _yaml.CParser._compose_sequence_node File "ext/_yaml.pyx", line 776, in _yaml.CParser._compose_node File "ext/_yaml.pyx", line 890, in _yaml.CParser._compose_mapping_node File "ext/_yaml.pyx", line 774, in _yaml.CParser._compose_node File "ext/_yaml.pyx", line 851, in _yaml.CParser._compose_sequence_node File "ext/_yaml.pyx", line 776, in _yaml.CParser._compose_node File "ext/_yaml.pyx", line 892, in _yaml.CParser._compose_mapping_node File "ext/_yaml.pyx", line 905, in _yaml.CParser._parse_next_event yaml.scanner.ScannerError: mapping values are not allowed in this context in "example.yaml", line 6, column 11

Code language: JavaScript (javascript)

The error message may be something else, but there is one thing you need to pay attention to, which is the line number that causes the problem. In this case, that line is line 6. That’s all we need to further investigate.

Double check indentation

Most of the time the error comes from unexpected indentations in the YAML file. A YAML file use spaces as indentation, you can use 2 or 4 spaces for indentation, but no tab. In other words, tab indentation is forbidden. Plus, the use of spaces have to be unified across the file. For example, you cannot mix 2-space and 4-space lines in the same file.

Let’s take a look at an Ansible playbook file :

--- - name: Foofile test hosts: localhost tasks: - name: Make the foofile copy: content: foobar foobar foobar dest: /tmp/foofile.txt

Code language: JavaScript (javascript)

We can see that from line 6 (copy:), the contents have extended indentations. We can fix it by moving the spaces back an indentation level, which should look like this :

--- - name: Foofile test hosts: localhost tasks: - name: Make the foofile copy: content: foobar foobar foobar dest: /tmp/foofile.txt

Code language: JavaScript (javascript)

All we had to do was move “copy” and all its children back two spaces.

Check for strange Unicode characters

One of the lesser-known things that causes mapping values are not allowed in this context error is Unicode characters.

Most of the time, you can manually remove strange-looking Unicode characters on your own, but pay extra attention to the various Unicode spaces.

Besides a regular space, we have other kinds of spaces such as U+2009(thin space), U+200A (hair space) and especially U+200B (zero width space) which doesn’t even show up in the file. YAML doesn’t recognize them as separation spaces, which is why parsing fails and greets you with mapping values are not allowed in this context error.

If you’re using Visual Studio Code, you can install the Gremlins extension, which reveals invisible whitespace and other annoying characters by showing them in the editor.

In rare cases, you should also look into dashes, too. A copy-paste code snippet from the web can easily bring special ‘EN DASH’ (U+2013) instead of the regular dash (named ‘HYPHEN MINUS’ U+002D) to your file.

Validate YAML syntax

We’ve written about the most common scenarios above, but if you’ve tried them all without any success, you may want to soak up your knowledge about YAML a little bit. YAML for beginners by Red Hat and Ansible’s YAML Basics are great quick reads. For more, consult Official YAML Specifications.

We hope that the short article provide helpful information on how to handle mapping values are not allowed in this context error in YAML files. You may want to check out our other great articles like how to use RegEx in VSCodeHandle merge conflicts and Quickly create HTML Boilerplate in VSCode.

If you have any question, don’t hesitate to let us know in the comment section below.

1 answer to this question.

Hey, @Nishit seems like you’ve made a small syntax error. 

yml files are very particular about the indentations and the spacing. The error log itself is showing you the where you’ve gone wrong 

Remove a space before volume-size: 8

It should be like this:

volume_type: gp2
volume_size: 8






answered

Jan 21, 2019


by
Emily



Related Questions In Ansible

  • All categories

  • ChatGPT
    (4)

  • Apache Kafka
    (84)

  • Apache Spark
    (596)

  • Azure
    (131)

  • Big Data Hadoop
    (1,907)

  • Blockchain
    (1,673)

  • C#
    (141)

  • C++
    (271)

  • Career Counselling
    (1,060)

  • Cloud Computing
    (3,446)

  • Cyber Security & Ethical Hacking
    (147)

  • Data Analytics
    (1,266)

  • Database
    (855)

  • Data Science
    (75)

  • DevOps & Agile
    (3,575)

  • Digital Marketing
    (111)

  • Events & Trending Topics
    (28)

  • IoT (Internet of Things)
    (387)

  • Java
    (1,247)

  • Kotlin
    (8)

  • Linux Administration
    (389)

  • Machine Learning
    (337)

  • MicroStrategy
    (6)

  • PMP
    (423)

  • Power BI
    (516)

  • Python
    (3,188)

  • RPA
    (650)

  • SalesForce
    (92)

  • Selenium
    (1,569)

  • Software Testing
    (56)

  • Tableau
    (608)

  • Talend
    (73)

  • TypeSript
    (124)

  • Web Development
    (3,002)

  • Ask us Anything!
    (66)

  • Others
    (1,947)

  • Mobile Development
    (263)

Subscribe to our Newsletter, and get personalized recommendations.

Already have an account? Sign in.

I’m trying to write an ansible playbook which only upgrades the linux-generic package but cannot find the magic incantation to get it to actually work. With this play, I get the error parameters are mutually exclusive: deb|package|upgrade which google suggests is due to upgrade: full is not meant for a single package:

  - name: Check if linux-generic is installed
    command: dpkg -l linux-generic
    register: pkg_chk

  - name: update kernel if installed
    when: pkg_chk.rc == 0
    apt:
      update_cache: yes
      autoremove  : yes
      autoclean   : no
      upgrade     : full
      dpkg_options: 'force-confold,force-confdef'
      name        : linux-generic

Removing upgrade: full runs the play…

TASK [update kernel if installed] **********************************************************
ok: [vivaldi.fammed.wisc.edu]

But, when I login to the machine and run aptitude full-upgrade it shows ansible didn’t do anything:

# aptitude full-upgrade
The following NEW packages will be installed:
  linux-headers-4.4.0-203{a} linux-headers-4.4.0-203-generic{a}
  linux-image-4.4.0-203-generic{a} linux-modules-4.4.0-203-generic{a}
  linux-modules-extra-4.4.0-203-generic{a}

Ok, so I thought I’d just throw the command: module at it and use bare apt commands but there is some issue with the command string that I have not escaped properly.

  - name: upgrade linux-generic
    when: pkg_chk.rc == 0
      command: apt-get install -y --only-upgrade -o Dpkg::Options::='force-confdef' -o Dpkg::Options::='force-confold' linux-generic
      register: install_chk

  - name: autoremove linux-generic
    when: install_chk.rc == 0
      command: apt-get -y autoremove -o Dpkg::Options::='force-confdef' -o Dpkg::Options::='force-confold'
      register: auto_chk

which yields this error:

Syntax Error while loading YAML.
  mapping values are not allowed in this context
    The error appears to be in '/usr/src/ansible/tests/upgrade-kernel-only.yml': line 24, column 12, but may
    be elsewhere in the file depending on the exact syntax problem.
    
    The offending line appears to be:
    
        when: pkg_chk.rc == 0
          command: apt-get install -y --only-upgrade -o Dpkg::Options::='force-confdef' -o Dpkg::Options::='force-confold' linux-generic
               ^ here

I’ve tried escaping the : = characters, as well as replacing double-quotes and single quotes. Anyone know how to get this working?

Понравилась статья? Поделить с друзьями:
  • Syntax error unexpected token foreach
  • Syntax error unexpected token else after effects
  • Syntax error unexpected t string define
  • Syntax error unexpected t double arrow
  • Syntax error unexpected string content expecting or identifier or variable or number