当前位置: 首页 > news >正文

Ansible中的任务执行控制

循环

简单循环

{{item}} 迭代变量名称
loop:
- value1
- value2
- ...                 //赋值列表{{item}}        //迭代变量名称

循环散列或字典列表

- name: create filehosts: host1tasks:- name: file moudleservice:name: "{{ item.name }}"state: "{{ item.state }}"loop:- name: httpdstate: started- name: vsftpdstate: stopped
~

条件

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

 多条条件组合

when:条件1 and 条件2- 条件1- 条件2            //当条件1和条件2都为真when:条件1 or 条件2        //满足其中一个when: >条件1or条件2                //满足其中一个

 创建两个用户 user1 和 user2, user1 的 UID 是 6666,注释是 user1 commentuser2 的 UID 是 7777,没有注释。

vim userlist.yml
userlist:- name: user1id: 6666comment: user1 comment- name: user2id: 7777vim usercreate.yml
- name: create userhosts: allvars_files: ./userlist.ymltasks:- name: create user with commentuser:name: "{{item.name}}"uid: "{{item.id}}"comment: "{{item.comment}}"state: presentwhen: item.comment is definedloop:"{{userlist}}"- name: create user without commentuser:name: "{{item.name}}"uid: "{{item.id}}"state: presentwhen: item.comment is not definedloop:"{{userlist}}"

 

触发器

notify:         //触发器当遇到更改是触发handlers
handlers:             //触发器触发后执行的动作

 在指定的服务器上部署网站,并创建一个简单的主页

vim webs.yml
webs:- name: bbs.westos.orgdoc: /var/www/virtual/westos.org/bbs/htmlindex: "bbs.westos.org's page"- name: login.westos.orgdoc: /var/www/virtual/westos.org/login/htmlindex: "login.westos.org's page"- name: www.westos.orgdoc: /var/www/htmlindex: "www.westos.org's page"

 

vim html.j2
{% for web in webs %}
{% if web.name is not defined %}
<VirtualHost _default_:80>
{% elif web.name is defined %}
<VirtualHost *:80>ServerName {{web.name}}
{% endif %}DocumentRoot {{web.doc}}
</VirtualHost>
{% endfor %}

 

vim web.yml
- name: Create website index pagehosts: allvars_files: ./webs.ymltasks:- name: install httpdyum:name: httpdstate: present- name: serviceservice:name: httpdstate: startedenabled: no- name: Create index.html for each websitetemplate:src: ./html.j2dest: "/etc/httpd/conf.d/vhost.conf"notify: restart httpd- name: create directorylineinfile:path: "{{item.doc}}/index.html"line: "{{ item.index }}"create: yesloop: "{{webs}}"handlers:- name: restarted httpdservice:name: httpdstate: restarted

 

 

 

 

http://www.lryc.cn/news/216976.html

相关文章:

  • 利用maven的dependency插件分析工程的依赖
  • 【广州华锐互动】VR野外求生技能学习,让你感受真实的冒险之旅!
  • k8s、调度约束
  • Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
  • 电子器件 MOS管的参数、选型与使用技巧
  • EtherCAT主站SOEM -- 2 -- SOEM之ethercatbase.h/c文件解析
  • Spring集成高性能队列Disruptor
  • C++——类和对象(中)完结
  • Sqoop的安装和使用
  • java毕业设计基于springboot+vue的村委会管理系统
  • 【C++】多态 ⑪ ( 纯虚函数和抽象类 | 纯虚函数语法 | 抽象类和实现 | 代码示例 )
  • node 第十四天 基于express的第三方中间件multer node后端处理用户上传文件
  • KnowledgeGPT:利用检索和存储访问知识库上增强大型语言模型10.30
  • Angular material Chips Autocomplete
  • 『亚马逊云科技产品测评』活动征文|搭建基础运维环境
  • 双指针扫描
  • uniapp小程序九宫格抽奖
  • mysql树状结构查询及注意事项
  • TimeGPT-1——第一个时间序列数据领域的大模型他来了
  • 通过Google搜索广告传送的携带木马的PyCharm软件版本
  • 网站文章收录因素,别人复制文章排名比你原创的好?
  • C#开源的一个能利用Windows通知栏背单词的软件 - ToastFish
  • 速拿offer,超全自动化测试面试题+答案汇总,背完还怕拿不到offer?
  • LeetCode----1415. 长度为 n 的开心字符串中字典序第 k 小的字符串
  • 2310C++协程超传服务器
  • 【排序算法】 计数排序(非比较排序)详解!了解哈希思想!
  • 20231103配置cv180zb的编译环境【填坑篇】
  • 足底筋膜炎如何治疗
  • rabbitMq路由键介绍
  • 【python基础】python切片—如何理解[-1:],[:-1],[::-1]的用法