Ansible - [09] 高级语法

 

error 处理机制

默认 ansible 在遇到 error 会立刻停止 playbook

[root@control ansible]# cat ~/ansible/error.yml
---
- hosts: test
  tasks:
    - name: start a service that does not exist.
      service:
        name: hehe
        state: started
    - name: touch a file
      file:
        path: tmp/service.txt
        state: touch

如果第一个任务不能成功执行,那么剧本就会中止,不会执行后续任务。

如果想要在第一个任务执行失败之后,继续执行后续任务,那么……

可以在剧本中添加:ignore_errors: true,可以忽略错误,继续后续的任务

[root@control ansible]# cat ~/ansible/error.yml
---
- hosts: test
  tasks:
    - name: start a service that does not exist.
      service:
        name: hehe
        state: started
      ignore_errors: true
    - name: touch a file.
      file:
        path: /tmp/service.txt
        state: touch

以上配置针对是某个任务,如果针对剧本全局忽略错误,可以进行如下配置.

[root@control ansible]# cat ~/ansible/error.yml
---
- hosts: test
  ignore_errors: true
  tasks:
    - name: start a service that does not exist.
      service:
        name: hehe
        state: started
    - name: touch a file.
      file:
        path: /tmp/service.txt
        state: touch

 

 

handlers

当某个任务需要依赖其他任务怎么办?

  • 可以通过handlers定义一组任务
  • 仅当某个任务触发(notify)handlers时才执行相应的任务
  • 如果有多个notify触发执行handlers任务,也仅执行一次
  • 仅当任务的执行状态为changed时,handlers任务才执行
  • handlers任务在所有其他任务都执行后才执行
[root@control ansible]# cat ~/ansible/handlers.yml
---
- hosts: test
  tasks:
    - name: create directory.
      file:
        path: /tmp/parents/subdir/
        state: directory
      notify: touch file
  handlers:
    - name: touch file
      file:
        path: /tmp/parents/subdir/new.txt
        state: touch

多次执行playbook该任务状态不再是changed

notify后面名称必须和handlers中的任务名称一致

 

when 条件判断

  • when可以定义判断条件,条件为真时才执行某个任务
  • 常见条件操作符如:==!=>>=<<=
  • 多个条件可以使用andor分割
  • when表达式中调用变量不要使用{{ }}

1、远程主机剩余内存不足700M则关闭NetworkManager

[root@control ansible]# cat ~/ansible/when_1.yml
---
- hosts: test
  tasks:
    - name: check memory size.
      service:
        name: NetworkManager
        state: stopped
      when: ansible_memfree_mb < 700

2、判断操作系统是RedHat8则创建测试文件(支持多行输入,不保留换行符)

[root@control ansible]# cat ~/ansible/when_2.yml
---
- hosts: test
  tasks: 
    - name: touch a file
      file:
        path: /tmp/when.txt
        state: touch
      when: >
        ansible_distribution == "RedHat"
            and
        ansible_distribution_major_version == "8"

 

 

block 任务块

使用block可以将多个任务合并为一个组

[root@control ansible]# cat ~/ansible/block_1.yml
---
- hosts: test
  tasks:
    - name: define a group of tasks.
      block:
        - name: install httpd
          yum:
            name: httpd
            state: present
        - name: start httpd
          service:
            name: httpd
            state: started
      when: ansible_distribution == "RedHat"

rescue 定义block任务执行失败时要执行的其他任务

always 定义无论block任务是否成功,都要执行的任务

[root@control ansible]# cat ~/ansible/block_2.yml
---
- hosts: test
  tasks:
    - block:
        - name: touch a file test1.txt
          file: 
            path: /tmp/test1.txt
            state: touch
      rescue:
        - name: touch a file test2.txt
          file:
            path: /tmp/test2.txt
            state: touch
      always:
        - name: touch a file test3.txt
          file: 
            path: /tmp/test3.txt
            state: touch

 

 

loop 循环

1、循环创建多个目录

[root@control ansible]# cat ~/ansible/simple_loop.yml
---
- hosts: test
  tasks:
    - name: mkdir multi directory.
      file:
        path: /tmp/{{ item }}
        state: directory
      loop:
        - School
        - Legend
        - Life

item是关键字

2、循环创建多个用户

[root@control ansible]# cat ~/ansible/complex_loop.yml
---
- hosts: test
  tasks:
    - name: create multi user.
      user:
        name: "{{ item.iname }}"
        password: "{{ item.ipass | password_hash('sha512') }}"
      loop:
        - { iname: 'term', ipass: '123456' }
        - { iname: 'amy', ipass: '654321' }

 

posted @ 2024-01-15 17:34  HOUHUILIN  阅读(15)  评论(0编辑  收藏  举报