编辑
hosts
配置文件
[root@mo ~]# vim /etc/ansible/hosts
![](https://i-blog.csdnimg.cn/direct/ce8f7d171a8f443ea54a702d841ee696.png)
#
创建目录
[root@mo ~]# mkdir /etc/ansible/playbook
#
编辑配置文件
[root@mo ~]# vim /etc/ansible/playbook/nginx.yml
![](https://i-blog.csdnimg.cn/direct/112df7f4883540c7904a22f8454526fa.png)
#
执行测试
[root@mo ~]# ansible-playbook /etc/ansible/playbook/nginx.yml
![](https://i-blog.csdnimg.cn/direct/ec4db26d0c0740e8a77335225e63842d.png)
roles
修改组
[root@mo ~]# vim /etc/ansible/hosts
![](https://i-blog.csdnimg.cn/direct/a58d283d9d9043998b4f8c4c7b24383c.png)
检查主机
s
是否可连接
[root@mo ~]# ansible s -m ping
s0 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
s1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
创建
roles
的目录结构
[root@mo ~]# cd /etc/ansible/roles/
[root@mo roles]# mkdir nginx
[root@mo roles]# ls
nginx
[root@mo roles]# cd nginx/
[root@mo nginx]# mkdir
![](https://i-blog.csdnimg.cn/direct/51612185534e4f1aa52b37924d47587c.png)
运行
[root@mo ~]# ansible-playbook /etc/ansible/playbook/test002.yml
![](https://i-blog.csdnimg.cn/direct/03aec12f58e345949937a13df5a56ef4.png)
卸载
nginx
[root@mo ~]# ansible s -m yum -a 'name=nginx state=absent'
编辑配置文件
---
安装
nginx
[root@mo ~]# vim /etc/ansible/roles/nginx/tasks/main.yml
![](https://i-blog.csdnimg.cn/direct/d4b5e2a3dce34b4eae4e220ac343d660.png)
运行
[root@mo ~]# ansible-playbook /etc/ansible/playbook/test002.yml
![](https://i-blog.csdnimg.cn/direct/3b22fa0edb914c408b4cdee477fcec1c.png)
使用
role
实现
lnmp
nginx
改配置
8080
mariadbz
中创建
eleme
数据库
创建表
ser
![](https://i-blog.csdnimg.cn/direct/ee084e59293942b4bbaaeee35d76c93d.png)
[root@mo ~]# vim /etc/ansible/roles/nginx/tasks/main.yml
![](https://i-blog.csdnimg.cn/direct/7b2b23e233cd4e6495c2aedb200b1811.png)
![](https://i-blog.csdnimg.cn/direct/8a3bdc7b14fc4e1cbd5dddb49c292094.png)
[root@mo ~]# cd /etc/ansible/
[root@mo ansible]# mkdir script
[root@mo script]# vim dbtable.sh
![](https://i-blog.csdnimg.cn/direct/03afd536771b42a38d046d9849ea078e.png)
[root@mo script]# cd
[root@mo ~]# vim /etc/ansible/playbook/test003.yml
![](https://i-blog.csdnimg.cn/direct/e3a9a83bc91541b4a7866a8775cf31a1.png)
[root@mo ~]# ansible-playbook /etc/ansible/playbook/test003.yml
![](https://i-blog.csdnimg.cn/direct/132a39b093ea4c4fa923374ddca19cdd.png)