Search code examples
pythonsalt-project

Using the output of salt to be used as input for an SLS state or pillar?


For example, I want to find all nodes with a certain tag, grab their IP address, then generate a config file to distribute to those nodes.

A use case might be a database that needs to know about every other node, but can have nodes added and removed at any time.


Solution

  • You can use Salt Mine to do it. First, distribute this configuration to all the Minions:

    mine_functions:
      grains.item:
        - roles
        - fqdn_ip4
    

    With that configuration, every Minion will publish those two grains to all other minions. More details about Mine here.

    Now, we can create a file with all the names with role=db.

    /tmp/mydbhosts:
      file:
        - managed
        - source: salt://example/myhosts
        - template: mako
    

    The template example/myhosts:

    % for minion, peer_grains in salt['mine.get']('*', 'grains.items').items():
    % if "db" in peer_grains["role"]:
    minion peer_grains["fqdn_ip4"]
    % endif
    % endfor