0

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 ?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.