Using information between Ansible playbooks

Hi,

How do i store/use information retrieved from running a playbook task later in another playbook task?

I tried dumping the information in json format from the first playbook/task to a file. But then how do i read/pass this information to the new playbook task as input?

thanks,
Raji

Short answer: Use "vars_files" or "include_vars".

Details: It's possible to store the variables in a file. For example the play

        - hosts: localhost
          vars:
            test_var1: VAR-1
            test_var2: VAR-2
            test_var3: VAR-3
          tasks:
            - copy:
                content: |
                  test_var1: {{ test_var1 }}
                  test_var2: {{ test_var2 }}
                  test_var3: {{ test_var3 }}
                dest: amnesiac-01.yml

creates the file with the variables

        $ cat amnesiac-01.yml
        test_var1: VAR-1
        test_var2: VAR-2
        test_var3: VAR-3

Use "vars_files" or "include_vars" to read the variables. For example the play

        - hosts: localhost
          tasks:
            - include_vars: amnesiac-01.yml
            - debug:
                msg: "{{ msg.split('\n') }}"
              vars:
                msg: |
                  test_var1: {{ test_var1 }}
                  test_var2: {{ test_var2 }}
                  test_var3: {{ test_var3 }}

gives

        ok: [localhost] => {
            "msg": [
                "test_var1: VAR-1",
                "test_var2: VAR-2",
                "test_var3: VAR-3",
                ""
            ]
        }
...

Cheers,

  -vlado

Thanks. It works for localhost.

But let’s say i need to save information from a task on all remote hosts in a local file (or files), how do i do it? So that in the next yml file, i can load the local file for all remote hosts.

It's possible to store all "hostvars". For example with this template

      $ cat my_hostvars.json.j2
      my_hostvars_all:
      {% for my_host in ansible_play_hosts_all %}
        {{ my_host }}:
          {{ hostvars[my_host]|to_nice_json }}
      {% endfor %}

the playbook below stores "hostvars" of all hosts in the dictionary
"my_hostvars_all" and put it into the file
"{{ inventory_dir }}/my_hostvars.json" at localhost

      - hosts: test_jails
        tasks:
          - set_fact:
              test_var: "test_var_in_{{ inventory_hostname }}"
          - template:
              src: my_hostvars.json.j2
              dest: "{{ inventory_dir }}/my_hostvars.json"
            delegate_to: localhost
            run_once: true

The dictionary can be included in the next playbook. For example the playbook
below

      - hosts: test_jails
        tasks:
          - include_vars: my_hostvars.json
          - set_fact:
              my_hostvars: "{{ my_hostvars_all[inventory_hostname] }}"
          - debug:
              var: my_hostvars.test_var

gives

      ok: [test_01] => {
          "my_hostvars.test_var": "test_var_in_test_01"
      }
      ok: [test_02] => {
          "my_hostvars.test_var": "test_var_in_test_02"
      }
      ok: [test_03] => {
          "my_hostvars.test_var": "test_var_in_test_03"
      }

Cheers,

  -vlado

Thanks Vladimir. Actually ‘delegate_to: localhost’ helped, and i’m using it with the copy module itself.

Thanks,
Raji