背景
在(zai)istack的(de)(de)(de)交(jiao)付過程中,集群節點的(de)(de)(de)信息需要(yao)同步(bu)維護在(zai)主機文件(jian)hosts與變量文件(jian)config.yaml,在(zai)進行交(jiao)付部(bu)署(shu)的(de)(de)(de)時候,交(jiao)付人員的(de)(de)(de)配(pei)置失誤(wu)會導致輕則(ze)部(bu)署(shu)失敗(bai),重則(ze)導致看似(si)成功,實則(ze)部(bu)署(shu)失敗(bai)的(de)(de)(de)問題(ti)(ti),并且在(zai)問題(ti)(ti)排查過程中會被人下意(yi)識(shi)地忽略配(pei)置不一致的(de)(de)(de)問題(ti)(ti),影響交(jiao)付的(de)(de)(de)周期。為了解(jie)決這個問題(ti)(ti),這里給出一個方(fang)法(fa),用于從變量生(sheng)成hosts文件(jian)。
解決方案
整個(ge)方案包(bao)含三個(ge)文件(jian):ansible-playbook文件(jian),jinjia2文件(jian)以及變量配(pei)置文件(jian)。
ansible-playbook文件
playbook文件名 inventory.yaml
- hosts: localhost
become: yes
become_user: root
become_method: sudo
gather_facts: no
tasks:
- name: 將計算節點添加進主機組
add_host:
hostname: "{{ item.host_ip }}"
ansible_ssh_host: "{{ item.host_ip }}"
ansible_ssh_port: "{{ item.host_port }}"
ansible_ssh_user: "{{ item.username }}"
ansible_ssh_pass: "{{ item.password }}"
groups: "{{ item.roles }}"
with_items: "{{ servers }}"
- name: 生成hosts文件
template: src={{ playbook_dir }}/hosts.j2 dest={{ playbook_dir }}/hosts
jinja2模板文件
模板文件文件名(ming)為hosts.j2
{% for grp in ["master", "worker"] %}
[{{ grp }}]
{% if grp in groups.keys() %}
{% for hst in groups[grp] %}
{{ hst }} ansible_ssh_host={{ hst }} ansible_ssh_port={{ hostvars[hst]['ansible_ssh_port'] }} ansible_ssh_user={{ hostvars[hst]['ansible_ssh_user'] }} ansible_ssh_pass={{ hostvars[hst]['ansible_ssh_pass'] }}
{% endfor %}
{% endif %}
{% endfor %}
[manager:children]
master
worker
變量配置文件
變量配(pei)置文件名(ming)為config.yaml,配(pei)置文件主機名(ming)與端口號為示例,并未在真實環境(jing)中(zhong)被使用。
servers:
- host_ip: 192.168.1.1
host_port: 22
username: xxx
password: xxxxxx
roles:
- master
- host_ip: 192.168.1.2
host_port: 22
username: xxx
password: xxxxxx
roles:
- master
- worker
運行腳本
運行腳(jiao)本,生成hosts文件。
[root@istack-oms-pro-24 test]# ls
config.yaml hosts.j2 inventory.yaml
[root@istack-oms-pro-24 test]# ansible-playbook -e '@config.yaml' inventory.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] ****************************************************************************************************************************************************************************************************************
TASK [將計算節點添加進主機組] **************************************************************************************************************************************************************************************************************
changed: [localhost] => (item={u'username': u'xxx', u'host_port': 22, u'password': u'xxxxxx', u'host_ip': u'192.168.1.1', u'roles': [u'master']})
changed: [localhost] => (item={u'username': u'xxx', u'host_port': 22, u'password': u'xxxxxx', u'host_ip': u'192.168.1.2', u'roles': [u'master', u'worker']})
TASK [生成hosts文件] ****************************************************************************************************************************************************************************************************************
changed: [localhost]
PLAY RECAP **********************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@istack-oms-pro-24 test]# ls
config.yaml hosts hosts.j2 inventory.yaml
[root@istack-oms-pro-24 test]# cat hosts
[master]
192.168.1.1 ansible_ssh_host=192.168.1.1 ansible_ssh_port=22 ansible_ssh_user=xxx ansible_ssh_pass=xxxxxx
192.168.1.2 ansible_ssh_host=192.168.1.2 ansible_ssh_port=22 ansible_ssh_user=xxx ansible_ssh_pass=xxxxxx
[worker]
192.168.1.2 ansible_ssh_host=192.168.1.2 ansible_ssh_port=22 ansible_ssh_user=xxx ansible_ssh_pass=xxxxxx
[manager:children]
master
worker
[root@istack-oms-pro-24 test]# vim inventory.yaml
[root@istack-oms-pro-24 test]# vim hosts.j2
[root@istack-oms-pro-24 test]# vim config.yaml