mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml
写一个简单的tasks/main.yml
vim /etc/ansible/roles/httpd/tasks/main.yml
- name: install apache
yum: pkg={
{
servername }} state=latest
定义变量:可以定义在全局变量中,也可以定义在roles角色变量中.
vim /etc/ansible/roles/httpd/vars/main.yml
servername: httpd
vim /etc/ansible/roles/mysql/tasks/main.yml
- name: install mysql
yum: pkg={
{
mysqlname }} state=latest
vim /etc/ansible/roles/mysql/vars/main.yml
mysqlname: mariadb*
vim /etc/ansible/roles/php/tasks/main.yml
- name: install php
yum: pkg={
{
phpname }} state=latest
vim /etc/ansible/roles/php/vars/main.yml
phpname: php
vim /etc/ansible/site.yml
- hosts: webserver
remote_user: root
roles:
- httpd
- mysql
- php