Simplify playbooks with characters
Ansible roles have the following advantages:
1. Roles can group content and are easy to reuse
2. Roles can be used to define basic system information: web servers, database servers, etc.
3. Roles can make large projects easy to manage
4. Roles can be parallelized by different people Development
The following subdirectories should be included under the role directory:
ansible | Roles will separate playbooks with different functions. A standard role contains the following subdirectories: |
defaults | The main.yml contains the default value of the role variable #The lowest priority among all variables |
files | Contains static files referenced by role tasks |
handlers | Mainly define handlers |
meta | Role-related information, such as author, license, etc. |
tasks | Define tasks |
templates | The jinja2 template referenced by the task |
tests | Can contain a manifest and test.yml playbook for testing |
whose | Variables defining roles |
## Use ansible characters in playbook
like:
- hosts: www.example.com
roles:
- role1
- role2
var1:var1
var2:var2 为 role2 定义了两个变量,任何 defaults 和 vars 中的变量都会被覆盖
Reuse content with system roles
## Install System Role
yum install -y rhel-system-roles
#The installed roles are located at:
ls -l /usr/share/ansible/roles
#The content (view network role)
ls -l /usr/share/ansible/roles/rhel-system-roles.network
## time synchronization role example
# See which roles can be used in the system
ansible-galaxy list
#Create directory to save variables
mkdir -pv group_vars/all
#View help
cat /usr/share/doc/rhel-system-roles/timesync/README.md
#Time zone setting example
ansible-doc timezone | grep -A 4 "EXAMPLES"
# Manifest and configuration files
# modify the main playbook
vim configure_time.yml
---
- name: Time Sync
hosts: groups
roles:
- rhel-system-roles.timesync
post_tasks:
- name: Set Timezone
timezone:
name: "{{ host_timezone }}"
notify: restart crond
handlers:
- name: restart crond
service:
name: crond
state: restarted
#Create a variable directory for the host group
mkdir -pv group_vars/{group1,group2}
#Create time yml file and write
# Detect and run
ansible-playbook --syntax-check configure_time.yml
ansible-playbook configure_time.yml
Creating a Role
You can search and download related roles on galaxy.ansible.com.
## Role creation process: divided into three steps
1. Create a role directory structure
2. Define the role content
3. Use the role in the playbook
## Create a character framework
[root@workstation mnt] cd project/
[root@workstation project] mkdir roles
[root@workstation project] cd roles/
[root@workstation roles] ansible-galaxy init my_role 创建目录,自动生成配置文件
tree my_role
my_role #The specific role project name, such as nginx, tomcat, php (free setting)
├── defaults #Used to set default variables for the current role, this directory should contain a main.yml
file│ └── main.yml # main.yml, similar to the main function in the code, for unified management
├── files # used to store files called by the copy module or script module
├── handlers # used to define the actions performed when the condition is triggered in this role , This directory should contain a main.yml file
│ └── main.yml
├── meta # used to define the special settings of this role and its dependencies, this directory should contain a main.yml file
│ └── main .yml ├──
README.md #Description
file├── tasks #Used to define the task list of the current role, this directory should contain a main.yml
file│ └──
main.yml ├── templates #Used to store jinjia2 Templates, the template module will automatically find jinjia2 template files in this directory
├── tests #Used to store the role of the test role itself The playbook and host definition files are compared in the development and testing stage
│ ├── inventory is commonly used, this directory should contain a main.yml file and its own resource setting inventory
│ └── test.yml
└── vars #Used to define the variables used by this role, this directory should contain a main.yml file
└── main.yml
#Create and initialize
[root@workstation ~] mkdir role-create
[root@workstation ~] cd role-create/
[root@workstation role-create] mkdir -v roles;cd roles
mkdir: created directory 'roles'
[root@workstation roles] ansible-galaxy init myvhost
- myvhost was created successfully
Configure tasks
vim tasks / main.yml
'install, start and configure virtual host'
---
# tasks file for myvhost
- name: Install httpd
yum:
name: httpd
state: latest
- name: Start and Enabled httpd
service:
name: httpd
state: started
enabled: true
- name: Install vhost file
template:
src: vhost.conf.j2
dest: /etc/httpd/conf.d/vhost.conf
owner: root
group: root
mode: 0644
notify:
- restart httpd
#Write handler [root @ workstation handlers] # vim main.yml
--
# handlers file for myvhost
- name: restart httpd
service:
name: httpd
state: restarted
#Write html file
mkdir -p role-create/files/html
vim files/html/index.html 创建index文件。
Create a new files directory and create an html directory in it. Note that this is not the files directory in myvhost. You can put it anywhere. This is just for convenience.
#Configuration template
vim templates/vhost.conf.j2
<VirtualHost *:80>
DocumentRoot /www
ServerName www.westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.westos.org
</VirtualHost>
#Write playbook using roles [root @ workstation role-create] # vim use_vhost_role.yml
- name: use myvhost
hosts: webservers
pre_tasks:
- name: pre_tasks message
debug:
msg: 'Ensure web server configuration'
roles:
- myvhost
post_tasks:
- name: Configure html
copy:
src: files/html/
dest: "/var/www/vhosts/{{ ansible_hostname }}"
- name: post_tasks message
debug:
msg: "Web server is configured."
'The host list used here is the system default: / etc / ansible / hosts'
execution result:
## Control the execution order
Sometimes it is necessary to perform some tasks before or after the role:
Keyword:
Before:
after pre_tasks: post_tasks