saltstack dynamically update etc hosts

1,100 views
Skip to first unread message

pankaj ghadge

unread,
Jul 3, 2014, 9:40:39 AM7/3/14
to salt-...@googlegroups.com
How to dynamically update /etc/hosts file with saltstack?

There is example that works with ansible great, but don't know how to do it with salt.


      - 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']

This will update /etc/hosts with all ansible hosts-ip and host address available in inventory file.

How it is possible with salt? 
I want to collect all minions ip address and hostname and update it on all minions /etc/hosts. 

    minion1 => ip (192.168.1.1) hostname is (example1.net)
    minion2 => ip (192.168.1.2) hostname is (example2.net)
    minion3 => ip (192.168.1.3) hostname is (example3.net)

In all minions /etc/hosts file entry should be like:

    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

 
Thanks in advance.

viq

unread,
Jul 3, 2014, 10:59:37 AM7/3/14
to salt-...@googlegroups.com
There appears to be a formula for this:
https://github.com/saltstack-formulas/hostsfile-formula

--
viq

pankaj ghadge

unread,
Jul 7, 2014, 8:22:22 AM7/7/14
to salt-...@googlegroups.com
But the above mentioned formula is not working for me.
But it also seems not working. 

How do I debug values from it like: 
1) {%- set addrs = salt['mine.get']('*', 'network.ip_addrs') %}  <====== How do I print it to see which values assigned to array addrs
2) {%- set if = grains['maintain_hostsfile_interface'] %}         <====== I don't understand what is this for. How do I print this value.

Valentin Bud

unread,
Jul 7, 2014, 8:53:51 AM7/7/14
to salt-...@googlegroups.com
Hello Pankaj,


On Mon, Jul 7, 2014 at 3:22 PM, pankaj ghadge <ghadge...@gmail.com> wrote:
But the above mentioned formula is not working for me.
But it also seems not working. 

How do I debug values from it like: 
1) {%- set addrs = salt['mine.get']('*', 'network.ip_addrs') %}  <====== How do I print it to see which values assigned to array addrs

The first thing after a set becomes a Jinja variable. You can print it's contents like:

{{ addrs }}
 
2) {%- set if = grains['maintain_hostsfile_interface'] %}         <====== I don't understand what is this for. How do I print this value.

I think it sets the `if' variable to the value of the `maintain_hostsfile_interface' 
grain from the minion the state runs on.

Who sets that grain? A custom module, a state, manually? Even better do
you have it setup in your infrastructure?

$ sudo salt '*' grains.item maintain_hostsfile_interface

If not, I would start from there and work my way up to the top :).

Best,
Valentin
 





On Thursday, 3 July 2014 20:29:37 UTC+5:30, vic viq wrote:
There appears to be a formula for this:
https://github.com/saltstack-formulas/hostsfile-formula

--
viq

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

pankaj ghadge

unread,
Jul 7, 2014, 9:35:21 AM7/7/14
to salt-...@googlegroups.com
Thanks for the reply, 

{%- set addrs = salt['mine.get']('*', 'network.ip_addrs') %} 
{{ addrs }} 

This does not print anything. 

{%- set if = grains['maintain_hostsfile_interface'] %} 
"if" variable is being set here, then why it is not used?  (https://github.com/saltstack-formulas/hostsfile-formula) 
 
Here i'm trying to print value of usr:
{% for usr in ['moe','larry','curly'] %}
{{ usr }}
{% endfor %}

It gives error like: "ValueError: need more than 1 value to unpack" 

JC Lawrence

unread,
Jul 7, 2014, 11:41:41 AM7/7/14
to salt-...@googlegroups.com
On 7 Jul 2014, at 06:35, pankaj ghadge <ghadge...@gmail.com> wrote:

Thanks for the reply, 

{%- set addrs = salt['mine.get']('*', 'network.ip_addrs') %} 
{{ addrs }} 

This does not print anything. 

Have you setup the Salt Mine for your network interfaces?

http://docs.saltstack.com/en/latest/topics/mine/

-- JCL

pankaj ghadge

unread,
Jul 8, 2014, 2:28:54 AM7/8/14
to salt-...@googlegroups.com
Hello Lawrence,

It works like I wanted.
Thank you so very much for responding to my question. You really helped me out a lot.

Reply all
Reply to author
Forward
0 new messages