ansible批量修改主机名

(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: all
  become: yes
  tasks:
    - name: 修改 /etc/hostname 文件
      lineinfile:
        path: /etc/hostname
        line: "{{ hostname }}"
        create: yes
    - name: 设置主机名
      command: hostnamectl set-hostname "{{ hostname }}"
      
    - name: 更新当前 shell 会话中的主机名
      shell: |
        hostname "{{ hostname }}"

  

 

posted @ 2024-09-12 15:00  LB_运维技术  阅读(31)  评论(0编辑  收藏  举报