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

  • 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
主站蜘蛛池模板: 乳源| 会泽县| 胶州市| 巴中市| 沾化县| 兰考县| 昌江| 新宁县| 晋州市| 简阳市| 都兰县| 桓台县| 天门市| 娱乐| 铁岭县| 龙里县| 甘南县| 郯城县| 西充县| 镇沅| 溧水县| 高安市| 常宁市| 大新县| 武威市| 南陵县| 临清市| 安溪县| 明溪县| 吉林市| 高雄县| 连江县| 泰顺县| 通州区| 太白县| 昭通市| 洮南市| 航空| 得荣县| 青冈县| 溆浦县|