Issue 1:
ISSUE TYPE

Bug Report

COMPONENT NAME

add_host module

ANSIBLE VERSION

1.9.0.1

CONFIGURATION

OS / ENVIRONMENT

SUMMARY

Trying to use the add_host module to add fresh servers to a group for tasks later in the playbook I keep getting this error. It will fail the first run but during the second run will work fine. I've included the two previous tasks to show how the with_items works.

My tasks:

- name: set hostnames
hostname: >
name="{{ item.tags.Name }}"
sudo: yes
delegate_to: "{{ item.private_ip }}"
with_items: created.tagged_instances

- debug: var=item
with_items: created.tagged_instances

- name: add hosts to fresh group
add_host:
hostname="{{ item.private_ip }}"
group=fresh
with_items: created.tagged_instances

The errors.

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

STEPS TO REPRODUCE

EXPECTED RESULTS

ACTUAL RESULTS

Issue 2:
ISSUE TYPE

Bug Report

COMPONENT NAME

delegate_to

ANSIBLE VERSION

ansible 2.4.2.0
config file = /Users/tagir/.config/ansible/ansible.cfg
configured module search path = [u'/Users/tagir/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/Cellar/ansible/2.4.2.0_1/libexec/lib/python2.7/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 2.7.14 (default, Sep 25 2017, 09:53:22) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]

CONFIGURATION

ANSIBLE_PIPELINING(/Users/tagir/.config/ansible/ansible.cfg) = True
CACHE_PLUGIN(/Users/tagir/.config/ansible/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/Users/tagir/.config/ansible/ansible.cfg) = .
CACHE_PLUGIN_TIMEOUT(/Users/tagir/.config/ansible/ansible.cfg) = 60
DEFAULT_BECOME_METHOD(/Users/tagir/.config/ansible/ansible.cfg) = su
DEFAULT_GATHERING(/Users/tagir/.config/ansible/ansible.cfg) = smart
DEFAULT_JINJA2_EXTENSIONS(/Users/tagir/.config/ansible/ansible.cfg) = jinja2.ext.do
DEFAULT_MODULE_NAME(/Users/tagir/.config/ansible/ansible.cfg) = shell
DEFAULT_ROLES_PATH(/Users/tagir/.config/ansible/ansible.cfg) = [u'/Users/tagir/git/ansible/r oles']
DEFAULT_STDOUT_CALLBACK(/Users/tagir/.config/ansible/ansible.cfg) = debug
RETRY_FILES_ENABLED(/Users/tagir/.config/ansible/ansible.cfg) = False

OS / ENVIRONMENT

N/A

SUMMARY

In my environment ansible_become_pass is defined on a per-host basis. This does not play well with the delegated tasks, because instead of using the password of the host the task is delegated to, the password of the delegating host is used.

STEPS TO REPRODUCE

Sample inventory file:

---
all:
hosts:
ns1:
ansible_become_pass: ns1
ns2:
ansible_become_pass: ns2
ns3:
ansible_become_pass: ns3

Sample playbook:

---
- hosts: all
tasks:
- debug: msg="{{ ansible_become_pass }}"
delegate_to: ns1

EXPECTED RESULTS

ansible_become_pass to be ns1 for every run of the delegated task.

ACTUAL RESULTS

The value of the ansible_become_pass is taken from the variables of the respective host.

$ ansible-playbook -i inventory.yml playbook.yml

PLAY [all] **********************************************************************************

TASK [debug] ********************************************************************************
ok: [ns1 -> None] => {}

MSG:

ns1

ok: [ns2 -> None] => {}

MSG:

ns2

ok: [ns3 -> None] => {}

MSG:

ns3

PLAY RECAP **********************************************************************************
ns1 : ok=1 changed=0 unreachable=0 failed=0
ns2 : ok=1 changed=0 unreachable=0 failed=0
ns3 : ok=1 changed=0 unreachable=0 failed=0