# add\_host module not adding multiple hosts

**URL:** <https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526>\
**Category:** Ansible Project\
**Created:** [May 23, 2016, 5:15pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526 "2016-05-23T17:15:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_S1](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@Eric\_S1](https://forum.ansible.com/u/Eric_S1)\
**Post date:** [May 23, 2016, 5:15pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/1 "2016-05-23T17:15:28Z")

</div>

ansible ver=2.0.2

When I’m trying to add more than one host to a group its only adding the first one in the list. Here is my task to add hosts:

- name: Add hosts group temporary inventory group  
add\_host: name={{ item.private\_ip }} groups=dynamic  
with\_items: “{{ ec2.instances }}”

This task does use the ec2.instances properly and will do it for all the hosts that I have created:

- name: Wait for SSH  
wait\_for:  
host: “{{ item.private\_ip }}”  
port: 22  
delay: 10  
timeout: 320  
state: started  
with\_items: “{{ ec2.instances }}”

So I’m creating 3 hosts and the add\_host task only adds the first one but the wait for ssh task will wait for all three and I get an ok back for the wait for ssh task on all three.

---

<div class="post-metadata">

**Author:** ![Eric\_S1](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@Eric\_S1](https://forum.ansible.com/u/Eric_S1)\
**Post date:** [May 24, 2016, 5:41pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/2 "2016-05-24T17:41:44Z")

</div>

anyone have an idea of why this is working like this? I’ve tried all version of ansible 1.9/2.0.0/2.0.1/2.0.2/2.1 and all the same result.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 24, 2016, 5:44pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/3 "2016-05-24T17:44:23Z")

</div>

It might help if you provided the output of your playbook run with `-v` so we can see what you are seeing.

---

<div class="post-metadata">

**Author:** ![Eric\_S1](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@Eric\_S1](https://forum.ansible.com/u/Eric_S1)\
**Post date:** [May 24, 2016, 6:29pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/4 "2016-05-24T18:29:12Z")

</div>

sorry for the long output:  
PLAY [ec2\_instances] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:24:42 -0500 (0:00:00.019) 0:00:00.019 \*\*\*\*\*\*\*\*\*\*\*  
ok: [aaa-frs-ans-test2]  
ok: [aaa-frs-ans-test3]  
ok: [aaa-frs-ans-test1]

TASK [Launch Instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:24:46 -0500 (0:00:04.019) 0:00:04.038 \*\*\*\*\*\*\*\*\*\*\*  
changed: [aaa-frs-ans-test3] =\> {“changed”: true, “instance\_ids”: [“i-683925ef”], “instances”: [{“ami\_launch\_index”: “0”, “architecture”: “x86\_64”, “block\_device\_mapping”: {“/dev/sda1”: {“delete\_on\_termination”: true, “status”: “attached”, “volume\_id”: “vol-712c60da”}}, “dns\_name”: “”, “ebs\_optimized”: false, “groups”: {“sg-7c538d04”: “fsr-allow-all”}, “hypervisor”: “xen”, “id”: “i-683925ef”, “image\_id”: “ami-3683605b”, “instance\_type”: “t2.micro”, “kernel”: null, “key\_name”: “FSR\_DEV\_QA\_KEYPAIR”, “launch\_time”: “2016-05-24T18:24:48.000Z”, “placement”: “us-east-1a”, “private\_dns\_name”: “ip-10-196-142-189.ec2.internal”, “private\_ip”: “10.196.142.189”, “public\_dns\_name”: “”, “public\_ip”: null, “ramdisk”: null, “region”: “us-east-1”, “root\_device\_name”: “/dev/sda1”, “root\_device\_type”: “ebs”, “state”: “running”, “state\_code”: 16, “tags”: {“Env”: “Dev”, “Name”: “aaa-fsr-ans-test2”}, “tenancy”: “default”, “virtualization\_type”: “hvm”}], “tagged\_instances”: }  
changed: [aaa-frs-ans-test2] =\> {“changed”: true, “instance\_ids”: [“i-b53a2632”], “instances”: [{“ami\_launch\_index”: “0”, “architecture”: “x86\_64”, “block\_device\_mapping”: {“/dev/sda1”: {“delete\_on\_termination”: true, “status”: “attached”, “volume\_id”: “vol-1a2c60b1”}}, “dns\_name”: “”, “ebs\_optimized”: false, “groups”: {“sg-7c538d04”: “fsr-allow-all”}, “hypervisor”: “xen”, “id”: “i-b53a2632”, “image\_id”: “ami-3683605b”, “instance\_type”: “t2.micro”, “kernel”: null, “key\_name”: “FSR\_DEV\_QA\_KEYPAIR”, “launch\_time”: “2016-05-24T18:24:48.000Z”, “placement”: “us-east-1a”, “private\_dns\_name”: “ip-10-196-141-57.ec2.internal”, “private\_ip”: “10.196.141.57”, “public\_dns\_name”: “”, “public\_ip”: null, “ramdisk”: null, “region”: “us-east-1”, “root\_device\_name”: “/dev/sda1”, “root\_device\_type”: “ebs”, “state”: “running”, “state\_code”: 16, “tags”: {“Env”: “Dev”, “Name”: “aaa-fsr-ans-test2”}, “tenancy”: “default”, “virtualization\_type”: “hvm”}], “tagged\_instances”: }  
changed: [aaa-frs-ans-test1] =\> {“changed”: true, “instance\_ids”: [“i-4f3a26c8”], “instances”: [{“ami\_launch\_index”: “0”, “architecture”: “x86\_64”, “block\_device\_mapping”: {“/dev/sda1”: {“delete\_on\_termination”: true, “status”: “attached”, “volume\_id”: “vol-722c60d9”}}, “dns\_name”: “”, “ebs\_optimized”: false, “groups”: {“sg-7c538d04”: “fsr-allow-all”}, “hypervisor”: “xen”, “id”: “i-4f3a26c8”, “image\_id”: “ami-3683605b”, “instance\_type”: “t2.micro”, “kernel”: null, “key\_name”: “FSR\_DEV\_QA\_KEYPAIR”, “launch\_time”: “2016-05-24T18:24:48.000Z”, “placement”: “us-east-1a”, “private\_dns\_name”: “ip-10-196-145-104.ec2.internal”, “private\_ip”: “10.196.145.104”, “public\_dns\_name”: “”, “public\_ip”: null, “ramdisk”: null, “region”: “us-east-1”, “root\_device\_name”: “/dev/sda1”, “root\_device\_type”: “ebs”, “state”: “running”, “state\_code”: 16, “tags”: {“Env”: “Dev”, “Name”: “aaa-fsr-ans-test1”}, “tenancy”: “default”, “virtualization\_type”: “hvm”}], “tagged\_instances”: }

TASK [Add hosts group temporary inventory group] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:25:29 -0500 (0:00:43.674) 0:00:47.713 \*\*\*\*\*\*\*\*\*\*\*  
changed: [aaa-frs-ans-test1] =\> (item={u’kernel’: None, u’root\_device\_type’: u’ebs’, u’private\_dns\_name’: u’ip-10-196-145-104.ec2.internal’, u’public\_ip’: None, u’private\_ip’: u’10.196.145.104’, u’id’: u’i-4f3a26c8’, u’ebs\_optimized’: False, u’state’: u’running’, u’virtualization\_type’: u’hvm’, u’architecture’: u’x86\_64’, u’ramdisk’: None, u’block\_device\_mapping’: {u’/dev/sda1’: {u’status’: u’attached’, u’delete\_on\_termination’: True, u’volume\_id’: u’vol-722c60d9’}}, u’key\_name’: u’FSR\_DEV\_QA\_KEYPAIR’, u’image\_id’: u’ami-3683605b’, u’tenancy’: u’default’, u’groups’: {u’sg-7c538d04’: u’fsr-allow-all’}, u’public\_dns\_name’: u’‘, u’state\_code’: 16, u’tags’: {u’Name’: u’aaa-fsr-ans-test1’, u’Env’: u’Dev’}, u’placement’: u’us-east-1a’, u’ami\_launch\_index’: u’0’, u’dns\_name’: u’‘, u’region’: u’us-east-1’, u’launch\_time’: u’2016-05-24T18:24:48.000Z’, u’instance\_type’: u’t2.micro’, u’root\_device\_name’: u’/dev/sda1’, u’hypervisor’: u’xen’}) =\> {“add\_host”: {“groups”: [“dynamic”], “host\_name”: “10.196.145.104”, “host\_vars”: {}}, “changed”: true, “item”: {“ami\_launch\_index”: “0”, “architecture”: “x86\_64”, “block\_device\_mapping”: {“/dev/sda1”: {“delete\_on\_termination”: true, “status”: “attached”, “volume\_id”: “vol-722c60d9”}}, “dns\_name”: “”, “ebs\_optimized”: false, “groups”: {“sg-7c538d04”: “fsr-allow-all”}, “hypervisor”: “xen”, “id”: “i-4f3a26c8”, “image\_id”: “ami-3683605b”, “instance\_type”: “t2.micro”, “kernel”: null, “key\_name”: “FSR\_DEV\_QA\_KEYPAIR”, “launch\_time”: “2016-05-24T18:24:48.000Z”, “placement”: “us-east-1a”, “private\_dns\_name”: “ip-10-196-145-104.ec2.internal”, “private\_ip”: “10.196.145.104”, “public\_dns\_name”: “”, “public\_ip”: null, “ramdisk”: null, “region”: “us-east-1”, “root\_device\_name”: “/dev/sda1”, “root\_device\_type”: “ebs”, “state”: “running”, “state\_code”: 16, “tags”: {“Env”: “Dev”, “Name”: “aaa-fsr-ans-test1”}, “tenancy”: “default”, “virtualization\_type”: “hvm”}}

TASK [Wait for SSH] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:25:29 -0500 (0:00:00.046) 0:00:47.759 \*\*\*\*\*\*\*\*\*\*\*  
ok: [aaa-frs-ans-test1] =\> (item={u’kernel’: None, u’root\_device\_type’: u’ebs’, u’private\_dns\_name’: u’ip-10-196-145-104.ec2.internal’, u’public\_ip’: None, u’private\_ip’: u’10.196.145.104’, u’id’: u’i-4f3a26c8’, u’ebs\_optimized’: False, u’state’: u’running’, u’virtualization\_type’: u’hvm’, u’architecture’: u’x86\_64’, u’ramdisk’: None, u’block\_device\_mapping’: {u’/dev/sda1’: {u’status’: u’attached’, u’delete\_on\_termination’: True, u’volume\_id’: u’vol-722c60d9’}}, u’key\_name’: u’FSR\_DEV\_QA\_KEYPAIR’, u’image\_id’: u’ami-3683605b’, u’tenancy’: u’default’, u’groups’: {u’sg-7c538d04’: u’fsr-allow-all’}, u’public\_dns\_name’: u’‘, u’state\_code’: 16, u’tags’: {u’Name’: u’aaa-fsr-ans-test1’, u’Env’: u’Dev’}, u’placement’: u’us-east-1a’, u’ami\_launch\_index’: u’0’, u’dns\_name’: u’‘, u’region’: u’us-east-1’, u’launch\_time’: u’2016-05-24T18:24:48.000Z’, u’instance\_type’: u’t2.micro’, u’root\_device\_name’: u’/dev/sda1’, u’hypervisor’: u’xen’}) =\> {“changed”: false, “elapsed”: 72, “item”: {“ami\_launch\_index”: “0”, “architecture”: “x86\_64”, “block\_device\_mapping”: {“/dev/sda1”: {“delete\_on\_termination”: true, “status”: “attached”, “volume\_id”: “vol-722c60d9”}}, “dns\_name”: “”, “ebs\_optimized”: false, “groups”: {“sg-7c538d04”: “fsr-allow-all”}, “hypervisor”: “xen”, “id”: “i-4f3a26c8”, “image\_id”: “ami-3683605b”, “instance\_type”: “t2.micro”, “kernel”: null, “key\_name”: “FSR\_DEV\_QA\_KEYPAIR”, “launch\_time”: “2016-05-24T18:24:48.000Z”, “placement”: “us-east-1a”, “private\_dns\_name”: “ip-10-196-145-104.ec2.internal”, “private\_ip”: “10.196.145.104”, “public\_dns\_name”: “”, “public\_ip”: null, “ramdisk”: null, “region”: “us-east-1”, “root\_device\_name”: “/dev/sda1”, “root\_device\_type”: “ebs”, “state”: “running”, “state\_code”: 16, “tags”: {“Env”: “Dev”, “Name”: “aaa-fsr-ans-test1”}, “tenancy”: “default”, “virtualization\_type”: “hvm”}, “path”: null, “port”: 22, “search\_regex”: null, “state”: “started”}  
ok: [aaa-frs-ans-test2] =\> (item={u’kernel’: None, u’root\_device\_type’: u’ebs’, u’private\_dns\_name’: u’ip-10-196-141-57.ec2.internal’, u’public\_ip’: None, u’private\_ip’: u’10.196.141.57’, u’id’: u’i-b53a2632’, u’ebs\_optimized’: False, u’state’: u’running’, u’virtualization\_type’: u’hvm’, u’architecture’: u’x86\_64’, u’ramdisk’: None, u’block\_device\_mapping’: {u’/dev/sda1’: {u’status’: u’attached’, u’delete\_on\_termination’: True, u’volume\_id’: u’vol-1a2c60b1’}}, u’key\_name’: u’FSR\_DEV\_QA\_KEYPAIR’, u’image\_id’: u’ami-3683605b’, u’tenancy’: u’default’, u’groups’: {u’sg-7c538d04’: u’fsr-allow-all’}, u’public\_dns\_name’: u’‘, u’state\_code’: 16, u’tags’: {u’Name’: u’aaa-fsr-ans-test2’, u’Env’: u’Dev’}, u’placement’: u’us-east-1a’, u’ami\_launch\_index’: u’0’, u’dns\_name’: u’‘, u’region’: u’us-east-1’, u’launch\_time’: u’2016-05-24T18:24:48.000Z’, u’instance\_type’: u’t2.micro’, u’root\_device\_name’: u’/dev/sda1’, u’hypervisor’: u’xen’}) =\> {“changed”: false, “elapsed”: 72, “item”: {“ami\_launch\_index”: “0”, “architecture”: “x86\_64”, “block\_device\_mapping”: {“/dev/sda1”: {“delete\_on\_termination”: true, “status”: “attached”, “volume\_id”: “vol-1a2c60b1”}}, “dns\_name”: “”, “ebs\_optimized”: false, “groups”: {“sg-7c538d04”: “fsr-allow-all”}, “hypervisor”: “xen”, “id”: “i-b53a2632”, “image\_id”: “ami-3683605b”, “instance\_type”: “t2.micro”, “kernel”: null, “key\_name”: “FSR\_DEV\_QA\_KEYPAIR”, “launch\_time”: “2016-05-24T18:24:48.000Z”, “placement”: “us-east-1a”, “private\_dns\_name”: “ip-10-196-141-57.ec2.internal”, “private\_ip”: “10.196.141.57”, “public\_dns\_name”: “”, “public\_ip”: null, “ramdisk”: null, “region”: “us-east-1”, “root\_device\_name”: “/dev/sda1”, “root\_device\_type”: “ebs”, “state”: “running”, “state\_code”: 16, “tags”: {“Env”: “Dev”, “Name”: “aaa-fsr-ans-test2”}, “tenancy”: “default”, “virtualization\_type”: “hvm”}, “path”: null, “port”: 22, “search\_regex”: null, “state”: “started”}  
ok: [aaa-frs-ans-test3] =\> (item={u’kernel’: None, u’root\_device\_type’: u’ebs’, u’private\_dns\_name’: u’ip-10-196-142-189.ec2.internal’, u’public\_ip’: None, u’private\_ip’: u’10.196.142.189’, u’id’: u’i-683925ef’, u’ebs\_optimized’: False, u’state’: u’running’, u’virtualization\_type’: u’hvm’, u’architecture’: u’x86\_64’, u’ramdisk’: None, u’block\_device\_mapping’: {u’/dev/sda1’: {u’status’: u’attached’, u’delete\_on\_termination’: True, u’volume\_id’: u’vol-712c60da’}}, u’key\_name’: u’FSR\_DEV\_QA\_KEYPAIR’, u’image\_id’: u’ami-3683605b’, u’tenancy’: u’default’, u’groups’: {u’sg-7c538d04’: u’fsr-allow-all’}, u’public\_dns\_name’: u’‘, u’state\_code’: 16, u’tags’: {u’Name’: u’aaa-fsr-ans-test2’, u’Env’: u’Dev’}, u’placement’: u’us-east-1a’, u’ami\_launch\_index’: u’0’, u’dns\_name’: u’‘, u’region’: u’us-east-1’, u’launch\_time’: u’2016-05-24T18:24:48.000Z’, u’instance\_type’: u’t2.micro’, u’root\_device\_name’: u’/dev/sda1’, u’hypervisor’: u’xen’}) =\> {“changed”: false, “elapsed”: 72, “item”: {“ami\_launch\_index”: “0”, “architecture”: “x86\_64”, “block\_device\_mapping”: {“/dev/sda1”: {“delete\_on\_termination”: true, “status”: “attached”, “volume\_id”: “vol-712c60da”}}, “dns\_name”: “”, “ebs\_optimized”: false, “groups”: {“sg-7c538d04”: “fsr-allow-all”}, “hypervisor”: “xen”, “id”: “i-683925ef”, “image\_id”: “ami-3683605b”, “instance\_type”: “t2.micro”, “kernel”: null, “key\_name”: “FSR\_DEV\_QA\_KEYPAIR”, “launch\_time”: “2016-05-24T18:24:48.000Z”, “placement”: “us-east-1a”, “private\_dns\_name”: “ip-10-196-142-189.ec2.internal”, “private\_ip”: “10.196.142.189”, “public\_dns\_name”: “”, “public\_ip”: null, “ramdisk”: null, “region”: “us-east-1”, “root\_device\_name”: “/dev/sda1”, “root\_device\_type”: “ebs”, “state”: “running”, “state\_code”: 16, “tags”: {“Env”: “Dev”, “Name”: “aaa-fsr-ans-test2”}, “tenancy”: “default”, “virtualization\_type”: “hvm”}, “path”: null, “port”: 22, “search\_regex”: null, “state”: “started”}

TASK [Wait a little longer for centos] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:26:42 -0500 (0:01:12.287) 0:02:00.047 \*\*\*\*\*\*\*\*\*\*\*  
Pausing for 20 seconds  
(ctrl+C then ‘C’ = continue early, ctrl+C then ‘A’ = abort)  
ok: [aaa-frs-ans-test1] =\> {“changed”: false, “delta”: 19, “rc”: 0, “start”: “2016-05-24 13:26:42.068693”, “stderr”: “”, “stdout”: “Paused for 20.0 seconds”, “stop”: “2016-05-24 13:27:02.068025”, “user\_input”: “”}

PLAY [dynamic] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:27:02 -0500 (0:00:20.040) 0:02:20.087 \*\*\*\*\*\*\*\*\*\*\*  
ok: [10.196.145.104]

TASK [ansible-hostname-master : gather facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:27:06 -0500 (0:00:04.062) 0:02:24.150 \*\*\*\*\*\*\*\*\*\*\*  
ok: [10.196.145.104] =\> {“ansible\_facts”: {“ansible\_ec2\_ami\_id”: “ami-3683605b”, “ansible\_ec2\_ami\_launch\_index”: “0”, “ansible\_ec2\_ami\_manifest\_path”: “(unknown)”, “ansible\_ec2\_block\_device\_mapping\_ami”: “/dev/sda1”, “ansible\_ec2\_block\_device\_mapping\_root”: “/dev/sda1”, “ansible\_ec2\_hostname”: “[ip-10-196-145-104.aws.foreseeresults.com](http://ip-10-196-145-104.aws.foreseeresults.com)”, “ansible\_ec2\_instance\_action”: “none”, “ansible\_ec2\_instance\_id”: “i-4f3a26c8”, “ansible\_ec2\_instance\_type”: “t2.micro”, “ansible\_ec2\_local\_hostname”: “[ip-10-196-145-104.aws.foreseeresults.com](http://ip-10-196-145-104.aws.foreseeresults.com)”, “ansible\_ec2\_local\_ipv4”: “10.196.145.104”, “ansible\_ec2\_mac”: “0a:16:b5:46:d4:a1”, “ansible\_ec2\_metrics\_vhostmd”: “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_device\_number”: “0”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_interface\_id”: “eni-5d46c71e”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_local\_hostname”: “[ip-10-196-145-104.aws.foreseeresults.com](http://ip-10-196-145-104.aws.foreseeresults.com)”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_local\_ipv4s”: “10.196.145.104”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_mac”: “0a:16:b5:46:d4:a1”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_owner\_id”: “325603188418”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_security\_group\_ids”: “sg-7c538d04”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_security\_groups”: “fsr-allow-all”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_subnet\_id”: “subnet-4aa02e3c”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_subnet\_ipv4\_cidr\_block”: “10.196.128.0/19”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_vpc\_id”: “vpc-debf9bba”, “ansible\_ec2\_network\_interfaces\_macs\_0a\_16\_b5\_46\_d4\_a1\_vpc\_ipv4\_cidr\_block”: “10.196.0.0/16”, “ansible\_ec2\_placement\_availability\_zone”: “us-east-1a”, “ansible\_ec2\_placement\_region”: “us-east-1”, “ansible\_ec2\_product\_codes”: “aw0evgkw8e5c1q413zgy5pjce”, “ansible\_ec2\_profile”: “default-hvm”, “ansible\_ec2\_public\_key”: “ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGUJO+rS5fIh3bGcKrQzt66hywpxcVQMhz7rb4WyeMtii2gZ3PHb2mnxrTaLE5Q7g6tL8aBfluzASeP0dijWtSDM7YGD1TR6sYEHysxcHRd7dJLhoEsJzwwa8HgRnus66FKnCNtV9XahG/BdAqqDpcWtkmJfkegmzgJ4rjP46EKGW4CZ3JF1/sjVgHAxsU7tv6WWoaCv9+zqKTwWGlBbDHRQxrTmHzWsQlmrpBdr2OnUPSsGMmOcdFX89T4+0T8BQt8nJusFeVV2Nj0jGRFs6fy/a9+wOKLYYLer80Z5RP3NhKVxIEZWfZDN93McBaKRKaQJoj1VnZi0O8yW6P+0rR FSR\_DEV\_QA\_KEYPAIR\n”, “ansible\_ec2\_reservation\_id”: “r-f0f66425”, “ansible\_ec2\_security\_groups”: “fsr-allow-all”, “ansible\_ec2\_services\_domain”: “[amazonaws.com](http://amazonaws.com)”, “ansible\_ec2\_services\_partition”: “aws”, “ansible\_ec2\_user\_data”: null}, “changed”: false}

TASK [ansible-hostname-master : get resource tags] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:27:07 -0500 (0:00:01.044) 0:02:25.194 \*\*\*\*\*\*\*\*\*\*\*  
ok: [10.196.145.104 → localhost] =\> {“changed”: false, “tags”: {“Env”: “Dev”, “Name”: “aaa-fsr-ans-test1”}}

TASK [ansible-hostname-master : Keep temporary old hostname in /etc/hosts] \*\*\*\*\*  
Tuesday 24 May 2016 13:27:07 -0500 (0:00:00.423) 0:02:25.617 \*\*\*\*\*\*\*\*\*\*\*  
ok: [10.196.145.104] =\> {“backup”: “”, “changed”: false, “msg”: “”}

TASK [ansible-hostname-master : Set hostname] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:27:08 -0500 (0:00:00.826) 0:02:26.444 \*\*\*\*\*\*\*\*\*\*\*  
changed: [10.196.145.104] =\> {“ansible\_facts”: {“ansible\_domain”: “”, “ansible\_fqdn”: “aaa-fsr-ans-test1”, “ansible\_hostname”: “aaa-fsr-ans-test1”, “ansible\_nodename”: “aaa-fsr-ans-test1”}, “changed”: true, “name”: “aaa-fsr-ans-test1”}

TASK [ansible-hostname-master : Update /etc/hosts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Tuesday 24 May 2016 13:27:10 -0500 (0:00:02.190) 0:02:28.634 \*\*\*\*\*\*\*\*\*\*\*  
changed: [10.196.145.104] =\> {“backup”: “”, “changed”: true, “msg”: “line added”}

TASK [ansible-hostname-master : add the preserve\_hostname true to /etc/cloud/cloud.cfg] \*\*\*  
Tuesday 24 May 2016 13:27:11 -0500 (0:00:00.837) 0:02:29.471 \*\*\*\*\*\*\*\*\*\*\*  
changed: [10.196.145.104] =\> {“backup”: “”, “changed”: true, “msg”: “line added”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
10.196.145.104 : ok=7 changed=3 unreachable=0 failed=0  
aaa-frs-ans-test1 : ok=5 changed=2 unreachable=0 failed=0  
aaa-frs-ans-test2 : ok=3 changed=1 unreachable=0 failed=0  
aaa-frs-ans-test3 : ok=3 changed=1 unreachable=0 failed=0

Tuesday 24 May 2016 13:27:12 -0500 (0:00:00.774) 0:02:30.246 \*\*\*\*\*\*\*\*\*\*\*

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 24, 2016, 6:50pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/5 "2016-05-24T18:50:37Z")

</div>

I’m not quite positive what you are doing. You omitted the task that creates the instances. However it looks like you are targeting 3 hosts:

aaa-frs-ans-test1

aaa-frs-ans-test2

aaa-frs-ans-test3

And you are creating 1 instance for each host.

add\_host only runs on the first host in the group, so you see it only run for aaa-frs-ans-test1. It is called a host loop bypass plugin, meaning that it doesn’t execute for each host you target in your play.

You are trying to loop ec2.instances, but there is only 1 host in that result, since you created a host on each instance, since vars are scoped to hosts.

Instead of targeting those 3 hosts, maybe you should instead target just localhost, and build your ec2 instances using a with\_items to create 3 hosts.

But again, still not really sure what you are doing, and if you have reasons for what you have done so far.

---

<div class="post-metadata">

**Author:** ![Eric\_S1](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@Eric\_S1](https://forum.ansible.com/u/Eric_S1)\
**Post date:** [May 24, 2016, 7:07pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/6 "2016-05-24T19:07:52Z")

</div>

Here’s the create task:

- hosts: ec2\_instances  
4 connection: local  
5 gather\_facts: true  
6 tasks:  
7 - name: Launch Instance  
8 ec2:  
9 group\_id: “{{ hostvars[inventory\_hostname].group\_id }}”  
10 instance\_type: ‘t2.micro’  
11 image: “{{ hostvars[inventory\_hostname].image }}”  
12 wait: true  
13 region: ‘us-east-1’  
14 keypair: “{{ key\_pair }}”  
15 vpc\_subnet\_id: “{{ subnet }}”  
16 instance\_tags: “{{ hostvars[inventory\_hostname].tags }}”  
17 register: ec2\_info

but why does that module not loop through ec2.instances like the others do? Also there are examples online that do i that way of using the add\_hosts mod with {var}.instances.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 24, 2016, 8:14pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/7 "2016-05-24T20:14:24Z")

</div>

That seems to be contradicting each other. Or is this (the local  
connection) something special use for ec2-stuff?

Johannes

---

<div class="post-metadata">

**Author:** ![Eric\_S1](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@Eric\_S1](https://forum.ansible.com/u/Eric_S1)\
**Post date:** [May 24, 2016, 8:35pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/8 "2016-05-24T20:35:50Z")

</div>

Yes when launching ec2 instances you use the local connection. They all launch fine and are able to be picked up by the wait for ssh task:

- name: Wait for SSH  
wait\_for:  
host: “{{ item.private\_ip }}”  
port: 22  
delay: 10  
timeout: 320  
state: started  
with\_items: “{{ ec2\_info.instances }}”

just not the add\_host task.

---

<div class="post-metadata">

**Author:** ![Eric\_S1](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@Eric\_S1](https://forum.ansible.com/u/Eric_S1)\
**Post date:** [May 27, 2016, 4:42pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/9 "2016-05-27T16:42:14Z")

</div>

is it an issue with the add\_host module? I even tried upgrading to 2.1 and still am experiencing the same thing. Can the add\_host module only add one host at a time?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 27, 2016, 4:56pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/10 "2016-05-27T16:56:03Z")

</div>

No, I tried to explain this in a previous response.

add\_host is what is called a “bypass host loop” plugin. This means that add\_host will only execute for the first host targeted by your `hosts` specification. Your `hosts` targets a group containing the following hosts:

aaa-frs-ans-test1

aaa-frs-ans-test2

aaa-frs-ans-test3

As such, add\_host only runs for aaa-frs-ans-test1.

The problem is with how you have structured your tasks. You have instructed ansible to build a new server, for each one of these servers. The results of your `ec2` variable that you register, get stored per host (aaa-frs-ans-test1, aaa-frs-ans-test2, aaa-frs-ans-test3). This means that `ec2.instances` only hold the results of a single host build, per host.

Most people only target localhost, instead of a group that contains multiple hosts.

And then they build servers with the ec2 module using exact\_count, or with\_items, then all of the results for building all new instances, are stored with 1 inventory host.

So again, you are building 3 new instances, using 3 old instances, so effectively you end up with an `ec2` variable stored for each host (aaa-frs-ans-test1, aaa-frs-ans-test2, aaa-frs-ans-test3).

However add\_host only runs on the first host (bypass host loop plugin), and thus only has access to the `ec2` var stored for aaa-frs-ans-test1.

Another solution would be to do something like:

- name: Add hosts group temporary inventory group  
add\_host: name={{ hostvars[item][‘ec2’].instances.0.private\_ip }} groups=dynamic  
with\_items: “{{ play\_hosts }}”

However, that assumes you always have only created 1 instance via the ec2 task per host in the play.

Another option, and this is completely untested, is to use the `extract` filter, something like:

- name: Add hosts group temporary inventory group  
add\_host: name={{ item.private\_ip }} groups=dynamic  
with\_items: “{{ play\_hosts|map(‘extract’, hostvars, [‘ec2’, ‘instances’])|list }}”

---

<div class="post-metadata">

**Author:** ![Eric\_S1](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@Eric\_S1](https://forum.ansible.com/u/Eric_S1)\
**Post date:** [May 27, 2016, 5:49pm UTC](https://forum.ansible.com/t/add-host-module-not-adding-multiple-hosts/21526/11 "2016-05-27T17:49:09Z")

</div>

Thanks for the deeper explanation, I get it now.

When you say “Most people only target localhost, instead of a group that contains multiple hosts.” do you mean when I’m applying a role? Because in the same playbook s the ec2 instance(s) creation i’m apply a role(s) to the newly created instances. So the whole playbook is:

- hosts: ec2\_test  
connection: local  
gather\_facts: true  
tasks:

- name: Launch Instance  
ec2:  
group\_id: “{{ groupID }}”  
instance\_type: ‘t2.micro’  
image: “{{ image }}”  
wait: true  
region: “{{ region }}”  
keypair: “{{ keypair }}”  
vpc\_subnet\_id: “{{ subnet }}”  
instance\_tags:  
Name: “{{ inventory\_hostname }}”  
ENV: “{{ tagEnv }}”  
register: ec2

- name: Add hosts to group  
add\_host: name=“{{ hostvars[item][‘ec2’].instances.0.private\_ip }}” groups=dynamic  
with\_items: “{{ play\_hosts }}”

- name: Wait for SSH  
wait\_for:  
host: “{{ item.private\_ip }}”  
port: 22  
delay: 10  
timeout: 320  
state: started  
with\_items: “{{ ec2.instances }}”

- name: Wait a little longer for centos  
pause: seconds=20

- hosts: dynamic  
gather\_facts: yes  
sudo: yes  
roles:

- hostname
