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=123 192.168.32.133 ansible_ssh_pass=123
2.看看2台主机的原始机器名
主机1 [root@ZooKeeper-01 ~]# hostname ZooKeeper-01 [root@ZooKeeper-01 ~]# ifconfig eth2 eth2 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 eth2 eth2 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 lo 2: 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:ff 3: 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:ff 4: 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 eth2 5: 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:ff 6: 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:ff 7: 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:0 8: 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 lo 2: 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:ff 3: 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:ff 4: 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 eth2 5: 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:ff 6: 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:ff 7: 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 bond0 8: 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
验证修改成功。。。