Rendering host file written by ansible role

The hosts file is usually used when deploying a distributed cluster. We usually write in the ip address and host name. How to implement rendering when writing Ansible?
Define the roles first

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

The first file takes the default etc/ansible cfg file and removes the key. Otherwise, ssh will be reported_ vars/ all.yml You can't use this hosts to put variables. The most important thing is the following three hosts, roles and sites- all.yml , used to write the entire playbook

[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 is the role, init is the role of system initialization, and the specific task details of tasks are all passed through main.yml To implement the writing, the template module used here will be directly referenced by J2 of templates below

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

---
#This playbook needs to initialize each node

- name: add to hosts
  template: src=host.j2 dest=/etc/hosts
In the j2 file, we use the k8s group, that is, we use the k8s:children group defined by hosts to get the ip under the k8s group, and then define the host name node in our hosts_ What is the definition of name? You can get it directly
 inventory_hostname gets the current hostname of the operation
 And custom nodes_ Name, so it's rendered
[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 %} 

Test execution

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

PLAY [1,Host initialization] **********************************************************************************************************************

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 : add to 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  

See if the rendering was successful

[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

Keywords: Operation & Maintenance ansible ssh

Added by dutcbhboy83 on Wed, 27 May 2020 18:20:53 +0300