9

I am trying to use Ansible to add entries in the server hosts file. I have a group of servers that I need to talk to each other over a private LAN.

My inventory file:

[server_list]
server1
server2

The task I am trying to get working:

- lineinfile: dest=/etc/hosts line="{{ hostvars[" {{ item }} "]['ansible_eth1']['ipv4']['address'] }}   {{ hostvars[" {{ item }} "]['ansible_hostname'] }}"
  with_items: groups['server_list']

It's not doing the trick, I get this:

fatal: [server1] => host not found:  {{item}} 
fatal: [server2] => host not found:  {{item}} 

This is basically the same as this, but in the new Ansible variable access format {{ }}.

Any ideas how to get this done?

techraf
  • 4,243
  • 8
  • 29
  • 44
melsayed
  • 1,122
  • 1
  • 6
  • 12

4 Answers4

11

OK. I had tried this before and it didn't really work. So I must have done something wrong back there.

This works:

- lineinfile: dest=/etc/hosts line="{{ hostvars[item]['ansible_eth1']['ipv4']['address'] }}   {{ hostvars[item]['ansible_hostname'] }}"
  with_items: groups['server_list']

or for 1.9 or later:

- lineinfile: dest=/etc/hosts line="{{ hostvars[item]['ansible_eth1']['ipv4']['address'] }}   {{ hostvars[item]['ansible_hostname'] }}"
  with_items: "{{ groups['server_list'] }}"
melsayed
  • 1,122
  • 1
  • 6
  • 12
7

Had the same problem, I wanted to go over the list of hosts in one group and then add firewall rules with their ips. Looked a bit at how hostvars[item] is structured and used the different name to access this value. This worked for me:

- name: Setting up firewall so web_servers can access MySQL on port {{ mysql_port }}
  ufw: rule=allow proto=tcp to_port={{ mysql_port }} src="{{ hostvars[item]['ansible_default_ipv4']['address'] }}"
  with_items: "{{ groups.web_servers }}"
Athropos
  • 81
  • 1
  • 5
1

if you need to both add a prefix and a suffix, as well as making everything a list, take a look at the below:

  set_fact:
    extended_etcd_endpoints_list: "{{ groups['etcd'] | map('extract', hostvars, ['ansible_default_ipv4','address']) | map('regex_replace', '^(.*)$','https://\\1:2379') | list  }}"

it takes the list of all machines in the group etcd, extracts the ipv4, adds an 'https://' in front and an ':2379' at the end. Finally, everything is transformed in a list.

0

Your syntax is only slightly off. Try using groups.server_list as your with_items list.

I don't use host_vars quite like you do, so let me know if you get a parser error working that portion of your playbook.

But the following should put a hostname in place of {{ item }}.

- lineinfile: dest=/etc/hosts line="{{ hostvars[" {{ item }} "]['ansible_eth1']['ipv4']['address'] }}   {{ hostvars[" {{ item }} "]['ansible_hostname'] }}"
  with_items: groups.server_list
Christopher Karel
  • 6,582
  • 1
  • 28
  • 34
  • 2
    Nop. Still broke. `groups['server_list']` is expanded correctly. I think the problem is the nesting. {{ }} around `item` inside {{ }} for hostvars. – melsayed Jul 26 '14 at 01:33