ansible 批量设置主机名

修改之前的主机名

# hostname -i
::1 127.0.0.1

编写hosts文件

在任意位置编写hosts文件

# cat hosts
[test]
ailog4x10.221.206.227 hostname=paas-10-221-206-227 ansible_python_interpreter=/usr/bin/python3 ansible_ssh_host=10.221.206.227 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='Tsss'
ailog4x10.221.206.228 hostname=paas-10-221-206-228 ansible_python_interpreter=/usr/bin/python3 ansible_ssh_host=10.221.206.228 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='Tsss'

ansible_python_interpreter=/usr/bin/python3 这是因为,远端主机上python的版本跟ansible主机上的ansible版本不一致。

编写playbook

[root@log4x6 es]# cat modify_hostname.yaml
---
- hosts: test
  tasks:
    - name: change name
      raw: "echo {{hostname|quote}} > /etc/hostname"
    - name:
      shell: hostnamectl set-hostname {{hostname|quote}}

yaml文件在乎格式,格式很重要,执行前,先检查语法

]# ansible-playbook -i /data/ailog4x/deploy/es/hosts  --syntax-check modify_hostname.yaml

playbook: modify_hostname.yaml

检查没问题后,执行

# ansible-playbook -i /data/ailog4x/deploy/es/hosts modify_hostname.yaml

PLAY [test] **********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************
[WARNING]: sftp transfer mechanism failed on [10.221.206.227]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: sftp transfer mechanism failed on [10.221.206.228]. Use ANSIBLE_DEBUG=1 to see detailed information
ok: [ailog4x10.221.206.227]
ok: [ailog4x10.221.206.228]

TASK [change name] ***************************************************************************************************************************************************************************************************
changed: [ailog4x10.221.206.227]
changed: [ailog4x10.221.206.228]

TASK [shell] *********************************************************************************************************************************************************************************************************
[WARNING]: sftp transfer mechanism failed on [10.221.206.228]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: sftp transfer mechanism failed on [10.221.206.227]. Use ANSIBLE_DEBUG=1 to see detailed information
changed: [ailog4x10.221.206.228]
changed: [ailog4x10.221.206.227]

PLAY RECAP ***********************************************************************************************************************************************************************************************************
ailog4x10.221.206.227      : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
ailog4x10.221.206.228      : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

检查主机

hostname -i
fexxf126%bond0 fe8xxx:afa3%bond1 10.221.208.227 10.221.206.227 192.168.122.1
[ailog4x@paas-10-221-206-227 ~]$ hostname 
paas-10-221-206-227
[ailog4x@paas-10-221-206-227 ~]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值