Isso pode ser feito usando loops aninhados e inclui, exemplo de script de trabalho test.yml
:
---
- hosts: localhost
vars:
- stack:
stack_outputs:
Roles: "webserver balancer dbserver"
dbserver: "54.0.1.1 54.0.1.2"
balancer: "54.0.2.3"
webserver: "54.0.2.5 54.0.2.7 54.0.3.1"
tasks:
- debug: var=stack.stack_outputs
- include: dynamic.yml
host_group={{ item.key }}
group_ips={{ item.value }}
with_dict: "{{ stack.stack_outputs }}"
dynamic.yml
:
---
- debug:
msg="Group is {{ host_group }} and ip is {{ item2 }}"
when: item2|ipaddr
with_items: "{{ group_ips.split() }}"
loop_control:
loop_var: item2
Exemplo de execução usando ansible-playbook 2.1.2.0
:
ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"stack.stack_outputs": {
"Roles": "webserver balancer dbserver",
"balancer": "54.0.2.3",
"dbserver": "54.0.1.1 54.0.1.2",
"webserver": "54.0.2.5 54.0.2.7 54.0.3.1"
}
}
TASK [include] *****************************************************************
included: /home/say/tmp/ansible/dynamic.yml for localhost
included: /home/say/tmp/ansible/dynamic.yml for localhost
included: /home/say/tmp/ansible/dynamic.yml for localhost
included: /home/say/tmp/ansible/dynamic.yml for localhost
TASK [debug] *******************************************************************
ok: [localhost] => (item=None) => {
"item_foo": "54.0.2.5",
"msg": "Group is webserver and ip is 54.0.2.5"
}
ok: [localhost] => (item=None) => {
"item_foo": "54.0.2.7",
"msg": "Group is webserver and ip is 54.0.2.7"
}
ok: [localhost] => (item=None) => {
"item_foo": "54.0.3.1",
"msg": "Group is webserver and ip is 54.0.3.1"
}
TASK [debug] *******************************************************************
ok: [localhost] => (item=None) => {
"item_foo": "54.0.2.3",
"msg": "Group is balancer and ip is 54.0.2.3"
}
TASK [debug] *******************************************************************
skipping: [localhost] => (item=None)
skipping: [localhost] => (item=None)
skipping: [localhost] => (item=None)
TASK [debug] *******************************************************************
ok: [localhost] => (item=None) => {
"item_foo": "54.0.1.1",
"msg": "Group is dbserver and ip is 54.0.1.1"
}
ok: [localhost] => (item=None) => {
"item_foo": "54.0.1.2",
"msg": "Group is dbserver and ip is 54.0.1.2"
}
PLAY RECAP *********************************************************************
localhost : ok=9 changed=0 unreachable=0 failed=0
Você precisa Ansible 2.1 ou acima para executá-lo.