主机名 | IP | 服务 |
---|---|---|
server4 | 172.25.35.4 | 主控节点 |
server5 | 172.25.35.5 | 被控节点 |
server6 | 172.25.35.6 | 被控节点 |
由于软件包具有依赖性,所以此实验需要配置ansible的源,西面分别在三台虚拟机上配置yum源,步骤如下:
真机:
虚拟机server4上:
虚拟机server5上:
虚拟机server6上:
1、在server4上面安装ansible
[root@server4 ~]# yum install -y ansible
[root@server4 ~]# ansible --version
[root@server4 ansible]# cd /etc/ansible/
[root@server4 ansible]# ls
ansible.cfg hosts roles
[root@server4 ansible]# ll ansible.cfg
-rw-r--r-- 1 root root 20277 Feb 22 07:04 ansible.cfg
[root@server4 ansible]# ll hosts
-rw-r--r-- 1 root root 1016 Feb 22 07:04 hosts
[root@server4 ansible]# cd roles/
[root@server4 roles]# ls
[root@server4 roles]# cd ..
4、编辑/etc/hosts文件,添加本地解析
5、创建普通用户并添加普通用户ansible,编辑ansible主配置文件
[root@server4 ansible]# useradd devopes
[root@server4 ansible]# su - devopes
[devopes@server4 ~]$ ls
[devopes@server4 ~]$ mkdir ansible
[devopes@server4 ~]$ ll /etc/ansible/ansible.cfg
-rw-r--r-- 1 root root 20277 Feb 22 07:04 /etc/ansible/ansible.cfg
[devopes@server4 ~]$ ls
ansible
[devopes@server4 ~]$ cd ansible/
[devopes@server4 ansible]$ ls
[devopes@server4 ansible]$ pwd
/home/devopes/ansible
[devopes@server4 ansible]$ ls
[devopes@server4 ansible]$ vim ansible.cfg
[devopes@server4 ansible]$ cat ansible.cfg
[defaults]
inventory = ./inventory
[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
[test]
172.25.35.5
[prod]
172.25.35.6
7、分别在server5和server6创建用户devopes并设置密码
[root@server5 ~]# useradd devopes
[root@server5 ~]# passwd devopes
[root@server6 ~]# useradd devopes
[root@server6 ~]# passwd devopes
[devopes@server4 ansible]$ ssh-keygen
[devopes@server5 ~]$ ssh-copy-id 172.25.35.5
[devopes@server6 ~]$ ssh-copy-id 172.25.35.6
[devopes@server4 ansible]$ id
[devopes@server4 ansible]$ ssh 172.25.35.5
[devopes@server4 ansible]$ ssh 172.25.35.6
[devopes@server1 ansible]$ ansible all -m ping
[devopes@server1 ansible]$ ansible all -m ping -u devopes
[devopes@server1 ansible]$ ansible all -m ping -u root
[devopes@server1 ansible]$ ansible test -m copy -a "src=/etc/passwd
[devopes@server1 ansible]$ ansible test -a "ls /tmp"
[devopes@server1 ansible]$ ansible test -a "ls /tmp"
<1> ping一下远程主机组的所有主机
<2> 使用devops的身份去检测
<3> copy模块
[root@server5 ~]# cd /tmp/
[root@server5 tmp]# ls
passwd
[root@server2 tmp]#
[root@server5 ~]# vim /etc/sudoers
92行 devopes ALL=(ALL) NOPASSWD: ALL
[root@server6 ~]# vim /etc/sudoers
92行 devopes ALL=(ALL) NOPASSWD: ALL
[devopes@server4 ansible]$ ansible test -m copy -a "src=/etc/passwd dest=/mnt/passwd" -b
[devopes@server4 ansible]$ ansible test -m copy -a "ls /mnt"
[devopes@server4 ansible]$ ls
ansible.cfg inventory
[devopes@server4 ansible]$ vim ansible.cfg
[devopes@server4 ansible]$ cat ansible.cfg
[defaults]
inventory = ./inventory
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[devopes@server4 ansible]$ ansible test -m copy -a "src=/etc/passwd dest=/mnt/passwd"
[devopes@server4 ansible]$ ls
ansible.cfg inventory
[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
[test]
172.25.35.5
[prod]
172.25.35.6
[devopes@server4 ansible]$ ansible 'test:!prod' -m ping
172.25.35.5 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
[test]
172.25.35.5
[prod]
172.25.35.6
172.25.35.5
[devopes@server4 ansible]$ ansible 'test:!prod' -m ping
[WARNING]: No hosts matched, nothing to do
[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
[test]
172.25.35.5
[prod]
172.25.35.6
[devopes@server4 ansible]$ ansible 'test:&prod' -m ping
[WARNING]: No hosts matched, nothing to do
}
[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
[test]
172.25.35.5
[prod]
172.25.35.6
172.25.35.5
[devopes@server4 ansible]$ ansible 'test:&prod' -m ping
172.25.35.5 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
[test]
172.25.35.5
[prod]
172.25.35.6
[devopes@server4 ansible]$ ansible 'test:prod' -m ping
172.25.35.5 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.25.35.6 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
[test]
172.25.35.5
[prod]
172.25.35.6
[webserver:children]
test
prod
[devopes@server4 ansible]$ ansible webserver -m ping
172.25.35.5 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.25.35.6 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[devopes@server4 ansible]$ ansible test -m yum -a "name=httpd state=present"
[root@server5 ~]# rpm -q httpd
httpd-2.4.6-45.el7.x86_64
[devopes@server4 ansible]$ ansible test -a "rpm -q httpd"
给server5安装httpd服务
在server5上查看发现了httpd服务
在server4上也可以查看,此时也能发现httpd的服务
[devopes@server4 ansible]$ ansible test -m yum -a "name=httpd state=absent"
[root@server5 ~]# rpm -q httpd
[devopes@server4 ansible]$ ansible test -m yum -a "name=httpd state=present"
[devopes@server4 ansible]$ ansible test -m service -a "name=httpd state=started"
[root@server5 ~]# systemctl status httpd
[devopes@server4 ansible]$ ansible test -m service -a "name=httpd state=stopped"
[root@server5 ~]# systemctl status httpd
在server5上查看httpd的状态,发现是开启的
关闭httpd服务
再次到server5上查看httpd的状态,发现是关闭的
<9> 给test组的主机创建用户
[devopes@server4 ansible]$ ansible test -m user -a "name=gjl password=westos"
[devopes@server5 mnt]$ cat /etc/passwd | grep gjl
[devopes@server5 mnt]$ cat /etc/shadow | grep gjl
[devopes@server1 ansible]$ ansible test -m setup
[devopes@server4 ansible]$ ansible test -m yum -a "name=mariadb-server state=present"
[devopes@server4 ansible]$ ansible test -m yum -a "name=MySQL-python.x86_64 state=present"
[devopes@server4 ansible]$ ansible test -m service -a "name=mariadb state=started"
[devopes@server4 ansible]$ ansible test -m mysql_user -a "name=gjl password=westos priv=*.*:select host='%' state=present"
[root@foundation35 Desktop]# mysql -h 172.25.35.2 -u gjl -p
show databases;
[root@server5 mnt]# mysql
show databases;
[devopes@server1 ansible]$ ansible test -m user -a "name=gjl password={{'westos'|password_hash('sha512')}}"
[devopes@server5 mnt]$ cat /etc/shadow | grep gjl
[devopes@server1 ansible]$ ansible test -m user -a "name=gjl password={{'westos'|password_hash('sha512','westos')}}"
[devopes@server5 mnt]$ cat /etc/shadow | grep gjl