为什么80%的码农都做不了架构师?>>>   

我前面一篇文章讲述了如何这ansible中批量修改主机名,但是上面没有经过严格的测试。

使用hostname模块修改主机名之后,/etc/hosts里的内容是并没有修改的

这里会造成一个问题,如果hosts里的的主机名没有被绑定,那么我们SSH的时候会造成问题,出现主机名无法解析的现象

解决办法。我这里是ubuntu的机器  centos的还没测试过

  1. 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:       filesprotocols:      db files
    services:       db files
    ethers:         db files
    rpc:            db filesnetgroup:       nis

再/etc/ssh/sshd_config中 加入 UseDNS no

以上解决办法可以消除掉hosts文件未被修改而导致ansible运行出错的问题

如何解决这个问题,使用ansible修改主机名,然后再修改hosts文件?

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.yml4 directories, 11 files
root@ansible:/etc/ansible#

playbook

root@ansible:/etc/ansible# cat playbooks/test.yml
- hosts : testremote_user : jastme
#  gather_facts : nosudo : yestasks:- name : install packetages on ubuntuapt : 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=presentyum: name={{ item }} state=presentwhen : ansible_distribution == "RedHat"with_items :- gcc- g++- nginx- name : excute local script on clientscript : 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 mouldetemplate : src=templates/test.j2 dest=/tmp/test.txt mode=0700notify :- jastme                             # call the handler which name is jastme- name : test the nrpe config file by templattemplate : src=templates/nrpe.j2 dest=/tmp/nrpe.cfgnotify :- restart nrpe- name : copy the nrpe cfg to nrpe dircopy : src=templates/showtime dest=/tmp/showtime    #module copies a file on the local box to remotehandlers:- name : jastmecommand : echo "jastme is here"- name : restart nrpeshell : 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 moduletemplate : src=templates/hosts.j2 dest=/etc/hosts

hosts.j2

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:~$

成功了

转载于:https://my.oschina.net/jastme/blog/648746

使用ansible批量修改主机名后/etc/hosts文件不能被正确修改的修复方法相关推荐

  1. centos永久修改主机名以及etc/hosts文件详解

    目录 1 在CentOS7中,有三种定义的主机名 2 查看主机名 3 修改主机名 3.1 方法1:临时有效 3.2 方法2:永久生效 4 注意事项 5 etc/hosts文件详解 1 在CentOS7 ...

  2. MySQL服务器修改主机名后问题解决

    1.单机MySQL主机名修改 今天无事看到自己的主机名不对,于是改了一下,以便区分服务器,那只重启MySQL时出现下面错误: MySQL manager or server PID file coul ...

  3. ubuntu修改主机名后无法解析主机

    修改完主机名后在执行sudo命令时, 会提示sudo: 无法解析主机.在网上搜了下,找到了解决方法: 1.sudo vim /etc/hosts 找到如下行: 127.0.1.1 XXX 修改为: 1 ...

  4. ORACLE 10G修改主机名后EM Enterprise Manager无法启动的解决办法-Robert

    修改主机名从info到info-0073后,ORACLE EM (Enterprise Manager)无法启动,尝试手工解决.<?xml:namespace prefix = o ns = & ...

  5. linux修改主机名后oracle em控制台起不来,更改计算机名后导致Oracle dbconsole无法启动问题解决方法...

    今天不知道哪根筋搭歪了,看着Oracle EM控制台的主机名WIN-LOSGI0TCOG0乱七八糟的很不爽,就把它给改了.然后Oracle EM就上不去了,Oracledbconsole服务起不来,我 ...

  6. sap 修改主机名过程

    因公司应急演练需要,SAP需要在虚拟环境下,快速复制并启动,为防止与原系统产生干扰 需要复制出来并修改主机名 本案例主机名从sapprd改成TRA 过程如下 1.主机名和etc/hosts文件 1.1 ...

  7. Centos 7 修改主机名

    前言 CentOS Linux release 7.4.1708 (Core) Step1:修改主机名 hostnamectl set-hostname test01 Step2:验证 hostnam ...

  8. MySQL5.6从库修改主机名同步停止的解决办法

    2019独角兽企业重金招聘Python工程师标准>>> 环境:MySQL5.6 + CentOS6.5 问题描述:从库修改主机名后,从库的同步没有自动启动,用start slave命 ...

  9. rhel修改hostname_RHEL7 -- 修改主机名

    RHEL7中,有三种定义的主机名: a.静态的(static):"静态"主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名 b.瞬态的(trans ...

最新文章

  1. 何恺明团队推出Mask^X R-CNN,将实例分割扩展到3000类
  2. 雷观(十六):帮人写项目,不如教会别人写项目的方法
  3. 万物之始正则表达式全解析三部曲(上篇)-正则表达式基础知识及语法
  4. 【SSH】禁用root远程、修改ssh端口
  5. NOIP 2011 聪明的质检员-二分答案
  6. C#套接字和windowsAPI套接字
  7. crop video in ffmpeg
  8. Spark实现WordCount案例
  9. 让Windows下的驱动乖乖在Linux中安家!
  10. 基于hydra的ssh密码的暴力破解
  11. 《解密并行和分布式深度学习:深度并发分析》摘要记录
  12. 电商数据分析基础指标体系
  13. 无法登陆skype显示无法找到服务器,无法登录 Lync,因为找不到此登录地址 - Skype for Business | Microsoft Docs...
  14. 网络爬虫技术是什么,网络爬虫的基本工作流程是什么?
  15. 浅析SkipList跳跃表原理及代码实现
  16. QQ “安全检查未通过,禁止下载该文件” 解决方法
  17. WebKit 打电话失败的问题
  18. acg-faka--功能丰富的发卡二次元商城源码
  19. chia官方矿池常见问题
  20. mysql innodb_large_prefix

热门文章

  1. declval 的说明
  2. 前端与后台(学习自某B站Up主)
  3. iwatch可以用计算机吗,千万不要买AppleWatch的5个原因!
  4. 【数字图像处理】前期准备工作,库的安装(skimage库的安装!)
  5. 跟小静读CLR via C#(18)——Enum
  6. 交换机(三层)接入层、汇聚层和核心层交换机的特点
  7. rx6600xt显卡相当于什么显卡
  8. c语言中如何将字体弄大,CFree怎样调大字体
  9. 项目1 设计简易灯箱画廊 实训要求: (1)利用超链接和图像标记设计简易灯箱画廊。 (2)给简易灯箱画廊增加背景音乐效果。
  10. html5诊断报告,放射科诊断报告模板.docx