#写一个简单的tasks/main.yml
vim /etc/ansible/roles/httpd/tasks/main.yml
- name: ensure apache is at the latest version
yum: pkg={{ pkg }} state=latest
#定义变量,可以定义在全局变量中,也可以定义在roles角色变量中,一般定义在角色变量中
vim /etc/ansible/roles/httpd/vars/main.yml
pkg: httpd
编写mysql模块
vim /etc/ansible/roles/mysql/tasks/main.yml
- name: ensure mysql is at the latest version
yum: pkg={{ pkg }} state=latest
#定义变量
vim /etc/ansible/roles/mysql/vars/main.yml
pkg: mariadb*
编写php模块
vim /etc/ansible/roles/php/tasks/main.yml
- name: ensure php is at the latest version
yum: pkg={{ pkg }} state=latest
#定义变量
vim /etc/ansible/roles/php/vars/main.yml
pkg: php*
编写roles实例
vim /etc/ansible/site.yml
- hosts: webserver
remote_user: root
roles:
- httpd
- mysql
- php
#执行剧本
ansible-playbook site.yml