首页 > 代码库 > 笔记二:ansible批量设置主机名

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


  • 批量设置主机名

系统环境:debian 8

本机ip   :180.76.169.6

被管理机:218.60.33.2 、218.60.33.3、218.60.33.4、218.60.33.5


更新时间: 2017-2-27
1. 在/etc/ansible/hosts配置主机信息(主机名需先在hosts设置好)
zhaojia@sredev2:~$ cat /etc/ansible/hosts

# This is the default ansible ‘hosts‘ file.

#

# It should live in /etc/ansible/hosts

#

......省略内容

[test]

218.60.33.2 hostname=cdnsy-junheng0.cdnsy ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=xxx

218.60.33.3 hostname=cdnsy-junheng1.cdnsy ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=xxx

218.60.33.4 hostname=cdnsy-junheng2.cdnsy ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=xxx

218.60.33.5 hostname=cdnsy-junheng3.cdnsy ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=xxx

—————————————————————————————————————

主机ip+ hostname+ 端口port+ 登录用户root+ 登录密码


2.修改主机名playbook

zhaojia@sredev2:~$ cat name.yml 

---

- hosts: test

  remote_user: root

  tasks:

     - name: change name

       raw: "echo {{hostname|quote}} > /etc/hostname"

     - name: 

       shell: hostname {{hostname|quote}}


3. 执行playbook

zhaojia@sredev2:~$ ansible-playbook name.yml 


[WARNING]: log file at /var/log/ansible.log is not writeable and we cannot create it, aborting

PLAY [test] ****************************************************************

TASK [setup] *******************************************************************

ok: [218.60.33.2]

ok: [218.60.33.3]

ok: [218.60.33.4]

ok: [218.60.33.5]

TASK [change name] *************************************************************

changed: [218.60.33.2]

changed: [218.60.33.3]

changed: [218.60.33.4]

changed: [218.60.33.5]

TASK [command] *****************************************************************

changed: [218.60.33.2]

changed: [218.60.33.3]

changed: [218.60.33.4]

changed: [218.60.33.5]

PLAY RECAP *********************************************************************

218.60.33.2               : ok=5    changed=4    unreachable=0    failed=0   

218.60.33.3               : ok=5    changed=4    unreachable=0    failed=0

218.60.33.4               : ok=5    changed=4    unreachable=0    failed=0   

218.60.33.5               : ok=5    changed=4    unreachable=0    failed=0   


4. 查看被管理机的主机名

zhaojia@sredev2:~$ ansible test -m shell -a "hostname"

218.60.33.2 | SUCCESS | rc=0 >>

cdnsy-junheng0.cdnsy

218.60.33.3 | SUCCESS | rc=0 >>

cdnsy-junheng1.cdnsy

218.60.33.4 | SUCCESS | rc=0 >>

cdnsy-junheng2.cdnsy

218.60.33.5 | SUCCESS | rc=0 >>

cdnsy-junheng3.cdnsy

本文出自 “一个Linux小白-学习运维” 博客,请务必保留此出处http://zhaojia.blog.51cto.com/12886290/1923584

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