salt-jinja变量使用grains

salt '' saltutil.sync_grains
salt '
' saltutil.sync_modules

语法如下:{{ grains.['ip_interfaces'] }}
如下是通过grains获得的minion服务器的信息:

...
    cpu_model:
        Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz
    cpuarch:
        x86_64
    defaultencoding:
        None
    defaultlanguage:
        None
    domain:
    fqdn:
        ubuntu
    fqdn_ip4:
        - 127.0.1.1
    fqdn_ip6:
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              unknown
    host:
        ubuntu
    id:
        ubuntu
    ip_interfaces:
        ----------
        eth0:
            - 192.168.220.133
        lo:
            - 127.0.0.1
    ipv4:
        - 127.0.0.1
        - 192.168.220.133
    ipv6:
        - ::1
        - fe80::20c:29ff:fe34:c4cb
    kernel:
        Linux
    kernelrelease:
        4.4.0-31-generic
    localhost:
        ubuntu
    lsb_distrib_codename:
        trusty
    lsb_distrib_description:
        Ubuntu 14.04.5 LTS
    lsb_distrib_id:
        Ubuntu
    lsb_distrib_release:
        14.04
    manufacturer:
...

依然使用上一篇salt部署zabbix来举例,部署完成后,额希望吧配置文件中的Hostname= “ ”
的值改成各自机器的ip地址,这样动态获取比一台一台去改快很多,

# Mandatory: no
# Default:
# Hostname=

Hostname={{ grains['ip_interfaces']['eth0'][0] }}

### Option: HostnameItem
#       Item used for generating Hostname if it is undefined. Ignored if Hostname is defined.
#       Does not support UserParameters or aliases.
#
# Mandatory: no
# Default:

替换完成后,我们执行下:

[root@centos2 conf]# salt  ubuntu  state.sls  cp  saltenv=base  test=true
ubuntu:
----------
          ID: send zabbix.tar.gz
    Function: file.managed
        Name: /usr/local/services/zabbix-3.0.0.tar.gz
      Result: None
     Comment: The following values are set to be changed:
              newfile: /usr/local/services/zabbix-3.0.0.tar.gz
     Started: 
    Duration: 
     Changes:   
----------
          ID: send configuration file
    Function: file.managed
        Name: /etc/zabbix_agentd.conf
      Result: None
     Comment: The following values are set to be changed:
              diff: --- 
              +++ 
              @@ -140,7 +140,7 @@
               # Default:
               # Hostname=
               
              -Hostname=Zabbix server
              +Hostname=192.168.220.133
               
               ### Option: HostnameItem
               #    Item used for generating Hostname if it is undefined. Ignored if Hostname is defined.
     Started: 
    Duration: 
     Changes:   

Summary
------------
Succeeded: 2 (unchanged=2)
Failed:    0
------------
Total states run:     2

从执行结果上看到:

              +Hostname=192.168.220.133

我们通过测试运行 ,这里已经将zabbixagent中Hostname=Zabbix server动态改成了对应agent的ip地址

你可能感兴趣的:(salt-jinja变量使用grains)