Ansible – Dynamicaly update /etc/hosts files on target servers

I was configuring GlusterFS on few servers using Ansible and have a need to update /etc/hosts with hostnames for easier configuration. I found this one working:

- name: Update /etc/hosts
  lineinfile: dest=/etc/hosts regexp='.*{{item}}$' line='{{hostvars.{{item}}.ansible_default_ipv4.address}} {{item}}' state=present
  with_items: '{{groups.somegroup}}'

Update – that doesn’t work in Ansible version 2.x, you have to use this:

- name: Update /etc/hosts from inventory
  lineinfile: dest=/etc/hosts regexp='.*{{item}}$' line='{{hostvars[item].ansible_default_ipv4.address}} {{item}}' state=present
  with_items: '{{groups.all}}'
    - hosts


One thought on “Ansible – Dynamicaly update /etc/hosts files on target servers”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.