0
votes

I need to output multiple varables (key-value pairs) from ansible lookup and save them to a json file. My json file should look like this:

{
  "name": "name1",
  "password": "pass1",
  "creationDate": 2019-01-01 00:44:35
}

I have the following code in my ansible playbook:

- hosts: localhost
  vars:
    my_variables:
      "name": "{{ lookup('env', 'name') }}",
      "password": "{{ lookup('env', 'password') }}",
      "creationDate": "{{ '%Y-%m-%d %H:%M:%S'| strftime(ansible_date_time.epoch) }}"

  tasks:
  - name: Create /tmp/{{ lookup('env', 'name') }}.json  
    file:
      name: '/tmp/{{ lookup('env', 'name') }}.json'
      state: touch
  - name: Write my_variables to /tmp/{{ lookup('env', 'name') }}.json
    copy:
      content: "{{ my_varibles | to_nice_json }}"
      dest: "/tmp/{{ lookup('env', 'name') }}.json"

I was able to create /tmp/name1.json file. However, my name1.json file looks like this:

{
  "name": "",
  "password": "",
  "creationDate": 2019-01-01 00:44:35
}

The values for name & password from lookup are missing.

1

1 Answers

0
votes

You have a typo in your playbook, and I have absolutely zero idea how your playbook ever worked, because ansible 2.7 and pyyaml both :fu: when given that syntax:

- hosts: localhost
  vars:
    my_variables:
      "name": "{{ lookup('env', 'name') }}", # <-- should not end with a comma