Anisble中的任务执行控制_ansible在debug的时候怎么控制changed为 false

		value in value2		value的值在value2列表中

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

  • 条件1
  • 条件2

when:
条件1 or 条件2

when: >
条件1
or
条件2


测试题:


\*建立大小为1500M名为exam\_lvm的 lvm在westos组中


\*如果westos不存在请输入:


vg westos is not exist


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


vg westos is less then 1500M


并建立800M大小的lvm


三、触发器



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\n\n<VirtualHost *:{{web2.port}}>\n\tServerName {{web2.name}}\n\tDocumentRoot {{web2.document}}\n\tCustomLog logs/{{web2.name}}.log combined\n”
      notify:
      restart apache

    handlers:

    • name: restart apache
      service:
      name: httpd
      state: restarted

脚本练习:


搭建一个vsftpd服务




  • name: creat vsftpd
    hosts: all
    tasks:

    • name: install vsftpd
      dnf:
      name: vsftpd
      state: present

    • name: config vsftpd.conf
      template:
      src: ~/ansible/vsftpd.conf.j2
      dest: /etc/vsftpd/vsftpd.conf
      notify: restart vsftpd

    • name: config ftpdir
      file:
      path: /var/ftp/pub
      group: ftp
      mode: ‘0775’
      setype: “public_content_rw_t”

    • name: set sebool
      seboolean:
      name: ftpd_anon_write
      state: yes
      persistent: yes

    • firewalld:
      zone: public
      service: ftp
      permanent: yes
      state: enabled
      immediate: yes

    handlers:

    • name: restart vsftpd
      service:
      name: vsftpd
      state: restarted

#### 四、处理失败任务


1.ignore\_errors#  
 #作用:  
 当play遇到任务失败是会终止  
 ignore\_errors: yes    将会忽略任务失败使下面的任务继续运行


#实例#




  • name: test
    hosts: all
    tasks:
    • name: test
      dnf:
      name: sun
      state: present
      ignore_errors: yes
    • name: create file
      file:
      path: /mnt/sun
      state: touch


测试效果:


![](https://img-blog.csdnimg.cn/20201227122518104.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


2.force\_handlers#  
 #作用:  
 #当任务失败后play被终止也会调用触发器进程


#示例#




  • name: apache change port
    hosts: all
    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 }}”
      notify: restart apache

    • name: install error
      dnf:
      name: westos
      state: latest

    handlers:

    • name: restart apache
      service:
      name: httpd
      state: restarted
      enabled: yes

验证:


![](https://img-blog.csdnimg.cn/20201227123116632.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


3.changed\_when#  
 #作用:  
 #控制任务在何时报告它已进行更改




  • name: apache change port
    hosts: all
    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 }}”
      changed_when: yes
      notify: restart apache

    • name: install error
      ignore_errors: yes
      dnf:
      name: westos
      state: latest

    handlers:

    • name: restart apache
      service:
      name: httpd
      state: restarted
      enabled: yes

测试:


![](https://img-blog.csdnimg.cn/20201227124031849.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


 4.failed\_when#  
 #当符合条件时强制任务失败




  • name: test
    hosts: all
    tasks:

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Linux运维工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Linux运维知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip1024b (备注Linux运维获取)
img

最全的Linux教程,Linux从入门到精通

======================

  1. linux从入门到精通(第2版)

  2. Linux系统移植

  3. Linux驱动开发入门与实战

  4. LINUX 系统移植 第2版

  5. Linux开源网络全栈详解 从DPDK到OpenFlow

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。

需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

59aa1f2c54.png)

本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。

需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-X2FsAFxI-1712645922350)]

  • 17
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值