Automated O & M-ansible (Part 3: playbook Introduction)

Source: Internet
Author: User
Tags vars custom name ansible modules

Two previous articles talk about ansible deployment and use of ansible modules respectively. They have an initial understanding of ansible. This article focuses on introducing playbook.
For details about ansible deployment, click ansible first: Deployment
Ansible module celebration: ansible Part 2: module usage

Playbook Introduction

Playbook is a list composed of one or more playbooks. Its main function is to merge roles defined by tasks into a group for unified management, that is, multiple playbooks are run in a playbook by calling the asible template of the task.

Playbook consists of the following parts: (1) Tasks: task, an operation completed by calling the module; (2) variables: variable; (3) templates: Template; (4) handlers: A processor that triggers the operation when a condition is met. (5) Roles: role.
Yaml Introduction

Yaml is a format used to express the order of data. It is highly readable because the meal is stuck in multiple languages. It can be considered as a language format. It must be written in this format to take effect.
Its features:
1) It is readable and easy to implement;
2) Strong expressiveness and good scalability;
3) Good interaction with the scripting language;
4) there is a consistent information scale;
5) it can be processed in a stream.


Common Data Types of yaml

Yml has two common data types: List and dictionary.
(1) List
All the elements in the list start with "-", and the columns are as follows:


(2) Dictionary
A dictionary is identified by key and value, for example:

name: chenglongJob: xuexiSkill: Elite

You can also place the key: Value in {} for identification, for example:

{ name: Exanple Developer,Job: Developer,Skill: Elite }

Introduction to basic ansilble Elements

(1) Inventory (host List)

To manage hosts more conveniently, ansible groups managed hosts in the host list. The default host list is located in the/etc/ansible/hosts file.

1) host variables:
You can add host variables when defining hosts for later use in playbook, for example:

[aaa_servers] http_port=80 http_port=8080 maxRequestsChild=900

2) group variables
Group variables are variables that can be directly used in palybook by setting hi to a specified host, such:


3) group nesting
Groups in inventory can also be nested in other groups, or you can specify variables to the hosts in the group, but these variables can only be used in ansible-playbook tools, direct use of the ansible tool is not supported, such:

[aaa] http_port=80 http_port=8080 maxRequestsChild=900[bbb][ccc:ddd]aaabbb

(2) variables

In ansible, variable names can only consist of letters, numbers, and underscores, and can only start with a letter. You can use two methods to pass the ansible variable.

1) Pass the variable through the command line.
When running a playbook, you can use the command line to pass variables to playbook, for example:

            ansible-playbook test.yml -e "http_port=8080"

2) It is defined and called when writing playbook.

-Hosts: AAA remote_user: Root vars: ** // definition variable **-package: Apache // variable definition package-server: httpd // variable definition server tasks:-Name: check latest Yum: Name ={{ package} state = Latest // call the variable package-Name: Start apache service: name = {server} state = started // call the variable Server

(3) condition test

1) when is the condition judgment. If the condition you give is true, it will execute the task in tasks. If it does not meet the conditions, it will not execute.

-Hosts: AAA remote_user: Root tasks:-Name: install Apache Yum: Name = httpd when: -ansible_distribution = "centos" // meets condition 1-ansible_distribution_major_version = "6" // meets condition 2

2) iteration: When you need to execute repeated tasks, it is very troublesome to write one by one. In this case, you need to use iteration to define the actions that need to be repeated as item variables and reference them, then, use the with_items statement to specify the element.

- hosts: aaa  remote_user: root  tasks:  - name: install pachages    yum: name={{ item }} state=latest    with_items:    - httpd    - openssh    - mysql
Hosts and users

Palybook is designed to allow some hosts to execute tasks as a user. The host used for task execution is defined by hosts, and the execution user is defined by remote_user. As follows:

- hosts: aaa  remote_user: root

Remote_user can define a user to run and execute on the managed host through sudo.

-Hosts: AAA remote_user: Root become: Yes become_user: switch from Oracle to Oracle
Task List and action
  • > The main body of play is the task list ). Tasks in the task list are executed one by one on all hosts specified in hosts. If any errors occur, they are rolled back.
  • > A task executes the module according to the specified parameters. Each task uses the name to output the running result of palybook. Generally, the output content describes the steps for executing the task, if the output result is not provided.
  • Example:

-Hosts: AAA remote_user: Root tasks:-Name: Install pachages // name: custom name Yum: Name = httpd state = Latest // Yum: Install the HTTPd service, check whether the latest version-Name: Disable SELinux command:/sbin/setenforce 0 // disable SELinux-Name: Disable firewall command: systemctl stop firewalld // disable firewall-Name: APACHE service: name = httpd enabled = true state = started // start the HTTPd service and set it to enable self-starting ansible-playbook-c aaa. yml checks whether the syntax and process are correct and does not actually execute it!

Handlers Introduction

Handlers is used to take actions when the monitored resource changes. The accumulative operations in handler y become handler, that is, the operations defined in Handler are required in handler y. The notify action is triggered at the end of each play, and the specified operation is executed only once after all changes are completed.

Templates Introduction

Templates can be viewed as a compiled template file used to generate the target text and pass the variable to the template to replace the tag in it.

Prepare a configuration file template in. J2 format. Then, edit the template to unify the template.
[[Email protected] ~] # Vim/etc/httpd/CONF/httpd. conf. J2

- hosts: aaa  remote_user: root  tasks:  - name: install pachages    yum: name=httpd state=latest  - name: disable selinux    command: /sbin/setenforce 0  - name: file to httpd    template: src=/etc/httpd/conf/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf

Tages Introduction

If you modify the playbook multiple times, you can use tags to allow the user to select to skip the unchanged code and only run the Code marked by tags. It is a "tag ".

Automated O & M-ansible (Part 3: playbook Introduction)

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: 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.