Ansible attributes unavailable when using a group filter

Hi all,

I am having an odd issue where if I use a group filter in my playbook, I can no longer access any of the ansible_ facts. I am able to access the ec2_ facts. If I remove my group filter, everything works fine.

Playbook

Anybody?

Doesn't it work as expected? you avoid gathering facts for machines in
groups['db'] that are not also in 'prd'.

Am I misunderstanding something? It works when using an ad-hoc command using the same parameters. Is this a hostvars issue? The groups exist with the correct hosts and thus as mentioned does contain the ec2_ facts. If I execute the same via an ad-hoc command, you see the ansible_ facts show up just fine.

cb_ansible_deploy $ ansible ‘db:&prd’ -m setup | grep ansible_hostname
“ansible_hostname”: “cbdb-e1b-04”,
“ansible_hostname”: “cbdb-e1a-02”,
“ansible_hostname”: “cbdb-e1b-02”,
“ansible_hostname”: “cbdb-e1b-05”,
“ansible_hostname”: “cbdb-e1a-05”,
“ansible_hostname”: “cbdb-e1a-03”,
“ansible_hostname”: “cbdb-e1a-01”,
“ansible_hostname”: “cbdb-e1b-03”,
“ansible_hostname”: “cbdb-e1a-06”,
“ansible_hostname”: “cbdb-e1b-01”,
“ansible_hostname”: “cbdb-e1b-06”,
“ansible_hostname”: “cbdb-e1a-04”,

cb_ansible_deploy $ ansible-playbook wiki.yml -l ‘db:&prd’ --list-hosts

playbook: wiki.yml

play #1 (api:db:lb:d3:&prd): api:db:lb:d3:&prd TAGS: [facts]
pattern: [u’api:db:lb:d3:&prd’]
hosts (12):
cbdb_e1a_01_cb_comcast_net
cbdb_e1a_06_cb_comcast_net
cbdb_e1b_05_cb_comcast_net
cbdb_e1b_03_cb_comcast_net
cbdb_e1b_06_cb_comcast_net
cbdb_e1a_05_cb_comcast_net
cbdb_e1b_02_cb_comcast_net
cbdb_e1a_04_cb_comcast_net
cbdb_e1a_02_cb_comcast_net
cbdb_e1b_01_cb_comcast_net
cbdb_e1b_04_cb_comcast_net
cbdb_e1a_03_cb_comcast_net

play #2 (localhost): localhost TAGS: [local]
pattern: [u’localhost’]
hosts (0):

play #3 (adm): adm TAGS: [adm]
pattern: [u’adm’]
hosts (0):

cb_ansible_deploy $ ansible-playbook wiki.yml

PLAY [db:&prd] *****************************************************************

TASK [setup] *******************************************************************

ok: [cbdb_e1a_02]

ok: [cbdb_e1a_04]

ok: [cbdb_e1a_06]

ok: [cbdb_e1a_05]

ok: [cbdb_e1b_04]

ok: [cbdb_e1b_03]

ok: [cbdb_e1b_05]

ok: [cbdb_e1b_01]

ok: [cbdb_e1a_03]

ok: [cbdb_e1a_01]

ok: [cbdb_e1b_02]

ok: [cbdb_e1b_06]

PLAY [localhost] ***************************************************************

TASK [store date time] *********************************************************

ok: [127.0.0.1]

TASK [build wiki] **************************************************************

fatal: [127.0.0.1]: FAILED! => {“changed”: false, “failed”: true, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘ansible_hostname’”}

You are comparing a subset to a whole, 'db:&prd' != groups['db']
unless all machines in 'db' that are in 'prd'.

Ok, how do I accomplish what I am trying to do? I would like to be able to run my playbook on specific groups of hosts that are either in production or in staging depending on what is specified on the command line using limit. Right now I have all production and staging in say an api group. I then have a production and staging group that only have their corresponding hosts assigned.

[prd:children]
tag_Env_Production

[stg:children]
tag_Env_Staging

[api:children]
tag_Role_API

This will gather form api hosts always no matter how you limit the
hosts, the drawback is that the loop is sequential.
- hosts: localhost
  connection: local
  gather_facts: no
  tags: [local]
  tasks:
    - name: gather api facts
      setup:
      delegate_to: "{{item}}"
      delegate_facts: True
      with_inventory_hostname: api

    - name: store date time
      set_fact: date_time="{{lookup('pipe','date')}}"
    - name: build wiki
      template: src=roles/wiki/templates/wiki.test.j2
dest=roles/wiki/files/wiki.html