I am writing a playbook that adds hosts to the inventory and then writes a host inventory file based on the hosts defined. The host added with add_host isn't reflected in the hostvars or groups variable. I would have expected the added host to be added to the group and any parent groups, but it doesn't seem to be the case. If I try to connect to the host in the next play, it will connect, but the "magic" variables don't pick it up. Seen in ansible 1.2.2 and 1.3.2.
---
- name: write hosts
hosts: all
gather_facts: no
tasks:
- local_action: debug msg="{{ hostvars }}"
- local_action: debug msg="{{ groups }}"
- name: Add Host
add_host: name=my_new_vm
groups=skytap_command
ansible_ssh_host=172.24.129.2
ansible_ssh_private_key_file=/tmp/ansible/jenkins_id_rsa
- local_action: debug msg="{{ hostvars }}"
- local_action: debug msg="{{ groups }}"
$ ansible-playbook -vvv -i hosts_local test_group_add.yml
PLAY [write hosts] ************************************************************
TASK: [debug msg="{{hostvars}}"] **********************************************
ok: [localhost] => {"msg": "{'localhost': {}}"}
TASK: [debug msg="{{groups}}"] ************************************************
ok: [localhost] => {"msg": "{'all': ['localhost'], 'skytap': [], 'stage_command': [], 'ungrouped': [], 'prod_command': [], 'command': [], 'skytap_command': [], 'prod': [], 'stack': [], 'localhost': ['localhost'], 'stage': []}"}
TASK: [Add Host] **************************************************************
creating host via 'add_host': hostname=my_new_vm
added host to group via add_host module: skytap_command
ok: [localhost] => {"new_groups": ["skytap_command"], "new_host": "my_new_vm"}
TASK: [debug msg="{{hostvars}}"] **********************************************
ok: [localhost] => {"msg": "{'localhost': {}}"}
TASK: [debug msg="{{groups}}"] ************************************************
ok: [localhost] => {"msg": "{'all': ['localhost'], 'skytap': [], 'stage_command': [], 'ungrouped': [], 'prod_command': [], 'command': [], 'skytap_command': [], 'prod': [], 'stack': [], 'localhost': ['localhost'], 'stage': []}"}
PLAY RECAP ********************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0