Ansible-Role編寫之使用渲染host文件

通常部署分佈式集羣的時候都會用到hosts文件,咱們通常會把ip地址和主機名寫進去,那麼編寫Ansible的時候渲染如何實現?
首先定義這個roles角色node

[root@ansible kubeadm-high-install]# ls
ansible.cfg  group_vars  hosts  roles  site-all.yml

第一個文件將默認的etc/ansible的cfg文件拿過來,將key去掉,否則會報ssh的問題,group_vars/all.yml放變量的,寫這個hosts也用不到,最重要的就是後面三個hosts、roles、site-all.yml,用來編寫整個playbookdom

[root@ansible kubeadm-high-install]# cat hosts
[master]
192.168.30.51 node_name=k8s-master1
192.168.30.52 node_name=k8s-master2
192.168.30.53 node_name=k8s-master3

[node]
192.168.30.54 node_name=node1

[k8s:children]
master  
node

roles就是這個角色,init就是系統初始化的這個角色,tasks具體的任務細節都經過main.yml來實現編寫,這裏使用的template模塊,若是下面templates的J2就會直接引用上ssh

[root@ansible kubeadm-high-install]# cat roles/init/tasks/main.yaml 

---
#這個playbook須要初始化每一個節點

- name: 添加hosts
  template: src=host.j2 dest=/etc/hosts
而這個j2文件裏面說的是經過k8s這個組,也就是咱們在hosts定義的k8s:children這個組下來的,hostvars來獲取k8s組下面的ip ,後面定義咱們hosts裏面的主機名node_name是什麼hostname定義的直接也就獲取到了
 inventory_hostname 獲取被操做的當前主機主機名
 以及自定義的node_name,這樣就渲染過來了
[root@ansible kubeadm-high-install]# cat roles/init/templates/host.j2 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for host in groups['k8s'] %}
{{ hostvars[host].inventory_hostname }} {{ hostvars[host].node_name }}
{% endfor %}

測試執行分佈式

[root@ansible kubeadm-high-install]# ansible-playbook -i hosts site-all.yml 

PLAY [一、主機初始化] **********************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.30.53]
ok: [192.168.30.52]
ok: [192.168.30.54]
ok: [192.168.30.51]

TASK [init : 添加hosts] ***************************************************************************************************************
changed: [192.168.30.52]
changed: [192.168.30.51]
changed: [192.168.30.54]
changed: [192.168.30.53]

PLAY RECAP **************************************************************************************************************************
192.168.30.51              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.30.52              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.30.53              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.30.54              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

查看是否成功渲染ide

[root@ansible kubeadm-high-install]# ansible all -a "cat /etc/hosts"
192.168.30.51 | CHANGED | rc=0 >>
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.51 k8s-master1
192.168.30.52 k8s-master2
192.168.30.53 k8s-master3
192.168.30.54 node1

192.168.30.54 | CHANGED | rc=0 >>
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.51 k8s-master1
192.168.30.52 k8s-master2
192.168.30.53 k8s-master3
192.168.30.54 node1

192.168.30.52 | CHANGED | rc=0 >>
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.51 k8s-master1
192.168.30.52 k8s-master2
192.168.30.53 k8s-master3
192.168.30.54 node1

192.168.30.53 | CHANGED | rc=0 >>
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.51 k8s-master1
192.168.30.52 k8s-master2
192.168.30.53 k8s-master3
192.168.30.54 node1
相關文章
相關標籤/搜索