Ansible: `add_host` module is only executed once, on the first host to which the play applies

Created on 14 Apr 2015  ·  3Comments  ·  Source: ansible/ansible

Issue Type:

Bug Report

Similar to the report #9931

Ansible Version:
  • Ansible 1.8.4 (detached HEAD ebc8d48d34) last updated 2015/04/14 17:59:44 (GMT +000)
  • Ansible 1.9.0 (detached HEAD 717ffe2bea) last updated 2015/04/14 17:58:24 (GMT +000)
  • Ansible 2.0.0 (devel 0345b675f8) last updated 2015/04/14 17:27:56 (GMT +000)
    Environment:

Ubuntu 14.04

Summary:

The add_host Task only executes on a single host in a play's host list.

Steps To Reproduce:

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'

Expected Results:

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.

Actual Results:
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

Most helpful comment

@bcoca, and what if I want to add new host to inventory for each host from another group?

All 3 comments

Possible Misunderstanding

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?

Was this page helpful?
0 / 5 - 0 ratings