Reputation: 243
I am using Salt to configure a number of servers. Two of them are assigned the role of a DNS server and configured using the dnsmasq-formula
. Others have various roles, such as gitlab-server
, which are defined in the pillar.
Here's how this is configured:
/srv/salt/top.sls
---
base:
'roles:dns-server':
- match: pillar
- dnsmasq
'roles:gitlab-server'
- gitlab
/srv/pillar/top.sls
---
{% import_yaml "roles.yaml" as r %}
base:
{% if 'dns-server' in r[grains['id']] %}
- dns.dnsserv
{% endif %}
/srv/pillar/dns/dnsserv.sls
---
dnsmasq:
...
hosts:
domain.name:
git: <GIT_SERVER_IP>
...
/srv/pillar/roles.yaml
---
'minion1-id':
- role1
- role2
'minion2-id':
- role3
- role4
# and so on
Right now the <GIT_SERVER_IP>
is hardcoded. I would rather like to query the minions, find the one with the right role and extract its IP address programatically.
When /srv/pillar/dns/dnsserv.sls
is processed, it is processed with the info available to the minion with the role "dns-server". By loading roles.yaml
inside of dnsserv.sls
I can manage to get the minion ID of the gitlab-server (with some very ugly code). But how can I get its IP from this file?
I have seen mentions of the salt mine, but have not really found a complete example how to use this functionality.
This question may well be an example of an XY problem. If so, an example of the "proper way" would be much appreciated.
Upvotes: 0
Views: 1136
Reputation: 883
To pass grains or pillar from one minion to another, you need to use "salt mine" (https://docs.saltstack.com/en/latest/topics/mine/). The topic is not simple but briefly:
mine_functions:
grains:
grains.items: []
I recommend you to first try mine.get on command line to see if you're able to retrieve your data, before starting to write a state.
Upvotes: 1