Ansible基于playbook批量修改主机名docker
安装Ansible,相信这里也不用多说,你们都知道shell
说一下环境:这里的主机名是修改以后的,我先把其余两台的主机名改成别的vim
192.168.30.21 主机名jenkins 安装Ansible 192.168.30.22 主机名docker 192.168.30.23 主机名repository
1.这里须要安装Ansible须要用到阿里云的yum仓库app
[root@jenkins]# wget -O /etc/yum.repos.d/aliyun.repo https://mirrors.aliyun.com/repo/Centos-7.repo --2019-05-31 09:43:15-- https://mirrors.aliyun.com/repo/Centos-7.repo 正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 121.22.232.218, 121.22.232.211, 221.194.147.226, ... 正在链接 mirrors.aliyun.com (mirrors.aliyun.com)|121.22.232.218|:443... 已链接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:2523 (2.5K) [application/octet-stream] 正在保存至: “/etc/yum.repos.d/aliyun.repo” 100%[=========================================================>] 2,523 --.-K/s 用时 0s 2019-05-31 09:43:16 (154 MB/s) - 已保存 “/etc/yum.repos.d/aliyun.repo” [2523/2523])
[root@jenkins yum.repos.d]# yum -y clean all [root@jenkins yum.repos.d]# yum makecache
2.安装Ansible自动化运维工具运维
[root@jenkins]# yum -y install ansible
3.修改/etc/ansible/hosts文件ssh
添加另外两台的IP+要完成修改的主机名+端口号+用户名+登陆密码ide
[root@jenkins ~]# vim /etc/ansible/hosts [test] 192.168.30.22 hostname=docker ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666 192.168.30.23 hostname=repository ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666
4.编写playbook剧本工具
[root@jenkins ~]# vim name.yml - hosts: test remote_user: root tasks: - name: change name raw: "echo {{hostname|quote}} > /etc/hostname" - name: shell: hostname {{hostname|quote}}
5.执行playbook -C 是预执行,若是没有报错就直接执行阿里云
[root@jenkins ~]# ansible-playbook -C name.yml [root@jenkins ~]# ansible-playbook name.yml
PLAY [test] ***code
TASK [Gathering Facts] ****
ok: [192.168.30.23]
ok: [192.168.30.22]
TASK [change name] ****
changed: [192.168.30.22]
changed: [192.168.30.23]
TASK [command] ****
changed: [192.168.30.23]
changed: [192.168.30.22]
PLAY RECAP ****
192.168.30.22 : ok=3 changed=2 unreachable=0 failed=0
192.168.30.23 : ok=3 changed=2 unreachable=0 failed=0
6.查看是否已经修改为功,没问题,咱们的主机名已经修改好了yes
[root@jenkins ~]# ansible test -m shell -a "hostname" 192.168.30.23 | SUCCESS | rc=0 >> repository 192.168.30.22 | SUCCESS | rc=0 >> docker