- Salt Cookbook
- Anirban Saha
- 646字
- 2021-07-16 13:21:59
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.
- Create a new state in the staging environment named
hostconfig
by creating a directory calledhostconfig
in the base directory of the staging environment. Create a directory calledfiles
in thehostconfig
directory and also a file namedinit.sls
. - 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] }} local_host_name: {{ grains['fqdn'] }}
- Create a file named
hosts
in the/opt/salt-cookbook/staging/hostconfig/files
directory and populate it with the following entries:127.0.0.1 localhost localhost.localdomain localhost4 ::1 localhost localhost.localdomain localhost6 {{ local_ip_address }} {{ local_host_name }} salt-master
- Now, run the following command to apply the state to the minion:
[root@salt-master ~]# salt 'stgdc1log01' state.sls hostconfig.hosts saltenv=staging stgdc1log01: ---------- ID: hosts_file Function: file.managed Name: /etc/hosts Result: True Comment: File /etc/hosts updated Started: 03:39:33.815985 Duration: 403.902 ms Changes: ---------- diff: --- +++ @@ -1,6 +1,3 @@ 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 +192.168.0.3 stgdc1log01 salt-master Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1
How it works...
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:
127.0.0.1 localhost localhost.localdomain localhost4 ::1 localhost localhost.localdomain localhost6 {{ local_ip_address }} {{ local_host_name }} salt-master
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
- Mastering Entity Framework Core 2.0
- Java高并發核心編程(卷2):多線程、鎖、JMM、JUC、高并發設計模式
- Learning Spring 5.0
- RabbitMQ Cookbook
- C語言程序設計實訓教程與水平考試指導
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- Scala編程實戰
- SSH框架企業級應用實戰
- Puppet 5 Beginner's Guide(Third Edition)
- HTML5 and CSS3:Building Responsive Websites
- Learning VMware vCloud Air
- 智能優化算法與MATLAB編程實踐
- Multithreading with C# Cookbook(Second Edition)
- Mastering Microsoft Dynamics AX 2012 R3 Programming
- Spring 5.0 Cookbook