Using Ansible 2.5.0.
Question: How do I dynamically access the host variables of the current hosts in play, during runtime execution.
Inventory file:
[group_one]
host-1 ansible_host=1.2.3.4
host-2 ansible_host=5.6.7.8
[group_two]
host-3 ansible_host=2.4.6.8
My current task:
- hosts: group_one
tasks:
- name: set fact for all ip's in play execution
set_fact:
all_ips: "{{ groups['all'] | map('extract', hostvars, ['ansible_host'] ) | join(',') }}"
# outcome = 1.2.3.4,5.6.7.8,2.4.6.8
This outcome is undesired. During execution, only host-1 and 2 are used, but the IP of host-3 is added as well.
Now, I've looked at the magic variables of Ansible, but it seems there is no variable which could be used in a way that it satisfy my needs.
Because I want the variable during runtime, I do not want to set the variable "{{ groups['group_one'] }}"
as this would defeat the purpose.
How do I configure Ansible in such a way, the outcome would be 1.2.3.4,5.6.7.8
, without statically configuring any variables?