(1)hosts文件
[task]
IP地址1 hostname=主机名1
IP地址2 hostname=主机名2
IP地址3 hostname=主机名3
[task:vars]
ansible_become=yes
ansible_become_method=sudo
ansible_become_user=root
ansible_become_password=密码
ansible_user=用户
ansible_password=密码
ansible_python_interpreter=/usr/bin/python
(2)yaml文件
--- - name: 设置主机名hosts: allbecome: yestasks:- name: 修改 /etc/hostname 文件lineinfile:path: /etc/hostnameline: "{{ hostname }}"create: yes- name: 设置主机名command: hostnamectl set-hostname "{{ hostname }}"- name: 更新当前 shell 会话中的主机名shell: |hostname "{{ hostname }}"