Ansible of Ad Hoc and Playbook

A, Ad-Hoc execution format

A command performing ad-hoc, the need for performing the following format:

ansible host module name or group -m -a 'module parameters' ansible parameter

And host group : is designated part in / etc / ansible / hosts, of course, is the use of dynamic Inventory script acquired from an external application in the host.

Module name : You can view by ansible-doc -l modules currently installed, default is not specified, use the command module, you can view specific /etc/ansible/ansible.cfg the "# module_name = command" section, the default modules in this modified configuration file;

Module parameters : You can view specific usage parameters and back through the "ansible-doc module name";

ansible parameters : You can view the information by ansible command's help, there are many parameters you can choose from, such as whether a password is required, whether sudo and so on.

二、playbook

1, playbook Profile

playbook literal meaning, that is, the script, reality, in Ansible, this time in accordance with screenplay by actor performances by the computer performance, computer installation, deployment of applications, provide external services, computer processing and organizing all sorts of things.

So why use the playbook it?

Perform some simple tasks, use ad-hoc command can easily solve the problem, but sometimes a facility is too complex and requires a large amount of operating time, ad-hoc command is not suitable, then it is best to use playbook, like execute shell commands and write shell scripts, as can be understood as batch jobs, but the playbook has its own syntax.

2, playbook file format

playbook document prepared by the YAML language. YAML is a similar XML, JSON markup language, YAML emphasis on data-centric, not to identify the language focus. Thus defined YAML itself is relatively simple, known as "a humane data format languages." First, learn to know YAML format, written on the back playbook helpful. The following is the playbook used to YAML format.

case sensitive

 use the space as a nested indentation tool, not allowed to use the Tab key to indent

 The number of spaces to indent does not matter, as long as the left elements are aligned to the same level

 Use "-" (dash) + single space: represents a single list item

 Use ":" (colon) + space: represents a single key-value pair

 using "{}" indicates a key value table

playbook file is parsed by the command ansible-playbook, ansbile-playbook playbook command file contents are sequentially performed according to an order from top to bottom. Meanwhile, many features PlayBook created, it may allow transmission of a command state to a following instruction, it may fetch a file from one machine along with the contents of the variable, and then used in another machine, this so that the playbook can achieve some complex deployment mechanism, which is ansible command can not be achieved.

3, playbook configuration

playbook is a list of one or more "play" component. The main function is to play, will host a pre-consolidated into the group dressed as defined in advance by ansible role. Multiple organizations play in a playbook you can let them in conjunction with the mechanism previously choreographed by up to complete a series of complex tasks.

It mainly has the following four parts:

target section : To perform a remote host definition of group playbook

variable part of the variable you want to use the definition playbook runtime:

part of the task : the task list is defined to be executed on the remote host

handler section : Defining task after task execution is completed need to call

And its corresponding directory layer five (optionally variable), respectively:

vars variable layer

tasks Task Level

handlers trigger conditions

files file

template template

4. Detailed playbook of grammar

(1)、Hosts和Users

The purpose of every play in the playbook is to allow the host to perform a certain task or to a specified user.

hosts: used to specify the host to perform a given task, each playbook must specify hosts, hosts can also use the wildcard format. Host or host group specified in the inventory list, you can use the system default / etc / ansible / hosts, you can also edit your own, at run time plus the -i option to specify the location of the custom host list. When running the manifest file, - list-hosts option displays the host will process those involved in the implementation of the task.

REMOTE_USER : used to specify the user to perform tasks on remote hosts. You can specify any user, you can also use sudo, but users must have the appropriate permissions to perform the task.

(2), the task list (tasks list)

play the main part is the task list.

The second start of each task in the task list in order, one by one on all hosts specified in the hosts performed, to complete the first task on all hosts before. When you run a top-down playbook, if she had an error occurs, all tasks are rolled back, and therefore needs to be performed once again after correcting the playbook.

Task object is performed using the specified parameter module, and the module parameters in a variable can be used. Module is executed (Idempotence; that is, a command is executed even if one or more times, the result is the same) idempotent, meaning that multiple executions is safe, because the results are consistent. tasks to be performed and the module contains the name, the name is optional, only to facilitate users to read, it is recommended to add the phrase, the module is necessary, but also to give the corresponding parameter module.

Defined tasks recommended module: options "format, for example:

service: name=httpdstate=running

(3)、handlers

To take certain action when a resource concern changes. handlers is and "notify" used in conjunction.

A one-time "notify" This action can be used at the end of each play is triggered, there are many times when to avoid change, every time the specified action by "notify", completed only after the change occurs in all perform the specified operation.

Operations listed in notify called handler, that is used to notify invoke operations defined handler.

Note: Like "-name" contents defined in and must notify the handlers defined, so as to achieve the effect of trigger, otherwise it will not take effect.

(4)、tags

tags used to let users choose to run or skip sections of code in the playbook. ansible idempotent, so no change will automatically skip portion; however playbook task when a relatively long time, one determines whether a portion of each of the change, but also take a long time. Thus, if it is determined there is no change certain portions, it can skip code snippets by tags.

5, Playbook perform analytical results

Run playbook file, the contents of the output in JSON format ansible-playbook. Composed of different colors and for easy identification. In general, the output content:

Green represents executed successfully, but the system remains intact

Yellow represents the state of the system is changed, that is, an operation performed to take effect

Red means failed, an error message will be displayed.

Example:

- name: create user

hosts: 172.16.213.233

user: root

gather_facts: false

of which :

- user1: "testuser"

tasks:

- name: start createuser

user: name="{{ user1 }}"

Resolution:

The above playbook is the function of a new user:

The name parameter playbook an overview of the functions implemented in the later execution, will output the value of the name;

hosts parameter specifies the host on which to operate.

user parameter specifies what a user logs into a remote host to operate.

When gather_facts parameter specifies the need to use the following tasks before execution, whether to perform the setup module acquires the host-related information, which will be used to obtain information setup in a later task.

vars parameter specifies the variable referred to herein as a word user1 variable whose value testuser, to be noted that the variable values ​​must use quotation marks.

tasks assigned a task, following its name parameter is also a description of the tasks in the implementation process will be printed. user is a module behind the user name of a user module in a parameter, and increased user name user1 called values ​​above variables.

6, collect facts information

Ansible facts component is a function of the managed equipment for collecting information. You can use the module to check the machine's setup information all facts, facts, including the remote host information release, IP address, CPU core number, system architecture, host name, etc., can use the filter to view specific information. Facts information is packaged in the entire data structure json format.

[root@ansibleplaybook]# ansible 172.16.213.233 -m setup

All data formats are JSON format, facts also support the view specific information, as follows:

[root@ansibleplaybook]# ansible 172.16.213.233 -m setup -a 'filter=ansible_all_ipv4_addresses'

playbook during execution, the default first tasks is to gather information on the distal end of the tube facts host, if the task does not use the back of the setup information acquired may be prohibited ansible collect facts, the instruction following the hosts playbook settings: you can "gather_facts false". The default gather_facts is true.

facts are often used in conditional statements and templates which can also be used to create dynamic host groups based on specified criteria.

Guess you like

Origin www.cnblogs.com/yxf-/p/11494096.html