「ansible」修改主机名


「ansible」修改主机名

ansible的修改hostname的module 只能修改hostname,不会同时修改hosts文件,会导致不能sudo 等命令使用超时,因为不能解析主机名,所以修改hostname的时候需要使用两个模块,一个是hostname, 一个是lineinfile

首先是hostname,用来修改hostname

<code>- name: change hostname to myserver
hostname:
name: "webserver"/<code>

其次是lineinfile, 这个用来修改/etc/hosts下面的localhost

<code>- name: add myself to /etc/hosts
lineinfile:
dest: /etc/hosts
regexp: '^127\\.0\\.0\\.1[ \\t]+localhost'
line: '127.0.0.1 localhost webserver'
state: present/<code>

hostname和hosts不一致的,会导致sudo timeout, 最后使用ansible playbook的时候需要设置下timeout的值, 防止认证超时

<code>ansible-playbook -i linuxpc playbook/hostname.yml -u ubuntu -b -T 200/<code>


分享到:


相關文章: