Having troubles accessing hostvars for new host

402 views
Skip to first unread message

Barry Kaplan

unread,
Jun 17, 2015, 6:34:33 AM6/17/15
to ansible...@googlegroups.com
I have some tasks like:

    - debug: var=hostvars[item]
      with_items
: groups.rabbitmq_server


   
- name: Update /etc/hosts with rabbitmq-server hostnames
      lineinfile
:
        dest
: /etc/hosts
        regexp
: "^{{hostvars[item]['ansible_default_ipv4']['address']}} {{hostvars[item]['ansible_hostname']}}$"
        line
: "{{hostvars[item]['ansible_default_ipv4']['address']}} {{hostvars[item]['ansible_hostname']}}"
        state
: present
      with_items
: groups.rabbitmq_server


When the second runs I get:

TASK: [Update /etc/hosts with rabbitmq-server hostnames] ********************** 
fatal: [10.0.136.70] => One or more undefined variables: 'dict object' has no attribute 'ansible_default_ipv4'
fatal: [10.0.137.195] => One or more undefined variables: 'dict object' has no attribute 'ansible_default_ipv4'

But just in the debug above I have:

            "ansible_default_ipv4": {
               
"address": "10.0.137.195",
               
"alias": "eth0",
               
"gateway": "10.0.137.1",
               
"interface": "eth0",
               
"macaddress": "0e:ab:ba:38:c4:a1",
               
"mtu": 9001,
               
"netmask": "255.255.255.0",
               
"network": "10.0.137.0",
               
"type": "ether"
           
},


( I used to reference the hostvars like hostvars[item].ansible_default_ipv4.address but tried using [] just out of frustration. )

Barry Kaplan

unread,
Jun 17, 2015, 6:35:30 AM6/17/15
to ansible...@googlegroups.com
Oh, I should have mentioned, this only happens when I have created the ec2 instances in the same play run. On a second run I don't ever seem to get this error.

And this is on 1.9.2

Brian Coca

unread,
Jun 18, 2015, 12:08:08 AM6/18/15
to ansible...@googlegroups.com
did you gather facts after creating the instances?


--
Brian Coca

Barry Kaplan

unread,
Jun 18, 2015, 12:22:58 AM6/18/15
to ansible...@googlegroups.com
Yes, the play explicitly has gather_facts:true. The debug just above the failure clearly shows the attribute is present, that's what is confusing.

Brian Coca

unread,
Jun 18, 2015, 1:29:37 AM6/18/15
to ansible...@googlegroups.com
gather_facts happens before other tasks, my question was if you
gathered facts AFTER creating the new hosts


--
Brian Coca

Barry Kaplan

unread,
Jun 18, 2015, 12:30:23 PM6/18/15
to ansible...@googlegroups.com
The flow is:

provision.yml:
------------------
- name: Provision rabbitmq cluster
  hosts
: localhost
  connection
: local
  gather_facts
: yes
  tags
: [provision, rabbitmq_cluster]

  tasks
:
   
- name: Create rabbitmq-server instance
      ec2
:
       
...

- include: ../../configure/rabbitmq-server.yml
 
tags: [configure, rabbitmq_cluster]



rabbitmq-server.yml
---------------------------
- name: Configure rabbitmq-server (install and configure)
  hosts
: rabbitmq_server
  sudo
: yes
  gather_facts
: true
  tags
: [rabbitmq_server]

  pre_tasks
:

Brian Coca

unread,
Jun 18, 2015, 12:49:29 PM6/18/15
to ansible...@googlegroups.com
So the new hosts don't get added to the rabbitmq_server group?

I assume this will happen automatically next time the inventory script
runs (if tagged), but that won't happen until you run ansible again,
normally you use add_hosts to temporarily add them for the 'current
run'.


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