Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 背景介绍
  • 安装
  • 配置说明
  • 基础命令
  • 常用模块
  • Playbook

    • 简单介绍
      • 简单上手示例
      • 关于加密
    • yaml语法
    • hosts
    • task
    • 运行playbook
    • handlers
    • tags
    • variables
    • template
    • when条件判断
    • with_items迭代
    • roles
    • 附:jinja2语法
  • Tool-Ansible
  • Playbook
Shetengteng
2022-05-15

简单介绍

playbook采用YAML语言编写

playbook是由一个或多个"play"组成的列表

  • play的主要功能在于将预定义的一组主机,装扮成事先通过ansible中的task定义好的角色
  • Task实际是调用ansible的一个module,将多个play组织在一个playbook中,即可以让它们联合起来,按事先编排的机制执行预定义的动作

流程

  • 用户通过ansible命令直接调用yml语言写好的playbook
  • playbook由多条play组成
  • 每条play都有一个任务 task 相对应的操作,调用模块modules
  • 应用在主机清单上,通过ssh远程连接,从而控制远程主机或者网络设备

# 简单上手示例

编写一个playbook的yaml文件,hello.yml

  • 使用command模块执行hostname命令
  • 在主机清单的group为websrvs的所有主机中,使用root用户执行
# 可忽略 ---
---
- hosts: websrvs
  remote_user: root
  
  tasks:
    - name: hello
      command: hostname
1
2
3
4
5
6
7
8

执行该yaml文件

[root@linux101 opt]# ansible-playbook hello.yml 

PLAY [websrvs] **********************
TASK [Gathering Facts] *****ok: [192.168.10.103]
ok: [192.168.10.102]
ok: [192.168.10.104]

TASK [hello] ***********changed: [192.168.10.102]
changed: [192.168.10.103]
changed: [192.168.10.104]

PLAY RECAP *****************************************
192.168.10.102  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.10.103  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.10.104  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 关于加密

由于playbook中可能会包含敏感信息,需要进行加密操作,使用ansible-vault命令进行加密操作

示例:对之前的hello.yml进行加密

[root@linux101 opt]# ansible-vault encrypt hello.yml 
New Vault password: 
Confirm New Vault password: 
Encryption successful
1
2
3
4

查看加密后的文件

[root@linux101 opt]# cat hello.yml 
$ANSIBLE_VAULT;1.1;AES256
61323234643433366430356264396131333935343862306334626265653630323737333634366338
3737363965616264646461356537366237643936356232390a373137623862386165306430333035
34646638626333383335323762313865373335663133643163623738306561313430363863653138
3039633234633435360a333135633030343539336438376433646232613832323637323238316165
37366430626439626363653938363162356465613462633139333339643838353461383132373266
33663963303233343438373930356134626530616531636465323634393263663466663532656465
61653631373764333731666665353931626135663263633262386633393865313039326439643661
36323635363737363034643465316338663966376663313336376336303232363961393937626239
33313566313637616266636562643636356639333934353533343032396530623033363566313133
3638363830653431396638366632376661323137663365636539
1
2
3
4
5
6
7
8
9
10
11
12

此时直接运行会报错

[root@linux101 opt]# ansible-playbook hello.yml
ERROR! Attempting to decrypt but no vault secrets found
1
2

如果要运行hello.yml需要进行解密操作

[root@linux101 opt]# ansible-vault decrypt hello.yml 
Vault password: 
Decryption successful
[root@linux101 opt]# cat hello.yml 
# 可忽略 ---
---
- hosts: websrvs
  remote_user: root
     
  tasks:
    - name: hello
      command: hostname
1
2
3
4
5
6
7
8
9
10
11
12

还可以不解密的情况下查看加密的文件以及重置key的操作,详细命令查看基础命令一节中的ansible-vault操作

Last Updated: 2022/05/22, 12:42:00
常用模块
yaml语法

← 常用模块 yaml语法→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式