See this blog experimental environment using ansible deployment and the basic module of
the playbooks of the need to write yml document is divided into multiple roles, easy management.
[devops@server1 ansible]$ mkdir roles
[devops@server1 ansible]$ ansible-galaxy list ##查看角色列表
[devops@server1 ansible]$ cd roles/
[devops@server1 roles]$ ansible-galaxy init httpd ##创建角色
- httpd was created successfully
We can see the files and directories within the directory generated.
Character | effect |
---|---|
files | Used to store the file copy module called by the script or module. |
templates | Jinjia2 used to store template, template module will automatically find jinjia2 template files in this directory. |
tasks | This directory should contain a main.yml file, define the task list for this role, this document can be used include contain other task files located in this directory. |
handlers | This directory should contain a main.yml file that defines the action to perform this role when the trigger condition. |
whose | This directory should contain a main.yml file that defines the variables used in this role. |
defaults | This directory should contain a main.yml file, used to set the default variable to the current role. |
meta | This directory should contain a main.yml file that defines the special setting of this role and its dependencies. |
We found a playbook of these roles will in fact have been a long yml separate file to a different directory httpd nothing like the code deployment services used earlier:
---
- hosts: webservers ##声明作用的主机
vars: ##角色1
http_port: 8080
tasks: ##角色2
- name: install httpd
yum:
name: httpd
state: latest
- name: create index.html
copy:
content: "{{ ansible_facts['hostname'] }}\n"
dest: /var/www/html/index.html
tags: index
- name: configure httpd
template:
src: templates/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd
service:
name: httpd
state: started
enabled: true
- name: start firewalld
service:
name: firewalld
state: started
enabled: true
- name: configure firewalld
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
handlers: ##角色3
- name: restart httpd
service:
name: httpd
state: restarted
We found that the above code marked roles vars, tasks, handler in the directory generated above all our correspondence below the above code is split into a role, a closer look at how to write a character playbook file.
First, let's write vars role
[devops@server1 httpd]$ ls
defaults files handlers meta README.md tasks templates vars
[devops@server1 httpd]$ vim vars/main.yml
Var content on and above the content of the module as
the same, write task roles
[devops@server1 httpd]$ ls
defaults files handlers meta README.md tasks templates vars
[devops@server1 httpd]$ vim tasks/main.yml
---
- name: install httpd
yum:
name: httpd
state: latest
- name: create index.html
copy:
content: "{{ ansible_facts['hostname'] }}\n"
dest: /var/www/html/index.html
tags: index
- name: configure httpd
template:
src: templates/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd
service:
name: httpd
state: started
enabled: true
- name: start firewalld
service:
name: firewalld
state: started
enabled: true
- name: configure firewalld
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
Contents and task module as above.
Next, write handlers role
[devops@server1 httpd]$ ls
defaults files handlers meta README.md tasks templates vars
[devops@server1 httpd]$ vim handlers/main.yml
Finally, templates role
[devops@server1 httpd]$ cd templates/
[devops@server1 templates]$ ls
httpd.conf
[devops@server1 templates]$ vim httpd.conf
This directory put the main configuration file httpd service, the port is set vars role {{http_port}}, easily modified later to do the port test.
This put a yml file into multiple roles, but you will find that the host has not been defined.
[devops@server1 ansible]$ vim apache.yml
---
- hosts: all ##定义主机
roles:
- httpd ##定义角色
We want to make this whole big role httpd run up, to be written in a general yml file ansible main configuration file, define the operation of the host, after pushing the yml file on it.
[devops@server1 ansible]$ ansible-playbook apache.yml
Push the success in testing to see if httpd roles we create can achieve the corresponding functions
[devops@server1 ansible]$ vim roles/httpd/vars/main.yml
Write vars role, to see whether the port can be modified.
[devops@server1 ansible]$ ansible-playbook apache.yml
Push.
View the remote host port, successfully modified, so the file is divided into multiple roles yml a success.