ansible剧本

一、ansible剧本

  • 也就是一系列的ansible的命令的集合

1、yaml语法规则

  • 大小写敏感

  • 使用缩进标识层级关系

  • 使用缩进时必须是空格,不能是Tab键

  • 缩进的空格数不重要,只要同一层级保持一致就可以

  • 有的时候需要加上一个 -

2、yaml数据类型

纯量:单个的,已经是最小个体,无法再拆分

数组:一组按次序排列的值,数组元素使用 短横线开头

  • var01

  • var02

字典:采用键值对的方式

基本信息:
  名称: 张三
  年龄: 30
  性别: 男
 
地址:
  国家: 中国
  省份: 广东
  城市: 广州
  街道: 中山路
  邮编: 510000
 
联系方式:
  电话: 
    - 类型: 手机
      号码: 13800000000
    - 类型: 工作
      号码: 020-88888888
  邮箱: zhangsan@example.com
 
技能:
  编程语言:
    - Python
    - Java
    - JavaScript
  工具:
    - Git

2、编写剧本案例

1、示范


[devops@master ansible]$ cat yum.yml 
- name: install package  # 定义play的名字
  hosts: node1  # 在哪一个主机上进行操作
  tasks:        # 定义任务
    - name:     # 任务的描述信息
      yum:      # 使用的模块为yum
        name: vsftpd   # 模块里面的参数
        state: present

# 执行剧本
[devops@master ansible]$ ansible-playbook yum.yml

2、编写一个安装httpd剧本

  • 写一个剧本

  • 安装httpd,启动,防火墙,自定义文件内容

[devops@master ansible]$ cat httpd.yaml 
- name: config httpd
  hosts: node1
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present
    - name: config-firewalld
      firewalld:
        service: http
        permanent: true
        state: enabled
        immediate: true
    - name: config-file
      copy:
        content: "welcome web age \n"
        dest: /var/www/html/index.html
    - name: start httpd
      service:
        name: httpd
        state: started
        enabled: yes



# 访问

[devops@master ansible]$ curl node1
welcome web age 


3、-v和检查语法

  • -v选项就是显示详细信息

  • --syntax-check 检查语法 简写是--sy即可

[devops@master ansible]$ ansible-playbook httpd.yaml --syntax-check 

playbook: httpd.yaml

  • -C 会执行这个剧本,但是的话不是真正的执行
# 这个C非常的好用,有的时候这个语法没有错误,但是执行-C就会有问题

[devops@master ansible]$ ansible-playbook httpd.yaml -C

PLAY [config httpd] ***************************************************************************

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

TASK [install httpd] **************************************************************************
ok: [node1]

TASK [config-firewalld] ***********************************************************************
ok: [node1]

TASK [config-file] ****************************************************************************
ok: [node1]

TASK [start httpd] ****************************************************************************
ok: [node1]

PLAY RECAP ************************************************************************************
node1                      : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4、ansible剧本执行流程

  • 从上往下执行这个流程

  • 如果执行了一半出现了错误的话,就会终止,下次执行的时候,就会从错误的地方执行即可

  • 如果有多个主机node1,node2,node1剧本出现了错误,不会影响node2的剧本

1、handler特殊任务

  • 只有当所有的任务执行完成后,并且状态是chaged状态,才会执行handler任务

  • 监听一个模块,当这个模块执行的状态是chaged状态,才会执行

  • 跟tasks对齐

# 后面的dir这个任务就不会执行了,因为前面的任务有错误的
[devops@master ansible]$ cat test.yaml 
- name: qq
  hosts: node1
  tasks:
    - name: file
      file:
        path: /opt/file1.txt
        state: touch
    - name: ls
      shell: ls /opt/qwe
    - name: dir
      file:
        path: /opt/d1
        state: directory

[devops@master ansible]$ ansible-playbook test.yaml 

PLAY [qq] *****************************************************************************************************

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

TASK [file] ***************************************************************************************************
changed: [node1]

TASK [ls] *****************************************************************************************************
fatal: [node1]: FAILED! => {"changed": true, "cmd": "ls /opt/qwe", "delta": "0:00:00.002507", "end": "2025-09-15 13:20:31.628715", "msg": "non-zero return code", "rc": 2, "start": "2025-09-15 13:20:31.626208", "stderr": "ls: cannot access '/opt/qwe': No such file or directory", "stderr_lines": ["ls: cannot access '/opt/qwe': No such file or directory"], "stdout": "", "stdout_lines": []}

PLAY RECAP ****************************************************************************************************
node1                      : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   


5、剧本报错解决方法

  • 如果执行一个剧本报错了,还想要执行的话,可以使用下列三种方法

  • 执行一个错误的剧本

[devops@master ansible]$ cat test.yaml 
- name: qq
  hosts: node1
  tasks:
    - name: file
      file:
        path: /opt/file1.txt
        state: touch
    - name: ls
      shell: ls /opt/qwe
    - name: dir
      file:
        path: /opt/d1
        state: directory

# 执行剧本
[devops@master ansible]$ ansible-playbook test.yaml 

PLAY [qq] ***********************************************************************

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

TASK [file] *********************************************************************
changed: [node1]

TASK [ls] ***********************************************************************
fatal: [node1]: FAILED! => {"changed": true, "cmd": "ls /opt/qwe", "delta": "0:00:00.002689", "end": "2025-09-17 16:02:57.598989", "msg": "non-zero return code", "rc": 2, "start": "2025-09-17 16:02:57.596300", "stderr": "ls: cannot access '/opt/qwe': No such file or directory", "stderr_lines": ["ls: cannot access '/opt/qwe': No such file or directory"], "stdout": "", "stdout_lines": []}

PLAY RECAP **********************************************************************
node1                      : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   


1、命令来解决

  • 在剧本中当中,rc作为命令的返回值,rc=0就是执行成功了

  • rc不等于0的话,就是失败的

  • 将rc的值变成0即可,/usr/bin/true

[devops@master ansible]$ cat test.yaml 
- name: qq
  hosts: node1
  tasks:
    - name: file
      file:
        path: /opt/file1.txt
        state: touch
    - name: ls
      shell: ls /opt/qwe || /usr/bin/true  # 添加一个逻辑或即可完成,rc的返回值为0
    - name: dir
      file:
        path: /opt/d1
        state: directory

2、ignore_errors

  • 如果是模块报错的话,后面的模块不能执行了

  • ignore_errors:true 就是当这个任务失败的话,就会忽略这个任务,继续执行后面的任务

  • 跟模块的名字对齐即可

[devops@master ansible]$ cat test.yaml 
- name: qq
  hosts: node1
  tasks:
    - name: ls
      shell: ls /opt/qwe 
      ignore_errors: true # 忽略错误
    - name: dir
      file:
        path: /opt/d1
        state: directory

3、handler来规避错误

  • 与tasks任务是并齐的关系

  • 之前是所有的任务执行完成后,才能执行这个handler任务

  • 并且如果有报错的话,不会执行handler任务

  • 但是添加一个参数,就能执行这个handler任务,force_handlers

  • 但是监听的模块必须是changed状态才行

[devops@master ansible]$ cat test.yaml 
- name: qq
  hosts: node1
  force_handlers: yes  # 有报错的问题,强制执行
  tasks:
    - name: dir   # 这个模块必须在前面,如果在错误模块的后面的话,不起作用
      file:
        path: /opt/d2
        state: directory
      notify: get_st

    - name: ls
      shell: ls /opt/qwe  


  handlers:  # 与tasks任务对齐
    - name: get_st
      file:
        path: /opt/handler.txt
        state: touch


posted @ 2025-09-10 20:41  w7nn  阅读(11)  评论(0)    收藏  举报