Playbook is a list of one or more "play"
The main function of play is to dress up a pre-set host as a role defined in advance through the task in ansible. Fundamentally, the so-called task is nothing more than a call to Ansible's module. By organizing multiple play in one playbook, you can connect them to run in a pre-programmed mechanism.
Playbook use Yaml language to write each ansible Playbook is a yaml format file, so to learn to write a script (Playbook), we first understand the basic use of YAML syntax
One, Yaml introduction
YAML is a highly readable format for expressing data sequences in Ali. Yaml references a number of other languages, including: XML,
C language, Python, Perl, e-mail format RFC2822, and so on. Clark Evans published the first of its kind in 2001
Language, in addition ingy d?t NET and Oren Ben-kiki are also common designers of this language
Second, the characteristics
Good readability of Yaml
The interactivity between Yaml and scripting language is good
YAML uses the data type of the implementation language
YAML has a consistent information model.
Yaml is easy to implement
YAML can process based on streams
Yaml has strong expressive ability and good extensibility
YAML Syntax Format:
1. In a single file, multiple files can be distinguished by three consecutive hyphen (--). In addition, there is a choice of three consecutive dot numbers
( ... ) Used to indicate the end of a file
2. The second line begins to write the contents of the playbook normally, it is generally suggested that the Playbook function
3. Comment Code using the # number
4. Indentation must be uniform, not space and tab mix
5. The level of indentation must also be consistent, the same indentation represents the same level, the level of the program discrimination configuration is by indenting the knot
Line-up to achieve
6.YAML file content is consistent with the Linux system case-sensitivity, is case-sensitive, and the k/v value is case-insensitive
The value of the 7.k/v can be written in either the same or a newline. Peer use: Separate
8.V is a string, but it's another list.
9. A complete code block function requires minimum elements to include Name:task
10. A name can consist of only one task
11.YAML file extension is usually yml or Yaml
Introduction to YAML Syntax:
List
List: Lists, all of whose elements begin with "-"
Example:
# A List of tasty fruits
–apple
–orange
–strawberry
–mango
Dictionary
Dictionary: A dictionary, usually consisting of multiple keys and value
Example:
—
# an Employee record
Name:example Developer
Job:developer
Skill:elite
You can also place key:value in {} to represent, separating multiple key:value
Example:
—
# an Employee record
{name:example Developer, job:developer, Skill:elite}
Third, the script playbook
Playbook Core elements:
hosts: List of remote hosts executed
tasks: Task List
varniables: Built-in variables or custom variables are called in Playbook
Templates: Templates that can replace variables in template files and implement some simple logic files
handlers: Used in conjunction with notify, the action triggered by a specific condition, satisfies the condition to execute, otherwise does not execute
tags: a tag that specifies a task execution that selects some code in the run playbook. The ansible has idempotent nature. So
will automatically skip the parts that are not changed. At this point, if you are sure that it has not changed, you can skip these code snippets through tags
Hosts:
role:each play in playbook is intended to allow one or more hosts to use a specified user
Identity execution tasks. Hosts are used to specify the host to perform the specified tasks, which must be defined in the host manifest
Can be in the following form:
One.example.com
One.example.com:two.example.com
192.168.1.50
192.168.1.*
Websrvs:dbsrvs Two-group set
Websrvs:&dbsrvs intersection of two groups
Webservers:!dbsrvs in Websrvs group, but not in Dbsrvs group
Example:
–hosts:websrvs:dbsrvs
Remote_user
function: can be used in host and task. You can also perform tasks on the remote host by specifying how it is sudo.
It can be used for play global or a task, and even when sudo is specified using Sudo_user to specify sudo
User
–hosts:websrvs
Remote_user:root
Tasks
–name:test Connection
Ping:
remote_user:magedu
Sudo:yes default sudo is root
Sudo_user:wang sudo to Wang
tasks:
Role: Task List
Format: module:arguments
Note:the shell and command modules are followed by commands, not key=value
Check playbook:
Ansible-playbook-c file.yml
How to run Playbook:
Ansible-playbook <filrname.yml> ... [Options]
Options
–check only detects changes that may occur, but does not actually operate
–list-hosts List the hosts running the task
–limit Host list is only performed for hosts in the host list
-V,-VV,-VVV Show detailed procedures
One of the simplest playbook to include is the base component host, Remote_user, tasks
Example 1:
Vim Http.yml
—
–hosts:websrvs
Remote_user:root
Tasks
–name:create New File
File:name=/data/newfile State=touch
–name:create New File
User:name=test2 System=yes Shell=/sbin/nologin
–name:install Package
Yum:name=httpd
–name:copy Index
Copy:src=/var/www/html/index.html dest=/var/www/html/
–name:start Service
SERVICE:NAME=HTTPD state=started Enabled=yes
Example: Create a simple script and execute
Tags tags
Task The task can also be labeled with "tags", which can then be invoked with the-T specified on the ansible-playbook command
Example 2: Using tags
Vim Http.yml
—
–host:websrvs
Remote_user:root
Tasks
–name:install httpd Package
Yum:name=httpd
Tags:inshttpd
–name:copy conf File
Copy:src=files/httpd.conf Dest=/etc/httpd/conf/backup=yes
Tags:cphttpd
–name:start Service
SERVICE:NAME=HTTPD STATE=STARTD Enable=yes
Tags:rshttpd
ansible-playbook-t rshttpd httpd.yml separate execution rshttp
Linux Nine Yin canon of nine yin Bones claw fragment 5 (ansible usage two playbook and YAML syntax)