Reputation: 7058
I am trying to create a hosts file with following task. I am running this task on machines completely out of groups['hadoop']. Here I want to create hosts file for nodes from groups['hadoop'] without running my this play on groups['hadoop']
- name: Update /etc/hosts
lineinfile: dest=/etc/hosts
regexp='.*{{ item }}$'
line="{{ hostvars[item].ansible_default_ipv4.address }} {{item}}"
state=present
when: hostvars[item].ansible_default_ipv4.address is defined
with_items: groups['hadoop']
tags:
- etc-hosts
I tried this but it didn't work, it worked only when I have groups['hadoop'] in my play hosts.
Anyone know what I am doing wrong here ?
Upvotes: 0
Views: 1214
Reputation: 60029
Ansible will detect facts like the IP through the setup module. This is by default executed as very first task of a play on those hosts which will be processed by the play. Therefore Ansible only knows facts about the hosts of the current play. Not part of the play -> no details available.
To solve this you have two options.
1) Add a play without tasks, just to run the setup module on the hadoop hosts.
---
- hosts: hadoop
gather_facts: yes
- hosts: other hosts
tasks: your actual tasks here
...
2) Enable fact caching. Fact caching, as the name suggests, provides a way for Ansible to memorize facts about hosts it did not process in the current playbook. For this you will need to set up a redis instance locally or somewhere available on the network.
Upvotes: 1