首页 > 其他分享 >Ansible - 高级语法

Ansible - 高级语法

时间:2024-01-15 17:45:29浏览次数:19  
标签:任务 name ansible 高级 语法 state Ansible file touch

 

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' }

 

标签:任务,name,ansible,高级,语法,state,Ansible,file,touch
From: https://www.cnblogs.com/houhuilinblogs/p/17965909

相关文章

  • 高级查询
    判断ISFULL(exp1,exp2)//exp1不为null,则返回exp1,否则返回exp2wherenameISnullwherenameISNOTnullwherebinaryname=‘aaa’//区别大小写=,!=,<>,>,>=查询处理逻辑查询执行顺序每一步生成一个虚拟表VT1,VT2,VT3,...如果没有指定某一子句,则跳过相应步骤,只有最后一步......
  • GitHub 高级搜索功能
    了解开源项目对于开发者的价值开源项目可以给开发者带来以下帮助:1、加速开发过程:开源项目提供了大量的可复用代码、库、框架和工具,可以帮助开发者快速构建应用程序和解决技术问题。这种可重用性可以显著加速项目的开发周期。2、降低开发成本:通过使用开源项目,你可以减少开发......
  • python语法——基本数据类型
    python常见数据类型有:number(数字),string(字符串),bool(布尔值),list(列表),tuple(元组),set(集合),dictionary(字典),bytes类型(1)number常见类型有int,float,bool.complex(复数)如何判断数据是什么类型?1.使用type()函数:`print(type(x))`该语句会输出数据x的类型2.使用isinstance()函数:is......
  • Ansible - 模块应用
     firewalld 模块使用firewalld模块可以配置防火墙策略[root@control~]#cat~/ansible/firewall.yml----hosts:agenttasks:-name:installfirewalld.yum:name:firewalldstate:present-name:runfirewalld.service:......
  • 【JaveWeb教程】(2)Web前端基础:JavaScript入门不再难:一篇文章教你轻松搞定JavaScript的
    目录1介绍2引入方式3基础语法3.1书写语法3.2变量3.3数据类型和运算符4函数4.1第一种定义格式4.2第二种定义格式html完成了架子,css做了美化,但是网页是死的,我们需要给他注入灵魂,所以接下来我们需要学习JavaScript,这门语言会让我们的页面能够和用户进行交互。1介绍通过代......
  • Net 高级调试之十六:平台互用性及P/Invoke和内存泄漏调试
    一、简介今天是《Net高级调试》的第十六篇文章,也是这个系列的最后一篇文章了。既然是最后一篇文章,我需要在这里说明一下,我当前的这个系列,不是针对《Net高级调试》这本书来的,而是根据“一线码农”的视频做的这个系列。当然了,他的视频是根据《Net高级调试》这本书来的,内......
  • 现代 IT 人一定要知道的 Ansible系列教程:playbook
    title='现代IT人一定要知道的Ansible系列教程:playbook'date=2023-12-23draft=falsesummary='AnsiblePlaybook提供了一个可重复、可重用、简单的配置管理和多机部署系统,非常适合部署复杂的应用程序。如果您需要多次使用Ansible执行任务,请编写一个playbook并将其......
  • 异常的基本语法
    最基本的语法#define_CRT_SECURE_NO_WARNINGS#include<iostream>intfunc(inta,intb){if(b==0){//2抛出异常throw12;//抛出一个int类型的异常}returna/b;}voidtest(){inta=10;intb=0;//1把......
  • 案例学Python:filter()函数的用法,高级!
    大家好,这里是程序员晚枫,又来分享有用的Python知识了。Python之所以好用,是因为有大量用于科学计算的内置函数和第三方库。用好这些第三方库,可以显著提高我们编程的速度和质量。今天我们一起来看一下Python中一个重要的内置函数:filter。filter()是Python中的一个内置函数,用于......
  • Angular 17+ 高级教程 – Component 组件 の Control Flow
     前言ControlFlow是Angularv17版本后推出的新模板语法,用来取代NgIf、NgForOf、NgSwitch这3个StructureDirective。StructureDirective的好处是比较灵活,原理简单,但是即便用了微语法,它看上去还是相当繁琐,而且不够优雅。ConrolFlow的好处是它的语法够美,缺点是不......