修改之前的主机名
# 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}}
ya

该文章描述了如何使用Ansible来修改远程主机的名称。首先展示了hosts文件的内容,然后编写了一个名为`modify_hostname.yaml`的Ansibleplaybook,通过`raw`和`shell`模块执行命令改变主机名。在执行过程中遇到了Python版本不一致的警告,但最终成功地更改了两台远程主机的名称。
最低0.47元/天 解锁文章
901

被折叠的 条评论
为什么被折叠?



