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

Configuring templates


In infrastructure management, we often face situations when we have to push files to multiple hosts but the information in the files is host-specific such as the hostname, IP address, and so on. In Salt, we can achieve the same objectives using templates, and in this recipe, we are going to learn about how to use templates.

How to do it...

We will use the minion stgdc1log01 that we configured in the last recipe, Using state modules.

  1. Create a new state in the staging environment named hostconfig by creating a directory called hostconfig in the base directory of the staging environment. Create a directory called files in the hostconfig directory and also a file named init.sls.

  2. Edit the init.sls file to have the following contents:

    hosts_file:
      file.managed:
        - name: /etc/hosts
        - source: salt://hostconfig/files/hosts
        - user: root
        - hroup: root
        - mode: 644
        - template: jinja
        - context:
          local_ip_address: {{ grains['ip4_interfaces']['eth1'][0]...