Table of contents
1. Understand the roles directory
2. Create roles directory based on building LAMP platform
1. Create a directory at 192.168.115.148
3. Write the main.yml file of the httpd role
4. Write the main.yml file of the mysql role
1. Understand the roles directory
In Ansible, roles are a way to organize and reuse tasks and variables. Roles are a reusable modular structure used to group related tasks, variables, and processing logic together for reuse in different playbooks.
By using roles, you can break complex tasks into smaller, manageable parts and make your playbooks more readable and maintainable. Roles also facilitate code reuse because roles can be reused in different scripts.
A role usually consists of:
1. Tasks: Contains the steps to be performed. These tasks can be any action related to a specific role, such as installing software, configuring files, starting services, etc.
2. Variables (vars): variables required to store the role. These variables can be used within the character's tasks to customize them for different needs.
3. Files: Contains the files required by the role. These files can be configuration files, script files, etc.
4. Templates: Contains template files used to generate configuration files, etc. Template files usually contain some customizable variables that can be replaced as needed when generating the file.
5. Handlers: Define role-related handlers to trigger actions when specific events occur. For example, restart a service after a configuration file change.
Roles provide a structured way to manage and organize complex configuration and deployment tasks by grouping related tasks, variables, and files together. They make playbooks easier to write, understand, and maintain, and facilitate the reuse and sharing of configuration and deployment logic across different playbooks.
roles/
├── role_name/ # role的名称
│ ├── defaults/ # 存放变量默认值的目录
│ │ └── main.yml # 变量默认值文件
│ ├── tasks/ # 存放任务文件的目录
│ │ └── main.yml # 任务文件
│ ├── templates/ # 存放模板文件的目录
│ │ └── template.j2 # 模板文件
│ ├── vars/ # 存放特定角色变量的目录
│ │ └── main.yml # 特定角色变量文件
│ ├── handlers/ # 存放处理器文件的目录
│ │ └── main.yml # 处理器文件
│ ├── files/ # 存放文件的目录
│ │ └── file.txt # 文件
│ ├── meta/ # 存放描述文件的目录
│ │ └── main.yml # 描述文件
│ └── README.md # role的说明文件
└── playbook.yml # 包含roles的Playbook文件
2. Create roles directory based on building LAMP platform
lab environment:
192.168.115.148: Install ansible and deploy LAMP
192.168.115.149: Deploy LAMP
192.168.115.151: Deploy LAMP
1. Create a directory at 192.168.115.148
cd /etc/ansible/roles/
mkdir -p {httpd,mysql,php}/{files,tasks,handlers,templates,vars,meta}
touch {httpd,mysql,php}/{tasks,handlers,vars,meta}/main.yml
tree
######################################################################
[root@localhost roles]# tree
.
├── http
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── mysql
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
└── php
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
2. Write php test page
vim /etc/index.php
<?php
phpinfo();
?>
3. Write the main.yml file of the httpd role
vim /etc/anseble/roles/http/tasks/mian/yml
---
- name: 安装httpd
yum: name=httpd state=present
- name: 分发php测试页面
copy: src=/index.php dest=/var/www/html/index.php
- name: 启动httpd、设为开机自启
service: name=httpd state=started enabled=yes
vim /etc/ansible/roles/http/hardlers/mian.yml
---
- name: restart httpd
shell: systemctl restart httpd
4. Write the main.yml file of the mysql role
vim /etc/ansible/roles/mysql/tasks/main.yml
---
- name: 安装mysql
shell: yum -y install mariadb mariadb-server
- name: 启动mariadb,并设为开机自启
service: name=mariadb state=started enabled=yes
5. Write the main.yml file of the php role
vim /etc/ansible/roles/php/tasks/main.yml
---
- name: 安装php及依赖包
yum: name=php,php-gd,php-ldap,php-odbc,php-pear,php-xml,php-xmlrpc,php-mbstring,php-snmp,php-soap,curl,curl-devel,php-bcmath,php-mysql state=present
notify: restart httpd
vim /etc/ansible/roles/php/hardlers/mian.yml
---
- name: restart httpd
shell: systemctl restart httpd
6. Write the lamp playbook
vim /etc/ansible/lamp.yml
---
- hosts: group
remote_user: root
roles:
- http
- mysql
- php
7. Start the script
ansible-playbook /etc/ansible/lapm.yml
Results of the