Ansible webserver backup: pull other host vars to generate backup scripts

Solution 1:

Q: "Is it possible to iterate through the host variables of another host outside of the current host and groups?"

(I have changed group/groups. A host can be a member of multiple groups.)

A: Yes. It is possible. hostvars is a dictionary of all hosts and their variables in the play, e.g

{% for item in ansible_play_hosts_all %}
Host {{ hostvars[item]['ansible_host'] }}
   Hostname {{ hostvars[item]['ansible_host'] }}
   User {{ hostvars[item]['ansible_user'] }}
   Port {{ hostvars[item]['ansible_port'] }}
   IdentityFile {{ hostvars[item]['ansible_ssh_private_key_file'] }}
{% endfor %}
  • Such a host must be a member of ansible_play_hosts_all. Quoting

"List of all the hosts that were targeted by the play"

  • If a host were not targeted by the play Ansible knows nothing about it, of course.

  • You will very probably want to delegate_to and run_once such a task.