title: saltstack dynamically update etc hosts
body: <p>How to dynamically update /etc/hosts file with saltstack.</p>
  
  <p>There is example that works with ansible great, but don't know how to do it with salt.</p>
  
  <p><a href="http://xmeblog.blogspot.fr/2013/06/ansible-dynamicaly-update-etchosts.html" rel="nofollow">http://xmeblog.blogspot.fr/2013/06/ansible-dynamicaly-update-etchosts.html</a></p>
  
  <pre><code>  - name: add hostname in /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['all']
  </code></pre>
  
  <p>This will update /etc/hosts with all ansible hosts-ip and host address available in inventory file.</p>
  
  <p>How it is possible with salt? 
  I want to collect all minions ip address and hostname and update it on all minions /etc/hosts. </p>
  
  <pre><code>minion1 =&gt; ip (192.168.1.1) hostname is (example1.net)
  minion2 =&gt; ip (192.168.1.2) hostname is (example2.net)
  minion3 =&gt; ip (192.168.1.3) hostname is (example3.net)
  </code></pre>
  
  <p>In all minions /etc/hosts file entry should be like:</p>
  
  <pre><code>127.0.0.1 localhost
  ::1 localhost
  192.168.1.1  example1.net
  192.168.1.2  example2.net
  192.168.1.3  example3.net
  </code></pre>