ansible 使用主机IP批量修改机器名

Posted 系统运维分享

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible 使用主机IP批量修改机器名相关的知识,希望对你有一定的参考价值。

1.首先看看我的hosts配置

root@jastme:/etc/ansible/playbooks# more ../hosts 
[test]192.168.32.131 ansible_connection=local 
[test1]192.168.32.133 ansible_ssh_pass=123[test2]192.168.32.132 ansible_ssh_pass=123[testall]192.168.32.132 ansible_ssh_pass=123192.168.32.133 ansible_ssh_pass=123

2.看看2台主机的原始机器名

主机1[root@ZooKeeper-01 ~]hostname ZooKeeper-01[root@ZooKeeper-01 ~]ifconfig eth2eth2      Link encap:Ethernet  HWaddr 00:0C:29:58:95:55  
          inet addr:192.168.32.132  Bcast:192.168.32.255  Mask:255.255.255.0          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:39614 errors:0 dropped:0 overruns:0 frame:0          TX packets:18265 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000 
          RX bytes:5279094 (5.0 MiB)  TX bytes:1110521 (1.0 MiB)
          
主机2[root@ZooKeeper-02 ~]hostname ZooKeeper-02[root@ZooKeeper-02 ~]ifconfig eth2eth2      Link encap:Ethernet  HWaddr 00:0C:29:C8:7F:48  
          inet addr:192.168.32.133  Bcast:192.168.32.255  Mask:255.255.255.0          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:41538 errors:0 dropped:0 overruns:0 frame:0          TX packets:20085 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000 
          RX bytes:6164131 (5.8 MiB)  TX bytes:1729825 (1.6 MiB)

3. 目标

我们需要需要改这两台机器的主机名,按照他们自己的IP修改   类似  web132  web133


4. playbook

root@jastme:/etc/ansible/playbooks# more changehostname.yml - hosts : testall
  remote_user : root
  tasks :
  - name : show hostname
    shell : hostname
  - name : show ip
    command : ip a
  - hostname : name=web{{ ansible_default_ipv4.address.split('.')[-1] }}                         #直接调用res字典,引用成变量


5.执行一下,看看结果

root@jastme:/etc/ansible/playbooks# ansible-playbook changehostname.yml             PLAY [testall] **************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [192.168.32.133]                                                                                               #IP{'module_name''setup''module_complex_args': {}, 'module_args'''}
ok: [192.168.32.132]
{'module_name''setup''module_complex_args': {}, 'module_args'''}

TASK: [show hostname] ********************************************************* 
changed: [192.168.32.133]
ZooKeeper-02                                                                                                        #主机名{'module_name'u'shell''module_complex_args': {}, 'module_args'u'hostname'}
changed: [192.168.32.132]
ZooKeeper-01{'module_name'u'shell''module_complex_args': {}, 'module_args'u'hostname'}

TASK: [show ip] *************************************************************** 
changed: [192.168.32.133]1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00    inet 127.0.0.1/8 scope host lo2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 qlen 1000    link/ether 00:0c:29:c8:7f:34 brd ff:ff:ff:ff:ff:ff3: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond1 qlen 1000    link/ether 00:0c:29:c8:7f:3e brd ff:ff:ff:ff:ff:ff4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000    link/ether 00:0c:29:c8:7f:48 brd ff:ff:ff:ff:ff:ff
    inet 192.168.32.133/24 brd 192.168.32.255 scope global eth25: eth3: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 qlen 1000    link/ether 00:0c:29:c8:7f:34 brd ff:ff:ff:ff:ff:ff6: eth4: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond1 qlen 1000    link/ether 00:0c:29:c8:7f:3e brd ff:ff:ff:ff:ff:ff7: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue 
    link/ether 00:0c:29:c8:7f:34 brd ff:ff:ff:ff:ff:ff
    inet 144.148.64.230/24 brd 144.148.64.255 scope global bond0
    inet 144.148.64.231/32 brd 144.148.64.231 scope global bond0:08: bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue 
    link/ether 00:0c:29:c8:7f:3e brd ff:ff:ff:ff:ff:ff
    inet 10.25.17.82/24 brd 10.25.17.255 scope global bond1
    inet 10.25.17.83/32 brd 10.25.17.83 scope global bond1:0{'module_name'u'command''module_complex_args': {}, 'module_args'u'ip a'}
changed: [192.168.32.132]1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00    inet 127.0.0.1/8 scope host lo2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 qlen 1000    link/ether 00:0c:29:58:95:41 brd ff:ff:ff:ff:ff:ff3: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond1 qlen 1000    link/ether 00:0c:29:58:95:4b brd ff:ff:ff:ff:ff:ff4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000    link/ether 00:0c:29:58:95:55 brd ff:ff:ff:ff:ff:ff
    inet 192.168.32.132/24 brd 192.168.32.255 scope global eth25: eth3: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 qlen 1000    link/ether 00:0c:29:58:95:41 brd ff:ff:ff:ff:ff:ff6: eth4: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond1 qlen 1000    link/ether 00:0c:29:58:95:4b brd ff:ff:ff:ff:ff:ff7: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue 
    link/ether 00:0c:29:58:95:41 brd ff:ff:ff:ff:ff:ff
    inet 144.148.64.229/24 brd 144.148.64.255 scope global bond08: bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue 
    link/ether 00:0c:29:58:95:4b brd ff:ff:ff:ff:ff:ff
    inet 10.25.17.81/24 brd 10.25.17.255 scope global bond1
{'module_name'u'command''module_complex_args': {}, 'module_args'u'ip a'}

TASK: [hostname name=web{{ ansible_default_ipv4.address.split('.')[-1] }}] **** 
changed: [192.168.32.133]
{'module_name'u'hostname''module_complex_args': {}, 'module_args'u'name=web133'}                                      #这里就是我们需要的操作  可以看见主机名已经被修改成功changed: [192.168.32.132]
{'module_name'u'hostname''module_complex_args': {}, 'module_args'u'name=web132'}

PLAY RECAP ******************************************************************** 192.168.32.132             : ok=4    changed=3    unreachable=0    failed=0   192.168.32.133             : ok=4    changed=3    unreachable=0    failed=0


6. 验证

[root@ZooKeeper-01 ~]# hostname web132

[root@ZooKeeper-02 ~]# hostname web133


验证修改成功


以上是关于ansible 使用主机IP批量修改机器名的主要内容,如果未能解决你的问题,请参考以下文章

笔记二:ansible批量设置主机名

Ansible playbook 批量修改服务器密码 先普通后root用户

Ansible-playbook实现Apache(httpd)编译安装及批量部署

linux 批量修改主机名脚本

ansible的配置文件和模块使用

Ansible介绍