1.更改配置文件vim /etc/ansible/hosts
2.测试m0主机与s0、s1、s2,之间可以ping通
[root@m0 ansible]# ansible group02 -m ping
3.书写脚本文件
[root@m0 ~]# vim test0001.yml
---
- hosts: group02
remote_user: root
tasks:
- name: 卸载vsftpd
yum: name=vsftpd state=absent
- name: 安装vsftpd
yum: name=vsftpd state=latest
- name: 启动服务
service: name=vsftpd state=started enabled=yes
~
4.运行脚本:
[root@m0 ~]# ansible-playbook ./test0001.yml
5.在剧本中添加其他项
6.添加重启项:
[root@m0 ~]# vim test0001.yml
---
- hosts: group02
remote_user: root
tasks:
- name: 卸载vsftpd
yum: name=vsftpd state=absent
- name: 安装vsftpd
yum: name=vsftpd state=latest
- name: 启动服务
service: name=vsftpd state=started enabled=yes
- name: 修改配置文件
copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf
notify:
- abcd
handlers:
- name: abcd
service: name=vsftpd state=restarted
~
~
[root@m0 ~]# ansible-playbook ./test0001.yml
7.写剧本:下载httpd服务并将原有端口80改为8080
[root@m0 ~]# vim httpd001.yml
---
- hosts: group02
remote_user: root
task:
- name: 安装httpd
yum: 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