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


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