5.5_Ansible中的任务执行控制

##1.循环##

#循环迭代任务#

(1)简单循环

loop:
  - value1    ##赋值列表
  - value2
  - ...

{{item}}      ##迭代变量名称

#实例#
---
- name: create file
  hosts: 172.25.0.254
  tasks:
    - name: file module
      file:
        name: /mnt/{{item}}
        state: present
      
      loop:
        - westos_file1
        - westos_file2

(2)循环散列或字典列表

---
- name: create file
  hosts: 172.25.0.254
  tasks:
    - name: file module
      service:
        name: "{{ item.name}}"
        state: "{{ item.state }}"
      
      loop:
        - name: httpd
          state: started
        - name: vsftpd
          state: stopped

测试:

安装并开启vsftpd、apache、dns,并在火墙中允许这些服务访问。

##2.条件##

when:
  - 条件1
  - 条件2 

#条件判断#
 =    value == "字符串",value == 数字
 <    value <  数字
 >    value >  数字
 <=   value <= 数字
 >=   value >= 数字 
 !=   value != 数字
 is defined value   value is defined        变量存在
 is not defined     value is not defined    变量不存在
 in                 value is in value       变量为   
not in              value is not in value   变量不为
bool变量 为true      value                   value的值为true
bool变量 false       not value               value的值为false
                     value in value2        value的值在value2列表中

#多条条件组合#
when:
  条件1 and 条件2
  - 条件1
  - 条件2

when:
  条件1 or 条件2
 
when: >
  条件1
  or
  条件2

测试题:

建立playbook ~/ansibles/lvm.yml要求如下:

*建立大小为1500M名为exam_lvm的lvm 在westos组中

*如果westos不存在请输出:

vg westos is not exist

*如果westos大小不足1500M请输出:

vg westos is less then 1500M

并建立800M大小的lvm

##3.触发器##

notify: 触发器当遇到更改时触发handlers

handlers:触发器触发后执行的动作

#实例#---- name: create virtualhost for web server
  hosts: 172.25.0.254
  vars_files:
    ./vhost_list.yml
  tasks:
    - name: create document
      file:
        path: "{{web2.document}}"
        state: directory
    - name: create vhost.conf
      copy:
        dest: /etc/httpd/conf.d/vhost.conf
        content:
          "<VirtualHost *:{{web1.port}}>\n\tServerName {{web1.name}}\n\tDocumentRoot 
{{web1.document}}\n\tCustomLog logs/{{web1.name}}.log combined\n</VirtualHost>\n\n<VirtualHost *:
 {{web2.port}}>\n\tServerName {{web2.name}}\n\tDocumentRoot {{web2.document}}\n\tCustomLog logs/
 {{web2.name}}.log combined\n</VirtualHost>"
      notify:
        restart apache
 
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

#4.处理失败任务#

(1)ignore_errors

#作用:

当play遇到任务失败是会终止

ignore_errors: yes将会忽略任务失败使下面的任务继续运行

#实例#
- name: test
  dnf:
    name: westos
    state: latest
  ignore_errors: yes
  
- name: create file
  file:
    path: /mnt/westos
    state: touch

(2)force_handlers#

#作用:

#当任务失败后play被终止也会调用触发器进程

#example
---
- name: apache change port
  hosts: 172.25.0.254
  force_handlers: yes
  vars:
    http_port: 80
  tasks:
    - name: configure apache conf file
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen {{ http_port }}"
      notify: restart apache
    
    - name: install error
      dnf:
        name: westos
        state: latest
 
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
        enabled: yes

(3)changed_when#

#作用:

#控制任务报告它已进行更改

#实例
---
- name: apache change port
  hosts: 172.25.0.254
  force_handlers: yes
  vars:
    http_port: 8080
  tasks:
    - name: configure apache conf file
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen {{ http_port }}"
      changed_when: true
      notify: restart apache
  
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
        enabled: yes

(4)failed_when#

#当符合条件时强制任务失败

---
  - name: test
  hosts: 172.25.0.254
  tasks:
    - name: shell
      shell: echo hello
      register: westos
      failed_when: "'hello' in westos.stdout"

(5)block

block: ##定义要运行的任务

rescue: ##定义当block句子中出现失败任务后运行的任务

always: ##定义最终独立运行的任务

#测试练习#

挂载/dev/cdrom到/mnt/isodir里

如果/mnt/isodir不存在 -----> /mnt/isodir is not exist -----> create /mnt/isodir

如果/mnt/isodir存在 -----> 直接挂载

相关推荐
韩楚风2 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
陈苏同学2 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
Ambition_LAO2 小时前
解决:进入 WSL(Windows Subsystem for Linux)以及将 PyCharm 2024 连接到 WSL
linux·pycharm
Pythonliu72 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我3 小时前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
追风赶月、3 小时前
【Linux】进程地址空间(初步了解)
linux
栎栎学编程3 小时前
Linux中环境变量
linux
我是哈哈hh3 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
郭二哈3 小时前
C++——模板进阶、继承
java·服务器·c++
挥剑决浮云 -3 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记