update /etc/hosts file

47 views
Skip to first unread message

Diego Rucci

unread,
Feb 18, 2015, 9:46:57 AM2/18/15
to ansible...@googlegroups.com
Hi, I'm just starting with ansible.
I'm running a proxmox server with 12 containers.
I made some playbooks that actually works (like setting tzdata and apt-update)

I was trying to automate etc/hosts file update. I'm using this playbook, taken from here:
---
- hosts: wordpress
  gather_facts
: True
  tasks
:
 
- name: update hosts
    lineinfile
: dest=/etc/hosts1 regexp='.*{{ item }}$' line="{{ hostvars[item].
ansible_default_ipv4.address }} {{item}}.mydomain {{item}} "
state=present
   
when: hostvars[item].ansible_default_ipv4.address is defined
    with_items
: groups['all']
Note I'm using etc/hosts1 file and just 1 host for testing! When I get running will change to correct values.

This works, but duplicate lines every time I run playbook, like this:
192.168.0.30 wordpress.mydomain wordpress
192.168.0.30 wordpress.mydomain wordpress
192.168.0.30 wordpress.mydomain wordpress

Is there something I'm missing?
Thanks

Diego Rucci

unread,
Feb 18, 2015, 12:33:50 PM2/18/15
to ansible...@googlegroups.com
Found it.
It is an extra "space" at lineinfile:
..... {{item}} " state=present
should it be
..... {{item}}" state=present
Reply all
Reply to author
Forward
0 new messages