官术网_书友最值得收藏!

  • 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.

  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] }}
          local_host_name: {{ grains['fqdn'] }}
  3. 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
  4. 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
主站蜘蛛池模板: 千阳县| 徐州市| 湘西| 沾益县| 波密县| 大宁县| 廉江市| 广河县| 巴彦淖尔市| 东乡| 太仓市| 石门县| 旬阳县| 志丹县| 外汇| 榆树市| 武强县| 鄂伦春自治旗| 临邑县| 吴堡县| 清新县| 大余县| 肇州县| 海丰县| 巨野县| 江西省| 黄骅市| 苗栗市| 资源县| 锦州市| 泾源县| 花垣县| 通道| 凤山县| 达拉特旗| 高清| 嘉禾县| 鄂伦春自治旗| 平阴县| 正安县| 西城区|