cscan cscan - 4 months ago 21
Ini Question

Ansible INI lookup

I'm looking up values in an INI file using Ansible's lookup function. Here's the example in the documentation:

- debug: msg="User in integration is {{ lookup('ini', 'user section=integration file=users.ini') }}"


And here's my task:

- set_fact: aws_access_var = "{{ lookup('ini', 'AWS_ACCESS_KEY_ID section=Credentials file=/etc/boto.cfg') }}"


They look identical in syntax but my task fails:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "template error while templating string: unexpected char u\"'\" at 18. String: \"{{ lookup('ini', 'AWS_ACCESS_KEY_ID section"}


Any idea what's wrong with it?

Answer

Remove the space around the equals sign, as that how ansible will parse the parameters. The following works fine:

---
- hosts: 127.0.0.1
  tasks:
    - set_fact: aws_access_var="{{ lookup('ini', 'AWS_ACCESS_KEY_ID section=Credentials file=boto.cfg') }}"
    - debug: msg="var is {{ aws_access_var }} "

Alternatively don't use the old style module calls, but use the YAML style parameter passing. It is usually less error prone. Here is the multiline YAML version:

---
- hosts: 127.0.0.1
  tasks:
    - set_fact:
        aws_access_var: "{{ lookup('ini', 'AWS_ACCESS_KEY_ID section=Credentials file=boto.cfg') }}"
    - debug:
        msg: "var is {{ aws_access_var }} "

Or the single line JSON style one:

---
- hosts: 127.0.0.1
  tasks:
    - set_fact: { aws_access_var: "{{ lookup('ini', 'AWS_ACCESS_KEY_ID section=Credentials file=boto.cfg') }}" }
    - debug: { msg: "var is {{ aws_access_var }} " }

All of the above playbooks are essentially equivalent and return the same message

Comments