Bug Report
Similar to the report #9931
Ubuntu 14.04
The add_host
Task only executes on a single host in a play's host list.
playbook
---
- name: Environment group_vars
hosts: all
gather_facts: no
tasks:
- debug: var=inventory_hostname
- name: Add to group
add_host: name={{inventory_hostname}} groups={{env}}
- name: Groups
hosts: all
gather_facts: no
tasks:
- debug: var=group_names
- debug: var=groups[env]
- fail: msg="done"
inventory file try.yml
10.0.0.70
10.0.1.171
execution
ansible-playbook -i ansible/try ansible/try.yml -e 'env=acceptance'
I expect the task which uses the add_host
module, Add to group, to execute twice: once for each host in the play. At the end of the play, each host would be in the group acceptance
.
PLAY [Environment group_vars] *************************************************
TASK: [debug var=inventory_hostname] ******************************************
ok: [10.0.0.70] => {
"var": {
"inventory_hostname": "10.0.0.70"
}
}
ok: [10.0.1.171] => {
"var": {
"inventory_hostname": "10.0.1.171"
}
}
TASK: [Add to group] **********************************************************
ok: [10.0.0.70]
PLAY [Groups] ***************************************************************** [30/1905]
TASK: [debug var=group_names] *************************************************
ok: [10.0.0.70] => {
"var": {
"group_names": [
"acceptance",
"ungrouped"
]
}
}
ok: [10.0.1.171] => {
"var": {
"group_names": [
"ungrouped"
]
}
}
TASK: [debug var=groups[env]] *************************************************
ok: [10.0.0.70] => {
"var": {
"groups[env]": [
"10.0.0.70"
]
}
}
TASK: [debug var=groups[env]] *************************************************
ok: [10.0.0.70] => {
"var": {
"groups[env]": [
"10.0.0.70"
]
}
}
ok: [10.0.1.171] => {
"var": {
"groups[env]": [
"10.0.0.70"
]
}
}
TASK: [fail msg="done"] *******************************************************
failed: [10.0.0.70] => {"failed": true}
msg: done
failed: [10.0.1.171] => {"failed": true}
msg: done
FATAL: all hosts have already failed -- aborting
Hi!
Thanks very much for your submission to Ansible. It sincerely means a lot to us.
We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.
In particular, this is expected behavior, which is documented in this page (the note):
http://docs.ansible.com/add_host_module.html#id3
In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.
Thank you once again for this and your interest in Ansible!
in any case you are not adding new hosts to inventory, you might want to use the group_by directive instead.
@bcoca, and what if I want to add new host to inventory for each host from another group?
Most helpful comment
@bcoca, and what if I want to add new host to inventory for each host from another group?