Book Image

Salt Cookbook

By : Anirban Saha
Book Image

Salt Cookbook

By: Anirban Saha

Overview of this book

Table of Contents (18 chapters)
Salt Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Using iterations in states


The need to do the same task repeatedly for a number of entities of the same type is a very basic requirement of any role in an organization. To achieve this, languages and tools provide us with the feature of iterations, simply known as loops. In this recipe, you will learn how to apply iterations in our configuration.

How to do it...

  1. We will use the same minion as the previous recipe. Modify the user pillar, that is, edit /opt/salt-cookbook/pillar/staging/user/init.sls to have the following contents:

    dev_user_list:
      optimus:
        uid: 7001
        passwd: '$1$Dw1TxMI7$pmeYTdmz.rlunqPd7JELR.'
      bumblebee:
        uid: 7002
        passwd: '$1$ZHUeIAfq$6sJl9rHVDX2UjBH1KrPZP1'
      ironhide:
        uid: 7003
        passwd: '$1$rcJAiq7y$bJzv3HzVTbeQlA3cIu1Gb1'
  2. Edit /opt/salt-cookbook/staging/user/init.sls to have the following contents:

    {% for user, details in pillar['dev_user_list'].iteritems() %}
    {{ user }}:
      user.present:
        - home: /home/{{ user }}
        - uid: {{ details['uid'] }}
     ...