For loops and pillar data in saltstack states

I was tinkering with iterating through pillar data in saltstack and I hit an odd bump. First and foremost, pillar data is apparently only available during the highstate runs. Calling a state that sources pillar data in an iterative fashion will fail when called via ‘salt \* state.sls mystate’. And it’ll fail silently, that’s the ugly part.

Anyway, quick rundown of adding a bunch of SSH Keys to a single user using Pillar data. 🙂

Pillar data located in /srv/pillar/dev-keys/init.sls

dev-keys:
beavis:
sshkey: ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA7zP6KewSJhSbmLAJoldDBiJlMEVZw2N0fdKX/0up6iYU0xlFjHpefwy3vCrGrlO4z6G09KRJoS2RnyyDn6o8CaCCfJNpQwMPtrMf/bYwdTwwktRXH44cQB54g+D1wSxJvpEKId3VpgEsvp+F9vBioWSJZOi8nm46/BwJZbmAWOM4KcQ2erAP7Y0VdfI42c4qraP/p8EQH6IiLFZqcaX/M2osuz1piKbi0R2UrPNeQW2JnLAXFOiqppPBRKhh5a5jDVYaeNYrp3HN2V9CzqsCxHNZ0kyb/OnKeZDtQW56oeywQNmVH1ujXSNxG9aeNOv8JoJ5AUuOWRWYkDfTpoOWSw== beavis@i.am.cornhol.io
butthead:
sshkey: ssh-dss AAAAB3NzaC1kc3MAAACBAJeFBZLdxOCEoQlcxQl2//3QfYHg0SEI21wtSgpz4Xkr3BaLAu/Ne9w4xBOd0cD6DchqqjXDTuO8xV996LVX/Tgb+eFXHjCHeKpzNdpv/Lk68QhsXaYgFWNf0SM4JOuIAditB/l6N6jxdI/UpGws51rjsZfjJmBeY1ozW3RFcUFJAAAAFQChe22uEXLRiLgDoxQGreYnu8JItwAAAIAXa3uUNv/pLVpLDPWkciin7f4fguhmPeqZqPEtkucxAtFot0YIBSeY9j1x4KcNsd7Hdifu8dCUysAeUi1vtqqgnT3DwgvTjw4WHxTl7F9sVAZZBXvIucc88cBs81Bz0i7cDvzs/XAZmwy51vTYdTc0pEWwzadiqI/fQAox+CVicAAAAIBFHCM/wKzLwfQTqBsXZbIudqUEDtvLM1x3seWYhZ93BNQIvJsWu4OtggH9LE7bd1Q6D01wQNHXylw+C1/UQxspUSVL/wtxt6z24A4pEUnU8hr1+OhRa5pw3x5Gu8fuDSd0f4UoDDJkU5EGGa/CfnzlN0B8kGIybNNpfajnXb3/zg== butthead@huhuhuh.huh.local

And our statefile…

shareduser:
user:
- present
- home: /home/shareduser
ssh_auth:
- present
- user: shareduser
- require:
- user: shareduser
- names:
{% for keys, args in pillar['dev-keys'].iteritems() %}
- {{ args['sshkey'] }}
{% endfor %}

Remember, run that from highstate or face the ugly “print hostname and exit” “error” that salt will throw at you. 🙂

Add Comment

Required fields are marked *. Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.