vBlog

vBlog

Useful tips and snippets

31 Dec 2016

Using Python in SaltStack reactor

Sometimes you need to do some complex actions in a Salt Reactor in which case Python comes in handy.

The documentation is not very clear on how to write Python states. The main point is that Salt expects a structure that can be compiled to a Python dictionary. The run() function has to return a dictionary which resembles the state you would write with YAML.

Here’s an example to add a domain to route 53:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!py

def run():
   '''Add domain name to route53'''

   full_name = data['name']

   subzone, name = full_name.split('.')
   zone = "some.zone.tld"

   try:
       aws_query = __salt__['cloud.full_query']()['aws']['ec2'][full_name]['dnsName']
   except KeyError:
       dns_setup_hsd = {'setup-dns':
           {'local.test.nop': [
               {'tgt': 'some.minion.id'},
               ]}}
   else:
       dns_setup_hsd = {'setup-dns':
           {'local.boto_route53.add_record': [
               {'tgt': 'some.minion.id'},
               {'arg': [
                   '.'.join((name, zone)),
                   aws_query,
                   zone,
                   'CNAME']}]}}

   return dns_setup_hsd

Tags