亚欧色一区w666天堂,色情一区二区三区免费看,少妇特黄A片一区二区三区,亚洲人成网站999久久久综合,国产av熟女一区二区三区

  • 發布文章
  • 消息中心
點贊
收藏
評論
分享
原創

ansible-playbook系列-自動生成hosts文件

2024-11-15 09:17:49
43
0

背景

在(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 
0條評論
0 / 1000
吳高峻
1文章數
0粉絲(si)數
吳高峻
1 文章 | 0 粉絲
吳高峻
1文(wen)章(zhang)數
0粉(fen)絲數
吳高峻
1 文章(zhang) | 0 粉絲
原創(chuang)

ansible-playbook系列-自動生成hosts文件

2024-11-15 09:17:49
43
0

背景

在(zai)istack的(de)交(jiao)(jiao)付(fu)(fu)過程中(zhong),集群節點(dian)的(de)信息需要同步維(wei)護在(zai)主機文件(jian)hosts與(yu)變(bian)量文件(jian)config.yaml,在(zai)進行交(jiao)(jiao)付(fu)(fu)部署(shu)的(de)時候,交(jiao)(jiao)付(fu)(fu)人員的(de)配(pei)置(zhi)失(shi)誤會(hui)導致(zhi)輕則部署(shu)失(shi)敗(bai),重則導致(zhi)看(kan)似成功,實(shi)則部署(shu)失(shi)敗(bai)的(de)問題(ti),并且在(zai)問題(ti)排(pai)查過程中(zhong)會(hui)被人下意(yi)識地忽(hu)略(lve)配(pei)置(zhi)不一致(zhi)的(de)問題(ti),影響交(jiao)(jiao)付(fu)(fu)的(de)周期。為了(le)解(jie)決這個問題(ti),這里給(gei)出一個方法,用于從變(bian)量生成hosts文件(jian)。

解決方案

整個方案包(bao)含三個文件:ansible-playbook文件,jinjia2文件以及變量配置文件。

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模板文件

模板文件文件名為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

變量配置文件

變量配置(zhi)文(wen)件名為config.yaml,配置(zhi)文(wen)件主機(ji)名與(yu)端口號為示例,并(bing)未在真實環(huan)境中被(bei)使用(yong)。

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

運行腳本

運行腳本,生(sheng)成(cheng)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 
文章來自個人專欄
文章 | 訂閱
0條評論
0 / 1000
請輸入你的評論
0
0