--- - hosts: control-plane name: "rename CP" tasks: - name: set_fact: hostnameid: "{{ groups[group_names[0]].index(inventory_hostname) | int + 1 }}" - debug: msg: "K8s-{{ group_names[0] }}{{ hostnameid }}" - name: set hostname become: yes hostname: name: "K8s-{{ group_names[0] }}{{ hostnameid }}" use: systemd - hosts: worker-node name: "rename WN" tasks: - name: set_fact: hostnameid: "{{ groups[group_names[0]].index(inventory_hostname) | int + 1 }}" - debug: msg: "K8s-{{ group_names[0] }}{{ hostnameid }}" - name: set hostname become: yes hostname: name: "K8s-{{ group_names[0] }}{{ hostnameid }}" use: systemd - hosts: all name: "change host file" tasks: - name: generate file become: yes blockinfile: backup: yes path: /etc/hosts block: | {% for host in groups['all'] %} {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['fqdn'] }} {{ hostvars[host]['ansible_facts']['hostname'] }} {% endfor %}