当前位置:网站首页>[ansible] playbook explains the execution steps in combination with the project
[ansible] playbook explains the execution steps in combination with the project
2022-08-02 08:21:00 【bigdata Wang Yi】
文章目录
The main part of this article is:8playbook案例——结合项目,带你快速了解playbook编写
1.playbook介绍
playbook是由一个或多个"play"组成的列表,play的主要功能在于将预定义的一组主机,装扮成事先通过ansible中的task定义好的角色.
个人理解:就是在taskDefine the execution process,然后执行ansibletime to define how to execute.
2.playbook执行流程
ansible命令调用yaml文件(playbook文件) =》 一个playbook包含多个play(这是个plays列表) ,每个play都有一个taskrelative to the operation =》 然后调用moudle模块,应用在主机清单上(A task is a pair ansible 模块的调用,A host inventory is the host or device to control) =》通过sshControl other hosts or devices
3.yaml优点
YAML是一个可读性高的用来表达资料序列的格式.它具备以下特性:
·YAML的可读性好
·YAML和脚本语言的交互性好
·YAML使用实现语言的数据类型
·YAML有一个一致的信息模型
·YAML易于实现
·YAML可以基于流来处理
·YAML表达能力强,扩展性好
4.playbook核心元素
Hosts 执行的远程主机列表(应用在哪些主机上)
Tasks 任务集
Variables 内置变量或自定义变量在playbook中调用
Templates模板 可替换模板文件中的变量并实现一些简单逻辑的文件
Handlers和notify结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行
tags标签 指定某条任务执行,用于选择运行playbook中的部分代码.
ansible具有幂等性,因此会自动跳过没有变化的部分,
即便如此,有些代码为测试其确实没有发生变化的时间依然会非常地长.
此时,如果确信其没有变化,就可以通过tags跳过此些代码片断
ansible-playbook -t tagsname useradd.yml
5.playbook的运行方式
格式:ansible-playbook <filename.yml> … [options]
通过执行ansible-playbook -h You can see what parameters are available
#ansible-playbook常用选项:
--check or -C #只检测可能会发生的改变,但不真正执行操作
--list-hosts #列出运行任务的主机
--list-tags #列出playbook文件中定义所有的tags
--list-tasks #列出playbook文件中定义的所以任务集
--limit #主机列表 只针对主机列表中的某个主机或者某个组执行
-f #指定并发数,默认为5个
-t #指定tags运行,运行某一个或者多个tags.(前提playbook中有定义tags)
-v #显示过程 -vv -vvv更详细
6.playbook的语法
①playbook使用yaml语法格式,后缀可以是yaml,也可以是yml.
②在单一一个playbook文件中,可以连续三个连子号(—)区分多个play.还有选择性的连续三个点好(…)用来表示play的结尾,也可省略.
③次行开始正常写playbook的内容,一般都会写上描述该playbook的功能.
④使用#号注释代码.
⑤缩进必须统一,不能空格和tab混用.
⑥缩进的级别也必须是一致的,同样的缩进代表同样的级别,程序判别配置的级别是通过缩进结合换行实现的.
⑦YAML文件内容和Linux系统大小写判断方式保持一致,是区分大小写的,k/v的值均需大小写敏感.
⑧k/v的值可同行写也可以换行写.同行使用:分隔,v可以是个字符串,也可以是一个列表.
⑨一个完整的代码块功能需要最少元素包括 name: task.
7.playbook的hosts设置
在一个playbook开始时,最先定义的是要操作的主机和用户
- hosts: 192.168.1.31
remote_user: root
除了上面的定义外,还可以在某一个tasks中定义要执行该任务的远程用户
- name: run df -h
remote_user: test
shell: name=df -h
还可以定义使用sudo授权用户执行该任务
tasks:
- name: run df -h
sudo_user: test
sudo: yes
shell: name=df -h
8.playbook案例——结合项目
项目中playbook目录
I think it's a direct look at a projectdemo,More helpful for quick understandingplaybook的编写.文中的{ {}}is the variable we passed in,I use others#编写说明
以下内容nameis the name of the custom step,tagsis the label for this step,可以通过 -t 和–skip-tagsto decide which tag to execute.执行install的tag,ansible-playbook -t install install.yml或者不执行install的tag,ansible-playbook --skip-tags install install.yml
主要看name和install之间的部分~~~
案例 install.yaml
#The three horizontal lines areyamlSeparator between files
---
#创建文件夹
- name: create install directory
file: path={
{
install_dir }} state=directory
tags: "install"
# copy的命令,复制文件,srcis the source path,dest是目标路径
- name: copy package
copy: src={
{
package_dir }}/{
{
package_name }}-{
{
package_version }}.{
{
compress_suffix }} dest={
{
install_dir }} mode=0755
tags: "install"
# statDetermine the folder status(文件是否存在):true和false,命名为file_status,当然也可以是file_status_a,file_status_b......
- name: check hdfs installed
stat:
path: "{
{ install_dir }}/{
{ package_name }}-{
{ package_version }}"
register: file_status
tags: "install"
# 当file_status为false,当文件夹不存在,进行解压,{
{ install_dir }}/{
{ package_name }}-{
{ package_version }}.{
{ compress_suffix }}In our program it isgtarclass archive
# mode是赋权
- name: unachieve package
unarchive: src={
{
install_dir }}/{
{
package_name }}-{
{
package_version }}.{
{
compress_suffix }} dest={
{
install_dir }} copy=no mode=0755
when: file_status.stat.exists == False
tags: "install"
# 软连接,path路径 -> src路径
- name: Symlink install directory
file: src={
{
install_dir }}/{
{
package_name }}-{
{
package_version }} path={
{
install_dir }}/yarn state=link
tags: "install"
# Templates 模板文件的使用,The specific directory can refer to my screenshot
#该步骤主要是将playbook中的.j2文件复制到destthe specified directory and rename it.
# 该步骤是将env.j2复制到{
{ install_dir }}/yarn目录下并命名为env
- name: create hadoop env file
template: src=env.j2 dest={
{
install_dir }}/yarn/env
tags: "install"
# 描述同上,该步骤是将nodemanager.service.j2文件复制到/usr/lib/systemd/system目录下并命名为nodemanager.service
- name: add to systmed
template: src=nodemanager.service.j2 dest=/usr/lib/systemd/system/nodemanager.service
when: "package_version.startswith('3.')"
tags: "install"
# 该步骤是使用shell命令
- name: append yarn-env.sh
shell: result=`cat {
{
install_dir }}/yarn/etc/hadoop/yarn-env.sh | grep "nodemanager.yaml"`;if [[ -z "$result" ]];then echo -e export YARN_NODEMANAGER_OPTS='"' -javaagent:{
{
install_dir}}/monitor/jmx_prometheus_javaagent-0.16.1.jar={
{
prometheus_port }}:{
{
install_dir }}/monitor/hadoop/nodemanager.yaml '$YARN_NODEMANAGER_OPTS''"' >> {
{ install_dir }}/yarn/etc/hadoop/yarn-env.sh; fi tags: "install" # 该步骤是使用shell命令,whenis to join the judgment condition,When passed in variablespackage_version以2Executed at the beginning - name: append yarn-env.sh shell: result=`cat {
{
install_dir }}/yarn/etc/hadoop/yarn-env.sh | grep "nodemanager.yaml"`;if [[ -z "$result" ]];then echo -e YARN_NODEMANAGER_OPTS='"' -javaagent:{
{
install_dir}}/monitor/jmx_prometheus_javaagent-0.16.1.jar={
{
prometheus_port }}:{
{
install_dir }}/monitor/hadoop/nodemanager.yaml '$YARN_NODEMANAGER_OPTS''"' >> {
{ install_dir }}/yarn/etc/hadoop/yarn-env.sh; fi when: "package_version.startswith('2.')" tags: "install"
案例 config.yaml
# Start with three horizontal linesyaml
---
# copy命令,srcThe files in the directory are copied and overwritten todest目录下文件.
# with_items迭代.遍历configsEverything under parameters
- name: update config
copy: src={
{
tmp_config_dir }}/{
{
item }} dest={
{
install_dir }}/yarn/etc/hadoop/{
{
item }}
with_items:
- "{
{ configs }}"
tags: "config"
# state=absentexpress abandonment,在playbookis often used to delete
- name: delete tmp config
file: path={
{
tmp_config_dir }} state=absent
tags: "config"
注:Iteration can also be written in the same way
with_items: #定义with_items
- httpd
- vsftpd
- nginx
嵌套迭代
---
- hosts: all
remote_user: root
tasks:
- name: Create New Group
group: name={
{
item }} state=present
with_items:
- group1
- group2
- group3
- name: Create New User
user: name={
{
item.name }} group={
{
item.group }} state=present
with_items:
- {
name: 'user1', group: 'group1' }
- {
name: 'user2', group: 'group2' }
- {
name: 'user3', group: 'group3' }
9.playbookThe variable input method
①命令行指定变量
执行playbook时候通过参数-e传入变量,这样传入的变量在整个playbook中都可以被调用,属于全局变量
test.yaml文件内容:
- hosts: all
remote_user: root
tasks:
- name: test var
yum: name={
{
变量 }}
ansible-playbook -e “pkg=httpd” test.yml
②hosts文件中定义变量
在/etc/ansible/hosts文件中定义变量,可以针对每个主机定义不同的变量,也可以定义一个组的变量,然后直接在playbook中直接调用.注意,组中定义的变量没有单个主机中的优先级高.
vi /etc/ansible/hosts
[apache]
192.168.1.36 webdir=/opt/test #定义单个主机的变量
192.168.1.33
[apache:vars] #定义整个组的统一变量
webdir=/web/test
[nginx]
192.168.1.3[1:2]
[nginx:vars]
webdir=/opt/web
yaml文件引用变量
- hosts: all
remote_user: root
tasks:
- name: create webdir
file: name={
{
webdir }} state=directory #引用变量
执行: ansible-playbook variables.yml
③playbook文件中定义变量
yaml文件内容如下.直接执行
---
- hosts: all
remote_user: root
vars: #定义变量
pkg: nginx #变量1
dir: /tmp/test1 #变量2
tasks:
- name: install pkg
yum: name={
{
pkg }} state=installed #引用变量
- name: create new dir
file: name={
{
dir }} state=directory #引用变量
如果执行时候又重新指定了变量的值,那么会已重新指定的为准.as set at the time of execution:ansible-playbook -e “dir=/tmp/test2” variables.yml
④调用setup模块获取变量
setup模块默认是获取主机信息的,有时候在playbook中需要用到,所以可以直接调用.Common parameter references
yaml文件内容:
---
- hosts: all
remote_user: root
tasks:
- name: create file
file: name={
{
ansible_fqdn }}.log state=touch #引用setup中的ansible_fqd
⑤独立的变量yaml文件中自定义
为了方便管理将所有的变量统一放在一个独立的变量YAML文件中,laybook文件直接引用文件调用变量即可.
YAMLThe file holds variables:
var1: vsftpd
var2: httpd
playbook:
---
- hosts: all
remote_user: root
vars_files: #引用变量文件
- ./var.yml #指定变量文件的path(这里可以是绝对路径,也可以是相对路径)
tasks:
- name: install package
yum: name={
{
var1 }} #引用变量
- name: create file
file: name=/tmp/{
{
var2 }}.log state=touch #引用变量
执行:ansible-playbook variables.yml
边栏推荐
猜你喜欢
随机推荐
Install Metasploitable2 on VMware
5分钟搞懂MySQL - 行转列
(2022 Niu Ke Duo School 5) D-Birds in the tree (tree DP)
2022-2023 十大应用开发趋势
Transimpedance amplifier
2022-08-01 第四小组 修身课 学习笔记(every day)
Comprehensive experiment of MPLS and BGP
Elasticserch 自定义字段,用户会频繁的创建和删除字段,怎么设计mapping?
I.MX6U-ALPHA开发板(EPIT定时器实验)
数据表格化打印输出
MPLS和BGP的综合实验
uniapp 禁止默认返回事件
OSPF 综合实验
Visual Analysis of DeadLock
典型的一次IO的两个阶段是什么?阻塞、非阻塞、同步、异步
离线部署通过tiup 配置好topology.yaml文件指定PD TV TIDBserver 是不是会自动在其他机器创建好对应得模块?
研发创新编码器霍尔板,引领企业高质量发展
暂未找到具体原因但解决了的bug
Appium 滑动问题
HCIP9_BGP增加实验