ansible playbook模式及语法

一、什么是playbook及其组成

什么是playbook

playbook 翻译过来就是"剧本"

playbook的组成

play:定义的是主机的角色
task:定义的是具体执行的任务
playbook:由一个或多个play组成,一个play可以包含多个task

二、playbook的优势

1、功能比adhoc更全
2、控制好依赖
3、展现更直观
4、持久使用

三、playbook的配置语法

基本使用

playbook基础使用

ansible-playbook playbook.yml [options]

-u REMOTE_USER, --user=REMOTE_USER 
# ssh 连接的用户名 
-k, --ask-pass 
#ssh登录认证密码 
-s, --sudo 
#sudo 到root用户,相当于Linux系统下的sudo命令
-U SUDO_USER, --sudo-user=SUDO_USER 
#sudo 到对应的用户 
-K, --ask-sudo-pass #用户的密码(—sudo时使用) 
-T TIMEOUT, --timeout=TIMEOUT 
# ssh 连接超时,默认 10-C, --check # 指定该参数后,执行 playbook 文件不会真正去执行,而是模拟执行一遍,然后输出本次执行会对远程主机造成的修改 
-e EXTRA_VARS, --extra-vars=EXTRA_VARS 
# 设置额外的变量如:key=value 形式 或者 YAML or JSON,以空格分隔变量,或用多个-e 
-f FORKS, --forks=FORKS # 进程并发处理,默认 5
-i INVENTORY, --inventory-file=INVENTORY
# 指定 hosts 文件路径,默认 default=/etc/ansible/hosts
-l SUBSET, --limit=SUBSET
# 指定一个 pattern,对- hosts:匹配到的主机再过滤一次
--list-hosts # 只打印有哪些主机会执行这个 playbook 文件,不是实际执行该 playbook
--list-tasks # 列出该 playbook 中会被执行的 task
--private-key=PRIVATE_KEY_FILE # 私钥路径
--step # 同一时间只执行一个 task,每个 task 执行前都会提示确认一遍
--syntax-check # 只检测 playbook 文件语法是否有问题,不会执行该 playbook
-t TAGS, --tags=TAGS #当 play 和 task 的 tag 为该参数指定的值时才执行,多个 tag 以逗号分隔
--skip-tags=SKIP_TAGS # 当 play 和 task 的 tag 不匹配该参数指定的值时,才执行
-v, --verbose #输出更详细的执行过程信息,-vvv可得到所有执行过程信息。 原文链接:https://www.imooc.com/article/22729

使用场景

1、playbook的配置

示例:

---
- hosts : 192.168.56.11
  remote_user : root
  vars :
          touch_file : devops.file
  tasks :
          - name : touch file
            shell: "touch /tmp/{{touch_file}}"

2、执行

devops@devops-virtual-machine:/etc/ansible$ cat /etc/ansible/hosts
[test_group1]
#192.168.56.11:22 ansible_ssh_user=root ansible_ssh_pass='1234567'
#192.168.56.11:22 ansible_ssh_user=root ansible_ssh_key_file=/home/devops/.ssh/id_rsa
192.168.56.11:22 ansible_ssh_user=root

#列出f1.yml指的的主机与/etc/ansible/hosts匹配到的主机
devops@devops-virtual-machine:/etc/ansible$ ansible-playbook -i /etc/ansible/hosts --list-hosts ./f1.yml

playbook: ./f1.yml

  play #1 (192.168.56.11): 192.168.56.11    TAGS: []
    pattern: ['192.168.56.11']
    hosts (1):
      192.168.56.11




devops@devops-virtual-machine:/etc/ansible$ ansible-playbook -i /etc/ansible/hosts ./f1.yml

PLAY [192.168.56.11] ***********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.56.11]

TASK [touch file] **************************************************************************************************************
 [WARNING]: Consider using file module with state=touch rather than running touch

changed: [192.168.56.11]

PLAY RECAP *********************************************************************************************************************
192.168.56.11              : ok=2    changed=1    unreachable=0    failed=0

3、执行结果返回

  • 红色:表示有task执行失败或者提醒的信息
  • 黄色:表示执行了且改变了远程主机状态
  • 绿色:表示执行成功

主机匹配

yaml语法和变量

yaml语法

  • 大小写敏感
  • 使用缩进表示层级关系(只能空格不能使用tab)
  • yaml文件"---"作为文档的开始

yaml支持的数据结构

yaml变量的应用

playbook变量

  1. playbook的yaml文件中的定义变量赋值
  2. --extra-vars执行参数赋给变量 

 示例:

devops@devops-virtual-machine:/etc/ansible$ cat f1.yml
---
- hosts : 192.168.56.11
  remote_user : root
  #  vars :           # 注册这两行
          #          touch_file : devops.file
  tasks :
          - name : touch file
            shell: "touch /tmp/{{touch_file}}"


# 执行
devops@devops-virtual-machine:/etc/ansible$ ansible-playbook ./f1.yml --extra-vars "touch_file=json2"

PLAY [192.168.56.11] ***********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.56.11]

TASK [touch file] **************************************************************************************************************
 [WARNING]: Consider using file module with state=touch rather than running touch

changed: [192.168.56.11]

PLAY RECAP *********************************************************************************************************************
192.168.56.11              : ok=2    changed=1    unreachable=0    failed=0

查看执行结果

devops@devops-virtual-machine:/etc/ansible$ ansible  all -a 'ls /tmp'
192.168.56.11 | SUCCESS | rc=0 >>
devops.file
json2

  3、在文件中定义变量

在资产清单中定义变量

devops@devops-virtual-machine:/etc/ansible$ cat /etc/ansible/hosts
[test_group1]
#192.168.56.11:22 ansible_ssh_user=root ansible_ssh_pass='1234567'
#192.168.56.11:22 ansible_ssh_user=root ansible_ssh_key_file=/home/devops/.ssh/id_rsa
192.168.56.11:22 ansible_ssh_user=root


# 添加两行内容如下: 当f1.yaml执行时会引用(touch_file)这个变量
[test_group1:vars]
touch_file=json3

# 执行
devops@devops-virtual-machine:/etc/ansible$ ansible-playbook ./f1.yml

PLAY [192.168.56.11] ***********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.56.11]

TASK [touch file] **************************************************************************************************************
 [WARNING]: Consider using file module with state=touch rather than running touch

changed: [192.168.56.11]

PLAY RECAP *********************************************************************************************************************
192.168.56.11              : ok=2    changed=1    unreachable=0    failed=0


# 查看执行服务器(192.168.56.11 )上是否有json3文件
devops@devops-virtual-machine:/etc/ansible$ ansible  all -a 'ls /tmp'
192.168.56.11 | SUCCESS | rc=0 >>
json2
json3
示例

  4、注册变量

register关键字可以存储指定的命令的输出结果到一个自定义的变量中

- name: get time

  command:date

  register:date_output

基本语句

异常处理和相关操作

四、场景批量部署服务(Nginx)

猜你喜欢

转载自www.cnblogs.com/hwlong/p/9301008.html