Linux 第五本书 第五单元--------Ansible中的任务执行控制

条件

when

条件判断:
vim mnt.yml

- name: mnt
  hosts: westos
  tasks:
    - name: check
      shell: test -e /mnt/test
      register: CHECK             设置变量CHECK             
      ignore_errors: yes
    - name: debug
      debug:
        msg: "/mnt/test is not exist"
      when:
          CHECK.rc != 0        在when里 0是存在,1是不存在       
    - name: debug exist
      debug:
        msg: "/mnt/test is exist"
      when:
         CHECK.rc == 0

=============================================
触发器   
例:apache

 


vim apache.yml


- name: configure webserver
  hosts: westos
  tasks:
    - name: install httpd
      dnf:
       name: httpd
       state: present
       
    - name: configure httpd.conf
      lineinfile:
            path: /etc/httpd/conf/httpd.conf
            regexp: "^Listen"
            line: "Listen 8080"
      notify:                     --------选择哪些服务名称需要用触发器
            restart httpd
  handlers:               --------触发器:如果前面已经执行过,则触发器下面的无需执行
    - name: restart httpd
      service:
        name: httpd
        state: restarted
        enabled: yes

 

       
==============================================
练习:建立大小为1500M的vdc1,判断vdc是否存在,如果不存在输入vdc is not exist,如果内存不足1.5G 输出vdc is less then 1.5G , 并建立800M挂载在westos上

- name: fdisk
  hosts: westos
  tasks:
     - name:  check
       debug:
         msg: /dev/vdc is not exist
       when: ansible_facts['devices']['vdc'] is not defined


     - name: test
       block:                  -----判断是否执行,执行失败则前往rescue
          - parted:
              device: /dev/vdc
              number: 1
              state: present
              part_end: 1500MiB
            when: ansible_facts['devices']['vdc'] is defined

       rescue:
          - debug:
              msg: /dev/vdc is less then 1500 MiB
          - parted:
              device: /dev/vdc
              number: 1
              state: present
              part_end: 800MiB

       always:             ---------必定执行
          - filesystem:
              fstype: xfs
              dev: /dev/vdc1
              force: yes
          - mount:
              path: /westos
              src: /dev/vdc1
              fstype: xfs
              state: mounted

 

 

===========================================
处理任务失败

igonre_errors     ----------忽视任务失败

force_handlers    ---------当任务失败后play被终止也会调用触发器

changed_when      -------------控制任务在何时报告它已进行更改

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误提示是因为安装ansible-7.2.0-1.el8.noarch软件包时,需要依赖于Python 3.11版本,但系统没有找到这个版本的Python安装包。要解决这个问题,可以采取以下步骤: 1. 检查系统安装的Python版本:使用命令`python3 --version`或`python --version`查看系统当前的Python版本。如果没有安装Python或版本过低,需要先安装或升级Python。 2. 检查系统Python可用的软件源:使用命令`sudo yum repolist`查看当前系统可用的软件源。如果没有合适的软件源,可以尝试添加EPEL(Extra Packages for Enterprise Linux)软件源,该源提供了更多的软件包选择。 3. 安装Python 3.11:如果系统没有可用的Python 3.11安装包,可以尝试从其他来源或第三方软件源安装该版本的Python。可以在Python官方网站上下载Python 3.11源码,并按照官方文档的指引进行编译和安装。 4. 更新软件源:确保已经添加并更新了可用的软件源。使用命令`sudo yum update`更新系统的软件包列表。 5. 安装ansible-7.2.0-1.el8.noarch软件包:在确保Python 3.11已经安装并可用的情况下,使用命令`sudo yum install ansible-7.2.0-1.el8.noarch`安装所需的软件包。 6. 验证安装:安装完成后,可以使用命令`ansible --version`验证Ansible的安装和版本号。 请注意,对于使用不同的操作系统版本和软件源的情况,上述步骤可能会有所不同。建议根据具体情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值