我前面一篇文章讲述了如何这ansible中批量修改主机名,可是上面没有通过严格的测试。python
使用hostname模块修改主机名以后,/etc/hosts里的内容是并无修改的ios
这里会形成一个问题,若是hosts里的的主机名没有被绑定,那么咱们SSH的时候会形成问题,出现主机名没法解析的现象nginx
解决办法。我这里是ubuntu的机器 centos的还没测试过web
jastme@jastme2:~$ more /etc/nsswitch.conf # /etc/nsswitch.conf # # Example configuration of GNU Name Service Switch functionality. # If you have the `glibc-doc-reference' and `info' packages installed, try: # `info libc "Name Service Switch"' for information about this file. passwd: compat group: compat shadow: compat #hosts: files dns 换成下面的,取消DNS的解析 hosts: files networks: files protocols: db files services: db files ethers: db files rpc: db files netgroup: nis
再/etc/ssh/sshd_config中 加入 UseDNS noshell
以上解决办法能够消除掉hosts文件未被修改而致使ansible运行出错的问题ubuntu
如何解决这个问题,使用ansible修改主机名,而后再修改hosts文件?centos
root@ansible:/etc/ansible# pwd /etc/ansible root@ansible:/etc/ansible# tree . ├── ansible.cfg ├── hosts └── playbooks ├── hosts.yml ├── script │ └── test.sh ├── source ├── templates │ ├── hosts.j2 │ ├── nrpe.cfg │ ├── nrpe.j2 │ ├── profile │ ├── showtime │ └── test.j2 └── test.yml 4 directories, 11 files root@ansible:/etc/ansible#
playbookapp
root@ansible:/etc/ansible# cat playbooks/test.yml - hosts : test remote_user : jastme # gather_facts : no sudo : yes tasks: - name : install packetages on ubuntu apt : pkg={{ item }} state=present update_cache=no cache_valid_time=36000 force=yes # sudo : yes # debug : "{{ item }}" when : ansible_distribution == "Ubuntu" with_items : - gcc - g++ - nginx - name : install packetpages on centos # yum: name="@Development tools" state=present yum: name={{ item }} state=present when : ansible_distribution == "RedHat" with_items : - gcc - g++ - nginx - name : excute local script on client script : script/test.sh #with local script , same as iterable ===with_lines=== - name : change the client hostname by clinet app+ipadd #在这里咱们修改了主机名 hostname : name=web{{ ansible_eth1.ipv4.address.split('.')[-1] }} # - include : hosts.yml - name : gather facts again #从新获取一次facts,若是不从新获取,hosts.j2文件中的 {{ ansible_hostname }} 变量仍然是修改以前的值 setup : - name : change the hosts file because the hosts file not change by ansible hostname module #替换文件 template : src=templates/hosts.j2 dest=/etc/hosts - name : copy j2 file by template moulde template : src=templates/test.j2 dest=/tmp/test.txt mode=0700 notify : - jastme # call the handler which name is jastme - name : test the nrpe config file by templat template : src=templates/nrpe.j2 dest=/tmp/nrpe.cfg notify : - restart nrpe - name : copy the nrpe cfg to nrpe dir copy : src=templates/showtime dest=/tmp/showtime #module copies a file on the local box to remote handlers: - name : jastme command : echo "jastme is here" - name : restart nrpe shell : killall nrpe;/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d - name : change the hosts file because the hosts file not change by ansible hostname module template : src=templates/hosts.j2 dest=/etc/hosts
hosts.j2ssh
root@ansible:/etc/ansible# cat playbooks/templates/hosts.j2 127.0.0.1 localhost {{ ansible_hostname }}
验证 测试
修改以前的
jastme@hello2:~$ cat /etc/hosts 127.0.0.1 localhost hello2 jastme@hello2:~$ cat /etc/hostname hello2 jastme@hello2:~$ hostname hello2
测试下结果
root@ansible:/etc/ansible# ansible-playbook playbooks/test.yml -K -vv sudo password: PLAY [test] ******************************************************************* GATHERING FACTS *************************************************************** <192.168.1.2> REMOTE_MODULE setup ok: [192.168.1.2] TASK: [install packetages on ubuntu] ****************************************** <192.168.1.2> REMOTE_MODULE apt pkg=gcc,g++,nginx state=present update_cache=no cache_valid_time=36000 force=yes ok: [192.168.1.2] => (item=gcc,g++,nginx) => {"changed": false, "item": "gcc,g++,nginx"} TASK: [install packetpages on centos] ***************************************** skipping: [192.168.1.2] TASK: [excute local script on client] ***************************************** changed: [192.168.1.2] => {"changed": true, "rc": 0, "stderr": "", "stdout": "\r\nSUDO-SUCCESS-bcmzwtkepywyexpkhjtrjdvcodebaqer\r\nhello2\r\n"} TASK: [change the client hostname by clinet app+ipadd] ************************ <192.168.1.2> REMOTE_MODULE hostname name=web2 changed: [192.168.1.2] => {"changed": true, "name": "web2"} TASK: [gather facts again] **************************************************** <192.168.1.2> REMOTE_MODULE setup ok: [192.168.1.2] TASK: [change the hosts file because the hosts file not change by ansible hostname module] *** changed: [192.168.1.2] => {"changed": true, "dest": "/etc/hosts", "gid": 0, "group": "root", "md5sum": "91d555f2278a5d5ceef39fd460a03e78", "mode": "0644", "owner": "root", "size": 25, "src": "/home/jastme/.ansible/tmp/ansible-tmp-1458962053.13-120793882613399/source", "state": "file", "uid": 0} TASK: [copy j2 file by template moulde] *************************************** changed: [192.168.1.2] => {"changed": true, "dest": "/tmp/test.txt", "gid": 1000, "group": "jastme", "md5sum": "a0462664fea4fba50e80f4f863d46f52", "mode": "0700", "owner": "jastme", "size": 16, "src": "/home/jastme/.ansible/tmp/ansible-tmp-1458962053.29-203298900062115/source", "state": "file", "uid": 1000} TASK: [test the nrpe config file by templat] ********************************** ok: [192.168.1.2] => {"changed": false, "gid": 1000, "group": "jastme", "mode": "0600", "owner": "jastme", "path": "/tmp/nrpe.cfg", "size": 1706, "state": "file", "uid": 1000} TASK: [copy the nrpe cfg to nrpe dir] ***************************************** ok: [192.168.1.2] => {"changed": false, "dest": "/tmp/showtime", "gid": 1000, "group": "jastme", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "jastme", "path": "/tmp/showtime", "size": 0, "state": "file", "uid": 1000} NOTIFIED: [jastme] ************************************************************ <192.168.1.2> REMOTE_MODULE command echo "jastme is here" changed: [192.168.1.2] => {"changed": true, "cmd": ["echo", "jastme is here"], "delta": "0:00:00.001353", "end": "2016-03-26 11:14:13.754484", "rc": 0, "start": "2016-03-26 11:14:13.753131", "stderr": "", "stdout": "jastme is here"} PLAY RECAP ******************************************************************** 192.168.1.2 : ok=10 changed=5 unreachable=0 failed=0
看看客户端的结果
jastme@hello2:~$ cat /etc/hosts 127.0.0.1 localhost web2 jastme@hello2:~$ cat /etc/hostname web2 jastme@hello2:~$ hostname web2 jastme@hello2:~$
成功了