0

I have the following playbook:

- name: Get-IP
  hosts: webservers

  tasks:

    - debug: var=ansible_all_ipv4_addresses
      register: foo

    - debug:
        var: foo

    - local_action: lineinfile line= {{ foo }} path=/root/file2.txt

I added debug to make sure variable foo carrying the IP address, and its working correctly, but when I try to save it to file on local desk, files remains empty. If I delete the file I get an error about the file does not exist.

Whats wrong with my playbook? Thanks.

U880D
  • 8,601
  • 6
  • 24
  • 40
XP_2600
  • 57
  • 6

2 Answers2

0

Write the lines in a loop. For example,

shell> cat pb.yml
- hosts: webservers
  tasks:
    - debug:
        var: ansible_all_ipv4_addresses
    - lineinfile:
        create: true
        dest: /tmp/ansible_all_ipv4_addresses.webservers
        line: "{{ item }} {{ hostvars[item].ansible_all_ipv4_addresses|join(',') }}"
      loop: "{{ ansible_play_hosts }}"
      run_once: true
      delegate_to: localhost

gives

shell> ansible-playbook  pb.yml

PLAY [webservers] ****************************************************************************

TASK [Gathering Facts] ***********************************************************************
ok: [test_11]
ok: [test_13]

TASK [debug] *********************************************************************************
ok: [test_11] => 
  ansible_all_ipv4_addresses:
  - 10.1.0.61
ok: [test_13] => 
  ansible_all_ipv4_addresses:
  - 10.1.0.63

TASK [lineinfile] ****************************************************************************
changed: [test_11 -> localhost] => (item=test_11)
changed: [test_11 -> localhost] => (item=test_13)

PLAY RECAP ***********************************************************************************
test_11: ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_13: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

shell> cat /tmp/ansible_all_ipv4_addresses.webservers
test_11 10.1.0.61
test_13 10.1.0.63
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

According your description and example I understand your use case that you like to gather Ansible facts about Remote Nodes and Cache facts on the Control Node.

If fact cache plugin is enabled

~/test$ grep fact_ ansible.cfg
fact_caching            = yaml # or json
fact_caching_connection = /tmp/ansible/facts_cache
fact_caching_timeout    = 129600

a minimal example playbook

---
- hosts: test
  become: false
  gather_facts: true
  gather_subset:
    - "!all"
    - "!min"
    - "network"

  tasks:

  - name: Show Facts network
    debug:
      msg: "{{ ansible_facts }}"

will result into local files like

~/test$ grep -A1 _ipv4 /tmp/ansible/facts_cache/test.example.com
ansible_all_ipv4_addresses:
- 192.0.2.1
--
ansible_default_ipv4:
  address: 192.0.2.1
--
...

By following this approach there is no need for re-implementing already existing functionality, less and easier to maintain code, less error prone, and so on.

Furthermore,

Fact caching can improve performance. If you manage thousands of hosts, you can configure fact caching to run nightly, then manage configuration on a smaller set of servers periodically throughout the day. With cached facts, you have access to variables and information about all hosts even when you are only managing a small number of servers.

Similar Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40