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文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
---
- 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 @   LB_运维技术  阅读(61)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2023-09-12 linux NFS报错 无法重启rpcbind
2022-09-12 Linux 系统IO响应缓慢系统hang住
2022-09-12 Linux 内核软死锁(soft lockup)bug原因分析
点击右上角即可分享
微信分享提示