|
系统环境: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设置好)
[email protected]:~$ 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
[email protected]:~$ 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
[email protected]:~$ ansible-playbook name.yml
[WARNING]: log file at /var/log/ansible.log is not writeable and we cannot create it, abortingPLAY [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. 查看被管理机的主机名
[email protected]:~$ 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