Copy host inventory in file

Hello,

How to put each host each time in file

example:

hosts file:

[webservers]

host1

hos2

Is this what you're looking for ?

    - shell: "echo {{ item }} >> file.txt"
      loop: "{{ ansible_play_hosts_all }}"
      run_once: true
      delegate_to: localhost

FWIW, you can improve the log by prepending the date

    - block:
        - command: date
          register: result
        - shell: "echo {{ result.stdout}} {{ item }} >> file.txt"
          loop: "{{ ansible_play_hosts_all }}"
      run_once: true
      delegate_to: localhost

Thank you, but i need to copy each time one host

Example:

  • First execution of playbook : copy host1

  • second execution of playbook : copy host2

Thank you

how to create a template jinja for copy hosts of static inventory