Gerald Shyti
Gerald Shyti

Reputation: 51

What is the problem with this ansible YAML file?

I'm trying to iterate a list of elements in Ansible, using this code to add via the Jira REST API the users to the "Jira-software-users" group in Jira Server.

 - name: Add user
  uri:
    url: "{{ atlassian_url }}/rest/api/2/user?groupname=jira-software-users"
    method: POST
    user: "{{ jira_username }}"
    password: "{{ jira_password }}"
    return_content: yes
    force_basic_auth: yes
    body_format: json
    headers:
      Accept: 'application/json'
      Content-Type: 'application/json'
    body: "{ 'name': "{{ item }}"
             }"
  register: result
  loop: "{{ users }}"
  tags:
    - adduser

The code seems good to me, but it continues to show an error that does not give enough explication. Here the error:

ERROR! Syntax Error while loading YAML. did not find expected key

The error appears to be in '/root/createProject2.yml': line 45, column 27, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      Content-Type: 'application/json'
    body: "{ 'name': "{{ item }}"
                      ^ here

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. For instance:

with_items:
  - {{ foo }}

Should be written as:

with_items:
  - "{{ foo }}"

Obviously by applying the automated suggestion nothing is solved, since by definition I'm applying the concepts explained in the Ansible documentation about the loops: https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html

Could you suggest me a way to solve this problem?

Thank you

Upvotes: 0

Views: 309

Answers (1)

chash
chash

Reputation: 4433

Your problem is with quoting. You have un-escaped double quotes inside a double-quoted string, which Ansible is unable to parse. In this case, you can quote the JSON string with single quotes and use double quotes within:

body: '{ "name": "{{ item }}" }'

Upvotes: 4

Related Questions