Ansible attributes unavailable when using a group filter

125 views
Skip to first unread message

Lee Connell

unread,
Feb 1, 2017, 12:27:27 PM2/1/17
to Ansible Project
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
---
- hosts: api:db:lb:d3:&prd
  remote_user: root
  gather_facts: yes
  tags: [facts]

- hosts: localhost
  connection: local
  gather_facts: no
  tags: [local]
  tasks:
    - 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



### Template

  {% for host in groups['db'] %}
  <tr>
    {{ hostvars[host]['ec2_private_ip_address']}}
    {{ hostvars[host]['ec2_tag_Name']}}

    {{ hostvars[host]['ansible_processor_cores']}}
  </tr>
  {% endfor %}

Lee Connell

unread,
Feb 2, 2017, 1:33:24 PM2/2/17
to Ansible Project
Anybody?

Brian Coca

unread,
Feb 2, 2017, 1:37:23 PM2/2/17
to Ansible Project
Doesn't it work as expected? you avoid gathering facts for machines in
groups['db'] that are not also in 'prd'.


----------
Brian Coca

Lee Connell

unread,
Feb 2, 2017, 6:47:32 PM2/2/17
to ansible...@googlegroups.com
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 received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/6NxLxbNUxwc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7eW_Jqayzjra8YqwdsEphZ%2B7CaX0NHHsFSUA6onwAk_Cg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Feb 2, 2017, 6:57:57 PM2/2/17
to Ansible Project
You are comparing a subset to a whole, 'db:&prd' != groups['db']
unless all machines in 'db' that are in 'prd'.


----------
Brian Coca

Lee Connell

unread,
Feb 2, 2017, 8:17:39 PM2/2/17
to ansible...@googlegroups.com
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

---
- hosts: api 
  gather_facts: yes 
  tags: [facts]

- hosts: localhost
  connection: local
  gather_facts: no
  tags: [local]
  tasks:
    - 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


----------
Brian Coca

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/6NxLxbNUxwc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Brian Coca

unread,
Feb 3, 2017, 10:16:00 AM2/3/17
to Ansible Project
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


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages