当前位置:网站首页>Automatic operation and maintenance sharp weapon ansible Playbook
Automatic operation and maintenance sharp weapon ansible Playbook
2022-07-06 17:33:00 【Tang Monk riding white horse】
One 、 Ad-Hoc The problem of
Through to AD-HOC Learning from , We found that AD-HOC Only simple commands can be executed on the managed node at a time .
And in daily work , We are often faced with a series of complex operations , For example, we may need to install software 、 Update configuration 、 A combination of a series of operations such as starting a service . And at that point it goes through AD-HOC It's a little too much to complete the task .
In this case ,Ansible The introduction of PLAYBOOK To help us solve such a complex problem .
Two 、 PlayBook What is it?
Playbook It is also usually translated into scripts . You can think of it as Ansible Customized one ⻔ Language ( Can be Playbook Compared to the Linux Medium shell, and Ansible Medium Module It can be compared to Linux Various commands in .)
3、 ... and 、 YAML Study
PlayBook follow YAML The grammar of . So I'm learning PlayBook Before , We have to figure it out first YAML Related knowledge .
1、YAML characteristic
YAML file
- With # Is the annotator
- With .yml perhaps .yaml ending
- With - - - Start , With … end , But the start and end flags are optional
2、 Basic grammar
- Case sensitive
- Use indentation to indicate hierarchy
- When indenting, use Tab Keys or spaces must be unified , Spaces are recommended .
- Elements of the same level must be left aligned ⻬ that will do
YAML There are three supported data structures
- character string
- list
- Dictionaries
2.1、 character string
---
# YAML Strings in can be without quotation marks , Even if there are spaces in it , Of course, there is nothing wrong with using single quotation marks and double quotation marks .
this is a string
'this is a string'
"this is a string"
# YAML If you can't finish what you want to say in one line , You can break lines . It is written as follows :
long_line: |
Example 1
Example 2
Example 3
# perhaps
long_line: >
Example 1
Example 2
Example 3
...
2.2、 list
---
# If familiar Python Words , We can think of it as Python Medium List , If familiar C The language of words , You can think of it as C In the array .
# How to define : Start with a dash + Space + Specific value
- red
- green
- blue
- # If the above value is converted to python Of List Is this :# ['red', 'green', 'blue']
2.3、 Dictionaries
---
# If familiar Python Words , We can think of it as Python Medium Dict
# How to define : key + The colon (:) + Space + value (value), namely key:value
name: Using Ansible
code: D1234
# Convert to python Of Dict# {'name': 'Using Ansibel', 'code': 'D1234'}...
2.4、 Mixed structure
---class:
- name: stu1
num: 001
- name: stu2
num: 002
- name: stu3
num: 003
# {'class': [{'name': 'stu1', 'num': 1},{'name':'stu2', 'num': 2},...]}
2.5、 verification YAML grammar
// take YAML file , adopt Python Of YAML Module validation , If it is incorrect, an error will be reported . If it is correct, it will output YAML Contents of Li .
// Pay attention to the use of , Be sure to install yaml software package .
python -c 'import yaml,sys; printyaml.load(sys.stdin)'<myyaml.yml
python3 -c 'import yaml,sys;print(yaml.load(sys.stdin))'<myyaml.yml
Four 、 Playbook Compiling
4.1、 Common properties
- name attribute , Every play Name
- hosts attribute , Every play The managed server involved , Same as ad-hoc Asset selector in
- tasks attribute , Every play Specific tasks to be completed in , Express... In the form of a list
- become attribute , If you need to raise the right , Then add become Related properties
- become_user attribute , If the right is raised , To which user does the right go
- remote_user attribute , Specify the user who connects to the remote node , Is the user who performs specific operations on the remote server . If not specified , The current execution is used by default ansible Playbook Users of
4.2、 A complete script
According to the real attributes introduced in the previous section , One contains one Play Of Playbook It 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.3、tasks Multiple ways to write tasks in properties
# Start up nginx service , And add startup as an example
# One line form :
service: name=nginx enabled=true state=started
# Multiline form :
service: name=nginx
enabled=true
state=started
# Write multiple lines in the form of a dictionary :
service:
name: nginx
enabled: true
state: started
4.4、 Have more than one Play Of Playbook
---
- 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.con fdest=/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
4.5、 How to Playbook Check Syntax
The following verification method , Can only verify PlayBook Whether it is right , It cannot be verified YAML Whether the file syntax is correct .
[[email protected] ~]# ansible-playbook -i hosts myymal.yml --syntax-check
because PlayBook Belong to YAML Format , We can also use check YAML The syntax format of the method is checked PlayBook Grammatical correctness of .
[[email protected] ~]# python -c 'import yaml,sys; print yaml.safe_load(sys.stdin)' < myymal.yml
4.6、 How to run PlayBook
# ansible-playbook -i hosts myplaybook.yml
4.7、 How to follow the debugging step by step PlayBook
// perform Task The task , You need to manually confirm whether to proceed .
# ansible-playbook -i hosts myplaybook.yml --step
4.8、 How to test and run PlayBook
A test run is a complete execution of PlayBook , But all Ta s k None of the actions in will be performed on the remote server , All execution is simulated behavior .
# ansible-playbook -i hosts myplaybook.yml -C
// -C Capital letters C
边栏推荐
- 關於Stream和Map的巧用
- Take you hand-in-hand to do intensive learning experiments -- knock the level in detail
- 灵活报表v1.0(简单版)
- Re signal writeup
- Learn the wisdom of investment Masters
- PostgreSQL 14.2, 13.6, 12.10, 11.15 and 10.20 releases
- JVM 垃圾回收器之Garbage First
- JVM之垃圾回收器下篇
- 1. JVM入门介绍
- Application service configurator (regular, database backup, file backup, remote backup)
猜你喜欢
Take you hand-in-hand to do intensive learning experiments -- knock the level in detail
How does wechat prevent withdrawal come true?
1. Introduction to JVM
C version selenium operation chrome full screen mode display (F11)
yarn : 无法加载文件 D:\ProgramFiles\nodejs\yarn.ps1,因为在此系统上禁止运行脚本
Flink parsing (IV): recovery mechanism
当前系统缺少NTFS格式转换器(convert.exe)
07 personal R & D products and promotion - human resources information management system
复盘网鼎杯Re-Signal Writeup
C#版Selenium操作Chrome全屏模式显示(F11)
随机推荐
[reverse intermediate] eager to try
信息与网络安全期末复习(基于老师给的重点)
关于Stream和Map的巧用
[reverse] repair IAT and close ASLR after shelling
Wu Jun's trilogy experience (VII) the essence of Commerce
吴军三部曲见识(五) 拒绝伪工作者
How does wechat prevent withdrawal come true?
05个人研发的产品及推广-数据同步工具
C#版Selenium操作Chrome全屏模式显示(F11)
Akamai 反混淆篇
List set data removal (list.sublist.clear)
MySQL basic addition, deletion, modification and query of SQL statements
Flexible report v1.0 (simple version)
C#WinForm中的dataGridView滚动条定位
Flink 解析(二):反压机制解析
Idea breakpoint debugging skills, multiple dynamic diagram package teaching package meeting.
Flink 解析(三):内存管理
Only learning C can live up to expectations Top1 environment configuration
手把手带你做强化学习实验--敲级详细
ByteDance overseas technical team won the championship again: HD video coding has won the first place in 17 items