Using Python in SaltStack reactor

Contents

[NOTE] Updated December 31, 2016. This article may have outdated content or subject matter.

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.

#!pydefrun():'''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']exceptKeyError: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']}]}}returndns_setup_hsd