`'item' is undefined` even though the task has a `loop`
BeginnerTroubleshootingYAML, Syntax and Jinja2 Errors
Answer
Likely causes → loop: is indented under the module arguments instead of at task level. Ansible renders the module arguments first, so {{ item }} is undefined. If that did not fail, you would see Unsupported parameters for (ansible.builtin.apt) module: loop.
Diagnose →
- ansible.builtin.apt:
name: "{{ item }}"
loop: [git, curl] # wrong level
Fix → Dedent loop: so it lines up with ansible.builtin.apt:. Better still, pass the list directly with name: [git, curl], which is faster too.
Prevent → loop, when, register, notify and become are task keywords. They always sit at the same indentation as the module name.
More on YAML, Syntax and Jinja2 Errors
`Syntax Error while loading YAML. mapping values are not allowed in this context`Beginner`found character '\t' that cannot start any token`Beginner`ERROR! conflicting action statements: ansible.builtin.copy, dest`IntermediateAnsible says `This one looks easy to fix. It seems that there is a value started with a quote, and the YAML parser is expecting to see the line ended with the same kind of quote.`Beginner`We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value.`Beginner`ERROR! A malformed block was encountered while loading tasks: {...} should be a list or None but is <class 'ansible.parsing.yaml.objects.AnsibleMapping'>`Intermediate