1.更改配置文件vim /etc/ansible/hosts
![](https://i-blog.csdnimg.cn/direct/2bd3352b59f04a9f95c8b569725ac64c.png)
2.测试m0主机与s0、s1、s2,之间可以ping通
[root@m0 ansible]# ansible group02 -m ping
![](https://i-blog.csdnimg.cn/direct/3b1018cc624548f38b0ec631b181b60a.png)
3.书写脚本文件
[root@m0 ~]# vim test0001.yml
---
- hosts: group02remote_user: roottasks:
- name: 卸载vsftpdyum: name=vsftpd state=absent- name: 安装vsftpdyum: name=vsftpd state=latest- name: 启动服务service: name=vsftpd state=started enabled=yes
~
![](https://i-blog.csdnimg.cn/direct/c065d8241c4c46b4960f07bd9b2cbf49.png)
4.运行脚本:
[root@m0 ~]# ansible-playbook ./test0001.yml
![](https://i-blog.csdnimg.cn/direct/42f707d8557c45c393355266e1e9b531.png)
5.在剧本中添加其他项
![](https://i-blog.csdnimg.cn/direct/0fb6144b728a4e04acabfce3d9c17d34.png)
6.添加重启项:
[root@m0 ~]# vim test0001.yml
---
- hosts: group02remote_user: roottasks:
- name: 卸载vsftpdyum: name=vsftpd state=absent- name: 安装vsftpdyum: name=vsftpd state=latest- name: 启动服务service: name=vsftpd state=started enabled=yes- name: 修改配置文件copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.confnotify:- abcdhandlers:- name: abcdservice: name=vsftpd state=restarted
~
~
[root@m0 ~]# ansible-playbook ./test0001.yml
![](https://i-blog.csdnimg.cn/direct/fdd3e23f4d7345e0b4d867d576b693e8.png)
7.写剧本:下载httpd服务并将原有端口80改为8080
[root@m0 ~]# vim httpd001.yml
---
- hosts: group02remote_user: roottask:- name: 安装httpdyum: name=httpd state=latest
- name: 修改配置文件command: sed -i '/^Listen/s/80/8080/g' /etc/httpd/conf/httpd.conf- name: 修改默认资源文件command: echo 'http html file' > /var/www/html/index.html- name: 启动httpd服务service: name=httpd state=started
~
~
[root@m0 ~]# ansible-playbook ./httpd001.yml
![](https://i-blog.csdnimg.cn/direct/cf8b6ae7e99849bdb8d471f269b6026d.png)