Use of Ansible playbooks

Source: Internet
Author: User
Tags dot net

Playbooks Concept:
Tasks:
Tasks: Specific operational tasks that are supported by each module:
Example:-M user-a ' name= password= '
Variables:
Variable:
Templates:
Template:
After defining the template, you can implement the variables corresponding to each node to replace the expression itself according to the current node
The assigned value is used to perform the operation, and the resulting value is given this parameter for generating configuration files of different configurations.
So the template is mainly implemented to configure different scene text files. And so using template language to define, template
The language can be replaced by definition with certain values for a specific host.
Handlers:
Processor:
If the remote and local configuration files are inconsistent if a change in the Operation profile
is to overwrite the configuration file so that it needs to be touched when the configuration file sends a change
Mechanisms to operate. In fact, it is a predefined operation that can be triggered under certain conditions:
Roles:
Role:
A mechanism of hierarchical organization playbook and its dependent resources; roles can be called separately;
The configuration of server servers between each other must be completely different, if for these nodes simultaneously
It is not possible to configure and provide the same playbooks, so divide each server into groups
Some roles or functions are the same, so for example, there may be time synchronization between Web servers,
Some services are generic, so having these mechanisms enables you to implement a class-specific server as needed
The same operation defines the underlying configuration, defining specific functions as a class of operations
Playbooks is a declarative profile that is defined based on the Yaml language
Laybooks
Contain one or more plays
Written in YAML #使用YAML编写的文档
-declarative Config Profile declaration
-not Code #没有代码
Executed in the order it is written (aka imperative)

tasks: #将一个任务定义应用某一个具体操作
–name:install Apache #定义名称为install Apache
Apt:pkg=apache2-mpm-prefork State=latest
#使用apt模块进行安装状态为latest
First, define a httpd service and enable it to turn on the auto-start service
Web. Yaml
–name:web servers
Remote_user:root
Hosts:taokey
Tasks
–name:install httpd
YUM:NAME=HTTPD state=present
–NAME:HTTPD Service
SERVICE:NAME=HTTPD Enabled=yes state=started
Ansible-playbook Web.yaml
[Email protected] ~]# Ansible-playbook Web.yaml

PLAY [Install httpd servive] **************************************************

Gathering FACTS ***************************************************************
OK: [192.168.1.82]

TASK: [Install] ***************************************************************
Changed: [192.168.1.82]

TASK: [Service on] ************************************************************
Changed: [192.168.1.82]

its features:
Yaml readability is good
Yaml and scripting language interactivity good
Yaml uses the implementation language data type
YAML has a consistent information model
Yaml is easy to implement
Yaml can handle the
Yaml expressive ability based on the stream, good extensibility

Name:john Smith
age:41
Gender:male
Spouse:
Name:jane Smith
age:37
Gender:female
Children:
–name:jimmy Smith
age:17
Gender:male
–name:jrnny Smith
Age-
Gender:female
The most critical parts of Yaml are lists, fields and mappings
all elements of the list use the "-" big head, for example: #A list of tasty fruits.
#A List of tasty fruits
-apple
-orange
-strawberry
-mango
Be sure to indent, sort by spaces, arrange neatly to think of the same level of
All elements of the list
Five, ansible basic elements
Facts
A mechanism to obtain its various related data before applying a host

Gets the variable information for a node:
[Email protected] ~]# ansible taokey-m Setup
A lot of data is returned directly, the function described here represents the various variables that are obtained from the specified host based on the setup

Before communicating with the other node, you can return some information that is available to the other node, which is obtained through facts
, and all of this information is a variable that can be called.
Some variables are useful, such as:
"ansible_os_family": "RedHat",
"Ansible_pkg_mgr": "Yum",
Decide which components to use to manage based on the results of the feedback

These things can be used to determine which tasks to apply
Get the other side of the thread, nothing more than the number of CPUs x2, then there is also reflected in the variable, as follows:
"Ansible_processor_cores": 1,
Types of variables
1. Custom variables
2.facts
3. Host/invertory variable host variables
4. Group variables, define multiple hosts as a single group, as follows:
[Taokey]
HostIP
[Taokey:vars]
Ntp_server=ntp.xxx.com
Group can be nested within a group
[Apache]
Host_1
Host_2
[Nginx]
Ngx1.xx.com

[Webserver:children]
Nginx
Apache

[Webservers:vars]
Ntp_server=ntp.xxx.com
So there are four types of common variables

Based on previous feedback to determine if the task was successfully executed
Gets to a specified result state before it runs, such as:
Tasks
-command:/bin/false
Register:result
Ingnore_errors:true
The second way:
–command:/bin/something
When:result|failed
When you can determine whether a previous result is expected, and then make a judgment based on the

Iteration:
You can also implement a traversal loop operation, as follows:
In host group Taokey, add 3 users
–name:add User
Remote_user:root
Hosts:taokey

Tasks
–name:add serveral Users
User:name={{items}} state=present
With_items:
–test4
–test5
–test6
The variable must be marked with {{items}}, and the calling variable must be with_items: to be assigned.

Let the individual perform the installation, but use Yum to install and only the other Package manager is Yum
Functions that need to be judged by variables
Related to: When
You first need to use Setup to get the other variable, and if you use a matching yum, then do it again.
[Email protected] ~]# ansible taokey-m Setup
Variables involved: "Ansible_pkg_mgr": "Yum",
Parameters involved:
When:ansible_pkg_mgr = = "Yum"
Operation is performed when the variable is yum
–name:install zsh
Remote_user:root
Hosts:a
Tasks
–name:install zsh
Yum:name=zsh state=present
When:ansible_pkg_mgr = = "Yum"

[Email protected] ~]# Ansible-playbook Zsh.yaml

PLAY [Install zsh] ************************************************************

Gathering FACTS ***************************************************************
OK: [192.168.1.82]

TASK: [Install zsh] ***********************************************************
Changed: [192.168.1.82]

PLAY RECAP ********************************************************************
192.168.1.82:ok=2 changed=1 unreachable=0 failed=0

Add: You can also use hashes when using With_items
User:name={{item,name}} state=present Groups={{item.group}}
With_items:
-{name: ' Test1′,gourp: ' Wheel '}
-{name: ' test2′,gourp: ' Root '

Through the above operation, it is simply by a number of lists, its main function is to pre-merge the host, first through the tasks defined role
Join together to accomplish a task

About Status Codes
If the exit status code for the command or script is not 0, then you can use
Mode instead
Tasks
–name:run this XXX
Shell:/usr/bin/somecommand | | /bin/true
#或:
Ignore_errors:true
Playbook Foundation Formation
Handlers Trigger action
Handlers is used to take certain actions when the resource of interest changes
Which means it only happens when it's triggered.

–name:conf
Remote_user:root
Hosts:taokey
Tasks
–name:install httpd
YUM:NAME=HTTPD state=present
When:ansible_pkg_mgr = = "Yum"
–name:conf file
Copy:src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
Notify:restart httpd
–name:start httpd
SERVICE:NAME=HTTPD Enabled=yes state=started

Handlers:
–name:restart httpd
SERVICE:NAME=HTTPD state=restarted
The above is the Yum Package Manager from the detection peer if Yum installs the httpd CP profile to the specified directory
Finally, the service is added to the boot key and started, and the service is restarted. However, there are several steps to repeat.

Improved:
If there is no need to re-install the Yum at the second execution time
One of the options in playbook-t
-T means that you can perform tasks that correspond to certain specific labels
Next, add a label for each step, as follows:
–name:conf file
Copy:src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
Tags:conf
Notify:restart httpd

Perform:
[Email protected] ~]# ansible-playbook http.yaml-t conf

PLAY [conf] *******************************************************************

Gathering FACTS ***************************************************************
OK: [192.168.1.82]

TASK: [conf file] *************************************************************
OK: [192.168.1.82]

PLAY RECAP ********************************************************************
192.168.1.82:ok=2 changed=0 unreachable=0 failed=0

If you have multiple tasks, you can also use a tags
are assigned tags:conf at each of the tasks.
It also splits a group of resources by way of a directory structure
As shown below:
Calling other Yaml files directly using Inculde
[email protected] ~]# cat Tasks/main.yaml
–name:install httpd
YUM:NAME=HTTPD state=present
When:ansible_pkg_mgr = = "Yum"

–name:conf file
Copy:src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
Tags:conf
Notify:restart httpd

–name:start httpd
SERVICE:NAME=HTTPD Enabled=yes state=started

Edit Httpd.yaml
[email protected] ~]# cat Httpd.yaml
–name:conf
Remote_user:root
Hosts:a
Tasks
Include:tasks/main.yaml

If there is no problem, change the httpd.conf configuration file and specify the label
A roles case is as follows:
Site.yml
Webservers.yml
Fooservers.yml
roles/
common/
files/
templates/
tasks/
handlers/
vars/
meta/
webservers/
files/
templates/
tasks/
handlers/
vars/
meta/

Steps to create a role
(1) Create a directory named after the roles;
(2) Create a directory named after each role name in the roles directory, such as webservers, etc.;
(3) The files, handlers, meta, tasks, templates, and VARs directories are created in each role-named directory, and directories that are not available can be created as empty directories or created;
(4) In the Playbook file, call each role;

Rolse
Define a directory again Rolse
[Email protected] httpd]# mkdir roles
Move the directory files to roles.
[Email protected] httpd]# MV Files/handlers/httpd.yaml tasks/roles/web/
[[email protected] httpd]# ls roles/
Web

The roles here represents the role directory, and this role name is called the Web
And in this role, it's usually named Site.yaml.
[Email protected] web]# MV Httpd.yaml Site.yaml

Defines a yaml for calling Site.yaml
[email protected] httpd]# cat Webserver.yaml
–name:web Service
Remote_user:root
Hosts:taokey
Roles
– Web

This article from the "Years in the passing, shining still in" blog, please be sure to keep this source http://taokey.blog.51cto.com/4633273/1719375

Use of Ansible playbooks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.