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

  • 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
主站蜘蛛池模板: 乌鲁木齐县| 武乡县| 绵阳市| 阿克陶县| 页游| 长武县| 晋宁县| 射阳县| 年辖:市辖区| 阳谷县| 襄垣县| 于都县| 阿克苏市| 巨鹿县| 广河县| 安宁市| 通江县| 壤塘县| 常宁市| 南召县| 永修县| 庄河市| 大石桥市| 喀喇沁旗| 神池县| 毕节市| 兴宁市| 海林市| 南安市| 仙桃市| 依安县| 梨树县| 永德县| 呼和浩特市| 体育| 江陵县| 齐齐哈尔市| 正宁县| 灵璧县| 吐鲁番市| 南陵县|