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.
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.
Edit the init.sls file to have the following contents:
Template is an excellent feature of Salt, which provides us with lot of flexibilities with regards to configuration management. Often, administrators prefer to implement a masterless infrastructure for configuration management by keeping the entire Salt repository on each minion and configuring the minion to be its own master. In this recipe, you learned how to configure the /etc/hosts file to enable the minion to point to itself as the Salt master.
First, we created a new state called hostconfig. In the init.sls file of the state, we configured a file definition to manage the /etc/hosts file of the minion. We entered a generic name for the definition called hosts_file, and then specified the state module to use, which was file.managed. We then specified the actual file to manage, which was /etc/hosts and then specified all other parameters such as the user, group, and mode. The source of the file has been mentioned as follows:
- source: salt://hostconfig/files/hosts
In the preceding line, salt means that we are looking for the file in the Salt file_roots environment path, hostconfig is the state directory, and the rest is the path to the template file that we configured. The context section refers to variables that we are going to pass to the template file. Here, we defined two variables to pass to the template, that is, the hostname and the IP address of the minion. The template parameter is extremely important when defining a template. It specifies the template format that we are using and in the case of Salt, it is jinja.
Next, we created the template file /opt/salt-cookbook/staging/hostconfig/files/hosts and entered the following content into it:
Here, we can see that the values of the variables that we had passed to the template from the state file are being mentioned in the {{ variable_name }} format, where the actual values of the variables will be substituted when the states are applied to the minions.
Finally, we applied the state to the minion, and from the output, it is visible how the configured values are being substituted in the target file.
See also
The Managing files recipe in Chapter 5, Advanced Administration Tasks, to find out more about handling files with Salt
The Using requisites recipe, to learn how to create dependencies between Salt definitions