1. Ad-Hoc problem
Learning AD-HOC, we found that AD-HOC can only execute simple commands on the managed node each time. In actual work, some complex operations are incapable of execution, so ansible introduced Playbook to help solve such complex problems.
2. What is Playbook
Playbook is usually translated into a script by everyone.
You can think of it as a language customized by Ansible (Playbook can be compared to a shell in Linux, and Module in Ansible can be compared to various commands in Linux.)
Three. YAML
1. Features
- Use # as comment
- End with .yml or .yaml
- Start with—, end with..., but both the start and end flags are optional
2. Basic grammar
- Case Sensitive
- Use indentation to indicate hierarchical relationships
- Whether to use the Tab key or spaces when indenting must be unified, and spaces are recommended.
- Elements of the same level must be aligned to the left
There are three data structures supported by YAML
- String
- List
- dictionary
2.1 String
---
#YAML 中的字符串可以不使用引号,即使里面存在空格的时候,当然了使用单引号和双引号也没有错。
this is a string
'this is a string'
"this is a string"
#YAML 中若一行写不完你要表述的内容的时候,可以进行折行。写法如下:
long_line: |
Example 1
Example 2
Example 3
#或者 l
ong_line: >
Example 1
Example 2
Example 3 ...
2.2 List
---
#若熟悉 Python 的话, 可以认为它就是Python中的List ,若熟悉 C 语言的话, 可以认为它是 C 中的数组。
#如何定义: 以短横线开头 + 空格 + 具体的值
- red
- green
- blue
#以上的值假如转换成 python 的 List 会是这样:
#['red', 'green', 'blue']
...
2.3 Dictionaries
---
#若熟悉 Python 的话, 可以认为它就是 Python 中的 Dict
#如何定义: key + 冒号(:) + 空格 + 值(value), 即 key: value
name: Using Ansible
code: D1234
#转换为 python 的 Dict
#{'name': 'Using Ansibel', 'code': 'D1234'}
...
2.4 Hybrid structure
Above, all the basic knowledge points for YAML are introduced. But in daily life, the data structure that is often required will be particularly complex, and it may be a combination of string, list, and dictionary. Here is a small example:
Everyone has gone to school, and they all know that they are in class as a unit. We use the form of lists and dictionaries to describe the composition of a class.
---
class:
- name: stu1
num: 001
- name: stu2
num: 002
- name: stu3
num: 003
{
'class': [{
'name': 'stu1', 'num': 1},{
'name': 'stu2', 'num': 2},...]}
...
字典中有列表,列表中有字典
2.5 Verify YMAL syntax
// Verify the YAML file through Python's YAML module, and report an error if it is incorrect. If it is correct, the content in YAML will be output.
//Be sure to install the yaml software package when using it.
python -c'import yaml,sys; printyaml.load(sys.stdin)' myyaml.yml
python3 -c'import yaml,sys;print(yaml.load(sys.stdin))'myyaml.yml
Example
// 正确的情况
cat myyaml.yml
---
- red
- green
- blue
...
#python -c 'import yaml,sys; print yaml.safe_load(sys.stdin)' < myyaml.yml
['red', 'green', 'blue']
// 错误的情况, 将YAML文件写错
#cat myyaml.yml
---
- red
- green
-blue
...
#python -c 'import yaml,sys; print yaml.load(sys.stdin)' < myyaml.yml
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/yaml/__init__.py", line 71, in load
return loader.get_single_data()
File "/usr/local/lib/python2.7/site-packages/yaml/constructor.py", line 37, in get_single_data
node = self.get_single_node()
...
...
Four. Playbook preparation
1. Definition of Play
1. Every Play starts with a dash
2. Every Play is a YAML dictionary format
According to the above two Play rules, a hypothetical Play should look like the following
---
- key1: value1
key2: value2
key3: value3
...
Since a Playbook is composed of one or more Plays, the structure of a Playbook with multiple Plays should look like the following
---
#一个含有3个Play 的伪PlayBook构成
- key1: value1
key2: value2
key3: value3
- key4: value1
key5: value2
key6: value3
- key1: value1
key2: value2
key3: value3
...
2.Play attributes
Based on Play in the previous section, each key in Play, such as key1, key2, etc.; these keys are defined as attributes of Play in PlayBook.
These attributes have special meanings, and we cannot customize the attributes of Play at will
Common attributes
- name attribute, the name of each play, can be customized
- The hosts attribute, the managed server involved in each play, is the same as the asset selector in ad-hoc
- tasks attribute, the specific tasks to be completed in each play, expressed in the form of a list
- become attribute, if you need to raise the right, add become related attributes
- become_user attribute, if the privilege is raised, to which user
- The remote_user attribute specifies the user connected to the remote node, which is the user who performs specific operations on the remote server. If not specified, the user currently executing ansible playbook will be used by default
3. A complete script
According to the real attributes introduced in the previous section, a Playbook with a Play should look like the following
---
- name: the first play example
hosts: webservers
remote_user: root #远程用户
tasks:
- name: install nginx package #任务名称
yum: name=nginx state=present
- name: copy nginx.conf to remote server
copy: src=nginx.conf dest=/etc/nginx/nginx.conf
- name: start nginx server
service:
name: nginx
enabled: true
state: started
4. Various ways of writing tasks in tasks attribute
#以启动 nginx 服务,并增加开机启动为例
#一行的形式:
service: name=nginx enabled=true state=started
#多行的形式:
service: name=nginx
enabled=true
state=started
#多行写成字典的形式:
service:
name: nginx
enabled: true
state: started
5. Playbook with multiple plays
---
- name: manage web servers
hosts: webservers
remote_user: root
tasks:
- name: install nginx package
yum: name=nginx state=present
- name: copy nginx.conf to remote server
copy: src=nginx.conf dest=/etc/nginx/nginx.conf
- name: start nginx server
service:
name: nginx
enabled: true
state: started
- name: manager db servers
hosts: db_servers
tasks:
- name: update database confg
copy: src=my.cnf dest=/etc/my.cnf
6. How to check the syntax of Playbook
The following verification method can only verify whether the playbook is correct, but not whether the YAML file is syntactically correct
#ansible-playbook -i hosts myplaybook.yml --syntax-check #语法检查
Because the playbook belongs to the YAML format, we can also use the check YAML syntax format to check the syntax correctness of the playbook
#python -c 'import yaml,sys; print yaml.safe_load(sys.stdin)' < myplaybook.yml
7. How to run PlayBook
ansible-playbook -i hosts myplaybook.yml
8. How to single step to follow the debug playbook
// 执行Task中的任务,需要手动确认是否往下执行。
#ansible-playbook -i hosts myplaybook.yml --step
9. How to test and run the playbook
The test run will execute the entire PlayBook, but all the actions in the Task will not be executed on the remote server, and all executions are simulated actions.
#ansible-playbook -i hosts myplaybook.yml -C
// -C 为大写的字母 C