Brief introduction
Playbook is a very simple configuration management and multi-host deployment system. Can be used as a foundation for deploying complex applications.
The playbook can be customized and can be executed in an orderly manner with the specified sequence of operations, supporting both synchronous and asynchronous methods.
Playbook is described in YAML format, which enables deployment of multiple host applications and performs specific instruction steps on different groups of hosts.
Playbook uses examples to demonstrate its use to customize a simple Nginx package management, including installation, configuration templates, state management, and more.
Configuration file: Nginx.yml
----Hosts:webservers#The hosts parameter action: Defines the object of the operation, in this case the Webservers groupVARs:#VARs Parameters: Define variables (used when configuring templates), scoped to Webservers groupWorker_processes:4Num_cpus:4Max_open_file:65506Root:/Dataremote_user:root#Specifies the user name of the remote operation, which is root by default and supports sudo running, by adding Sudo:yestasks:#Defining a task List (top-down sequential execution)-Name:ensure Nginx isAt the latest version#each transaction can define a name tag, with the benefit of enhanced readability and the ability to see where the run is when the results are outputYum:pkg=nginx State=latest#Yum installs the latest version of Nginx-name:write The nginx config file template:src=/home/test/ansible/nginx/nginx2.conf dest=/etc/nginx/nginx.conf#Configure the Nginx configuration file according to the template, SRC is the main terminal template path, and Dest is the configured file path of the controlled end.Notify:-Restart Nginx-Name:ensure Nginx isrunning Service:name=nginx state=started#start NginxHandlers:#The notification handler (which must have a notify trigger to execute), selects the corresponding name tag in the handlers according to the notify to do so. If the Notify is restart Nginx, then handlers in the name tag content is restart Nginx, in order to perform-Name:restart nginx service:name=nginx state=restarted
Template: nginx2.conf
user nginx;worker_processes {{worker_prcesses}};{ if Num_cpus = = 2%; { elif Num_cpus = = 40100 0010 0001; { elif Num_cpus >= 800000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; { Else %0100 0010 0001; { % ENDIF%}worker_flimit_notifile {{max_open_file}};
Executive Playbook
Format:
Ansible-playbook playbook.yml (playbook file, customizable name) [parameters]
Cases:
# Enable 10 concurrent processes number execution Playbook (NGINX.YML)
Common parameters:
-U remote_user # manually specify playbook system user --syntax-check # Check Playbook syntax -- List-hosts playbook # matches to the host list -t timeout # defines the playbook execution timeout time -- Step # runs as a single task, making it easy to do every step of confirmation work --help # Help Info
Playbook roles and include declarations contain
When the playbook file is very large, it can be quite exhausting to reuse some features, ansible support for writing playbook files is split into multiple files and referenced in the form of inclusions (include).
Cases:
Feature (re-use) file: tasks/fool.yml
---#possibly saved as tasks/foo.yml- name:placeholder foo /bin/foo- name : Placeholder Bar /bin/bar
Playbook file used: PLAYBOOK.YML
tasks: # using include to reference the functionality of Multiplexing
Role
Role: Ansible custom good standard specification, with different levels of directory hierarchy and files on the role, variables, tasks, handlers, etc. to split, for the subsequent function expansion, maintainability lay the foundation.
Cases:
Take the above nginx.yml as an example to split the structure as follows:
Description
hosts
#自定义主机, non-mandatory option, the default will refer to/etc/ansible/hosts parameters, to refer to the custom hosts, need to be implemented through the-I file parameter, such as: Ansible-playbook-i hosts
[webservers]192.168.1.111192.168.1.112
Group_vars
#定义组变量目录, the file name in the directory is consistent with the group name, and the variable scope defined by the group variable file is within that group only and cannot be scoped to other groups
"Group_vars/all" #代表所有主机
---#Variables listedhere is applicable to all host groups ntpserver:ntp.sjtu.edu.cn
"Group_vars/webservers" #webservers组
---4466535/data
Site.yml
#全局配置文件, the following refers to two role blocks, the role of the scope of application and implementation functions are not the same
---- name:apply Common configuration to all nodes hosts:all roles: # corresponding directory: Nginx /roles/common and and application code hosts:webservers roles: # The corresponding directory is: Nginx/roles/web
Roles
#角色目录, typically each role corresponds to a specific functional service
"Roles/common"
handlers/main.yml #处理程序文件
---- Name:restart NTP service:name=ntp state=restarted
tasks/main.yml #任务列表文件
---- name:install NTP yum:name=ntp state=present- name:configure ntp file template:src # reference template does not require a write path, default in the ancestor's templates directory to find notify:restart NTP- Name:start the NTP Service service:name=ntp state=started enabled=trueif is running command:getenforce register:sestatus changed_when:false
templates/ngp.conf.j2 #模板
Driftfile /var/lib/ntp/driftrestrict 127.0.0.1restrict -6:: 1Server # here NtpServer references the ntpserver variable defined in vars/main.yml includefile /etc/ntp/rypto/pwkeys/etc/ Ntp/keys
vars/main.yml #变量配置文件
---#Variable listedhere is applicable to all host groups ntpserver:210.72.145.44
"Roles/web"
handlers/main.yml #处理程序文件
---- name:restart nginx service:name=nginx state=restarted
tasks/main.yml #任务列表文件
----name:ensure nginx is at the latest version # yum:pkg=nginx state=latest # yum Install the latest version of Nginx - Name:write the Nginx Config file template:src =/home/test/ansible/nginx/nginx2.conf dest=/etc/nginx/nginx.conf # notify: - restart Nginx -name:ensure nginx is running service:name =nginx state=started # start nginx
templates/nginx2.conf #模板
user nginx;worker_processes {{worker_prcesses}};{ if Num_cpus = = 2%; { elif Num_cpus = = 40100 0010 0001; { elif Num_cpus >= 800000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; { Else %0100 0010 0001; { % ENDIF%}worker_flimit_notifile {{max_open_file}};
Run roles
# enables the execution of playbook for 10 concurrent processes. The Hosts file uses the-I point to customize the Hosts,playbook configuration file to Site.yml
Resources:
Organized according to Liu Tians, "python automated operation and maintenance technology and best practices"
Ansible Value Playbook