add_host module returns 'NoneType' object has no attribute 'add_group'

Issue Type:
Bug Report

Ansible Version:
ansible 1.9.0.1
configured module search path = None

Environment:
CentOS release 6.6 (Final)

Summary:

Using the add_host module fails on the first run. When run a second time it works.

Steps To Reproduce:

`

  • name: add hosts to fresh group

add_host: >

hostname={{ item.private_ip }}

groups=fresh
with_items: created.tagged_instances
`

Expected Results:

`
TASK: [jaf_aws | add hosts to fresh group] ************************************

creating host via ‘add_host’: hostname=172.20.16.135

added host to group via add_host module: fresh
ok: [localhost]
`

Actual Results:

`
ASK: [jaf_aws | add hosts to fresh group] ************************************
creating host via ‘add_host’: hostname=172.20.16.135
fatal: [localhost] => Traceback (most recent call last):
File “/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/runner/init.py”, line 582, in _executor
exec_rc = self._executor_internal(host, new_stdin)
File “/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/runner/init.py”, line 811, in _executor_internal
complex_args=complex_args
File “/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/runner/init.py”, line 1028, in _executor_internal_inner
result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
File “/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/runner/action_plugins/add_host.py”, line 82, in run
grp.add_host(new_host)
File “/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/inventory/group.py”, line 67, in add_host
host.add_group(self)
AttributeError: ‘NoneType’ object has no attribute ‘add_group’

FATAL: all hosts have already failed – aborting
`

Another example with debug output.

TASK: [cep_aws | set hostnames] ***********************************************
<172.20.16.148> REMOTE_MODULE hostname name="awsqa-cep-primary00"

changed: [localhost -> 172.20.16.148] => (item={'ramdisk': None, 'kernel': 'aki-919dcaf8', 'root_device_type': 'ebs', 'private_dns_name': 'ip-172-20-16-148.ec2.internal', 'tags': {'ansible': 'awsqa_cep_primary', 'group': 'ancillary', 'Name': 'awsqa-cep-primary00', 'env': 'awsqa', 'app': 'cep', 'class': 'private'}, 'key_name': 'aws', 'public_ip': None, 'image_id': 'ami-cda70ca6', 'tenancy': 'default', 'private_ip': '172.20.16.148', 'groups': {'sg-642d9a03': 'sg_to_bind', 'sg-58c64a3f': 'sg_ec2_qa_cep', 'sg-76cf6c11': 'sg_from_nagios', 'sg-a34d95c4': 'sg_from_warp'}, 'public_dns_name': u'', 'state_code': 16, 'id': 'i-b7960465', 'placement': 'us-east-1b', 'ami_launch_index': '0', 'dns_name': u'', 'region': 'us-east-1', 'ebs_optimized': False, 'launch_time': '2015-08-13T23:28:38.000Z', 'instance_type': 'm3.large', 'state': 'running', 'root_device_name': '/dev/sda', 'hypervisor': 'xen', 'virtualization_type': 'paravirtual', 'architecture': 'x86_64'}) => {"changed": true, "item": {"ami_launch_index": "0", "architecture": "x86_64", "dns_name": "", "ebs_optimized": false, "groups": {"sg-58c64a3f": "sg_ec2_qa_cep", "sg-642d9a03": "sg_to_bind", "sg-76cf6c11": "sg_from_nagios", "sg-a34d95c4": "sg_from_warp"}, "hypervisor": "xen", "id": "i-b7960465", "image_id": "ami-cda70ca6", "instance_type": "m3.large", "kernel": "aki-919dcaf8", "key_name": "aws", "launch_time": "2015-08-13T23:28:38.000Z", "placement": "us-east-1b", "private_dns_name": "ip-172-20-16-148.ec2.internal", "private_ip": "172.20.16.148", "public_dns_name": "", "public_ip": null, "ramdisk": null, "region": "us-east-1", "root_device_name": "/dev/sda", "root_device_type": "ebs", "state": "running", "state_code": 16, "tags": {"Name": "awsqa-cep-primary00", "ansible": "awsqa_cep_primary", "app": "cep", "class": "private", "env": "awsqa", "group": "ancillary"}, "tenancy": "default", "virtualization_type": "paravirtual"}, "name": "awsqa-cep-primary00"}

TASK: [cep_aws | debug var=item] **********************************************

ok: [localhost] => (item={'ramdisk': None, 'kernel': 'aki-919dcaf8', 'root_device_type': 'ebs', 'private_dns_name': 'ip-172-20-16-148.ec2.internal', 'tags': {'ansible': 'awsqa_cep_primary', 'group': 'ancillary', 'Name': 'awsqa-cep-primary00', 'env': 'awsqa', 'app': 'cep', 'class': 'private'}, 'key_name': 'aws', 'public_ip': None, 'image_id': 'ami-cda70ca6', 'tenancy': 'default', 'private_ip': '172.20.16.148', 'groups': {'sg-642d9a03': 'sg_to_bind', 'sg-58c64a3f': 'sg_ec2_qa_cep', 'sg-76cf6c11': 'sg_from_nagios', 'sg-a34d95c4': 'sg_from_warp'}, 'public_dns_name': u'', 'state_code': 16, 'id': 'i-b7960465', 'placement': 'us-east-1b', 'ami_launch_index': '0', 'dns_name': u'', 'region': 'us-east-1', 'ebs_optimized': False, 'launch_time': '2015-08-13T23:28:38.000Z', 'instance_type': 'm3.large', 'state': 'running', 'root_device_name': '/dev/sda', 'hypervisor': 'xen', 'virtualization_type': 'paravirtual', 'architecture': 'x86_64'}) => {
    "item": {
        "ami_launch_index": "0",
        "architecture": "x86_64",
        "dns_name": "",
        "ebs_optimized": false,
        "groups": {
            "sg-58c64a3f": "sg_ec2_qa_cep",
            "sg-642d9a03": "sg_to_bind",
            "sg-76cf6c11": "sg_from_nagios",
            "sg-a34d95c4": "sg_from_warp"
        },
        "hypervisor": "xen",
        "id": "i-b7960465",
        "image_id": "ami-cda70ca6",
        "instance_type": "m3.large",
        "kernel": "aki-919dcaf8",
        "key_name": "aws",
        "launch_time": "2015-08-13T23:28:38.000Z",
        "placement": "us-east-1b",
        "private_dns_name": "ip-172-20-16-148.ec2.internal",
        "private_ip": "172.20.16.148",
        "public_dns_name": "",
        "public_ip": null,
        "ramdisk": null,
        "region": "us-east-1",
        "root_device_name": "/dev/sda",
        "root_device_type": "ebs",
        "state": "running",
        "state_code": 16,
        "tags": {
            "Name": "awsqa-cep-primary00",
            "ansible": "awsqa_cep_primary",
            "app": "cep",
            "class": "private",
            "env": "awsqa",
            "group": "ancillary"
        },
        "tenancy": "default",
        "virtualization_type": "paravirtual"
    },
    "var": {
        "item": {
            "ami_launch_index": "0",
            "architecture": "x86_64",
            "dns_name": "",
            "ebs_optimized": false,
            "groups": {
                "sg-58c64a3f": "sg_ec2_qa_cep",
                "sg-642d9a03": "sg_to_bind",
                "sg-76cf6c11": "sg_from_nagios",
                "sg-a34d95c4": "sg_from_warp"
            },
            "hypervisor": "xen",
            "id": "i-b7960465",
            "image_id": "ami-cda70ca6",
            "instance_type": "m3.large",
            "kernel": "aki-919dcaf8",
            "key_name": "aws",
            "launch_time": "2015-08-13T23:28:38.000Z",
            "placement": "us-east-1b",
            "private_dns_name": "ip-172-20-16-148.ec2.internal",
            "private_ip": "172.20.16.148",
            "public_dns_name": "",
            "public_ip": null,
            "ramdisk": null,
            "region": "us-east-1",
            "root_device_name": "/dev/sda",
            "root_device_type": "ebs",
            "state": "running",
            "state_code": 16,
            "tags": {
                "Name": "awsqa-cep-primary00",
                "ansible": "awsqa_cep_primary",
                "app": "cep",
                "class": "private",
                "env": "awsqa",
                "group": "ancillary"
            },
            "tenancy": "default",
            "virtualization_type": "paravirtual"
        }
    }
}

TASK: [cep_aws | add hosts to fresh group] ************************************
creating host via 'add_host': hostname=172.20.16.148
fatal: [localhost] => Traceback (most recent call last):
  File "/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/runner/__init__.py", line 582, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/runner/__init__.py", line 811, in _executor_internal
    complex_args=complex_args
  File "/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/runner/__init__.py", line 1028, in _executor_internal_inner
    result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
  File "/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/runner/action_plugins/add_host.py", line 82, in run
    grp.add_host(new_host)
  File "/opt/wp/virtualenv/hegemon/lib/python2.7/site-packages/ansible/inventory/group.py", line 67, in add_host
    host.add_group(self)
AttributeError: 'NoneType' object has no attribute 'add_group'

 

FATAL: all hosts have already failed – aborting

You didn’t specify this in your description, but from your naming it is clear you’re using add_host after provisioning new aws nodes. I don’t believe this is an Ansible issue but an AWS issue. After creating the node, it’s takes a little while for the api to respond that it exists (my guess any way). You might be able to pause if anything in your ec2 task changes, to give aws a minute to register the new vm. See

http://docs.ansible.com/ansible/pause_module.html

The wait_for and/or pause modules can help you with this.

I’d love to use the wait_for module to ensure the tags exist before moving to the next step but I don’t see a way to run a command like:

aws ec2 describe-instances
–filter
“Name=tag:ansible,Values=awsqa_helios_web”
“Name=instance-state-code,Values=16”
–query “Reservations.Instances.Tags

and wait for the module to register the expected output before moving on.

Thanks for the help guys.

Nevermind this should work.

http://docs.ansible.com/ansible/playbooks_loops.html#do-until-loops

Thanks for the suggestion Brian and senorsmile but I realized after creating a task that ensures the tags exist before running the add_host module I don’t think this has anything to do with the api.

The add_host task is just adding the IP address of the instance to a group

TASK: [cep_aws | add hosts to fresh group] ************************************
creating host via 'add_host': hostname=172.20.16.148

but it bombs out

AttributeError: 'NoneType' object has no attribute 'add_group'

Any ideas what could be causing this?

Cheers