saltstack 实战(三)

目录

1. saltstack 实战(一)  安装saltstack与配置

2. saltstack 实战(二)  利用saltstack安装软件  

3. saltstack 实战(三)  利用saltstack管理主机hosts文件

    待更新...

利用saltstack管理主机hosts文件


创建saltstack  host目录

mkdir -p /data/salt/salt/host  /data/salt/pillar/host

/data/salt/salt/host/map.jinja

{% set settings = salt['pillar.get']('host')%}


/data/salt/salt/host/config.sls

{% from "host/map.jinja" import settings with context %}

{% if 'tasks' in settings %}
{% for task,task_options in settings.tasks.iteritems() %}

host.{{ task }}:
    host.{{ task_options.type|default('present') }}:
        - name: '{{ task_options.name }}'
        - ip: '{{ task_options.ip }}'
{% endfor %}
{% endif %}


/data/salt/salt/host/init.sls

include:
    - host.config


/data/salt/pillar/host/init.sls

#    - type: present  ; present 添加  ; absent 删除   default: present
#    - name: 主机名  必须参数
#    - ip: 对应IP地址 必须参数
host:
    tasks:
        node01:
            type: 'present'
            name: 'node01'
            ip: '172.16.50.111'
        node02:
            type: 'present'
            name: 'node02'
            ip: '172.16.50.118'


执行

salt-call state.sls host.config


查看

cat /etc/hosts
.....
172.16.50.111           node01
172.16.50.118           node02
.....


说明:

引用了pillar变量,查看pillar变量

salt-call pillar.items  # 查看该主机拥有的所有pillar变量
salt-call pillar.get host # 查看该主机pillar变量 host
salt-call pillar.get host:tasks:node01 #  查看该主机pillar变量 host下的tasks再下级的node01


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章