首页 > 其他分享 >ansible_playbook任务控制

ansible_playbook任务控制

时间:2024-09-22 23:34:09浏览次数:1  
标签:systemctl name when nginx 任务 ansible hosts playbook

ansible_playbook任务控制:
条件判断when
循环语句with_items
触发器 handlers
标签tags 根据指定的标签执行,调试
包含include
错误忽略ignore_errors
错误处理 changed_when

1.条件判断when
使用()
变量不使用{{}}

自带的变量名字获取:
ansible servers -i hosts.cfg -m setup

1)示例:当系统为CentOS时复制文件
- hosts: servers
tasks:
- name: change configure file when is CentOS
template: src=./test.test dest=/root
when : ( ansible_distribution == "CentOS" )

命令:ansible-playbook -i hosts.cfg when.yaml

2)示例:hostname包含"iZ2zei0"时复制文件(给特定主机名的主机)
- hosts: servers
tasks:
- name: change configure file when is iZ2zei0*
template: src=./test.test dest=/root
when: ( ansible_fqdn is match ("iZ2zei0*") )

#多个主机名( 使用或者 or)
- hosts: servers
tasks:
- name: change configure file when is iZ2zei0*
template: src=./test.test dest=/root
when: ( ansible_fqdn is match ("iZ2zei0*") ) or ( ansible_fqdn is match ("ali-*") )

 

2.循环语句with_items
1)示例:使用方法一
- hosts: servers
tasks:
- name: systemctl restart {{ item }}
serverice: name={{ item }} state=restarted
with_items:
- nginx
- php

2)示例:使用方法二
批量创建用户
- hosts: servers
tasks:
- name: Create User
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: 'test111', group: 'bin'}
- { name: 'test222', group: 'bin'}


命令:ansible-playbook -i hosts.cfg with_items.yaml

 


3.触发器 handlers(所有任务都执行完,最后执行)
notify监控 --> handlers 触发

handlers与tasks同级

1)示例:配置文件改变,则重启nginx服务
- hosts: servers
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify: systemctl restart nginx
handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted

命令:ansible-playbook -i hosts.cfg handlers.yaml

2)示例:配置文件改变,则重启多个服务
- hosts: servers
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify:
- systemctl restart nginx
- systemctl restart php-fpm

handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted
- name: systemctl restart php-fpm
service: name=php-fqm state=restarted

 

4.标签tags 根据指定的标签执行,调试

对一个任务指定一个标签
对一个任务指定多个标签
对多个任务指定一个标签

 

1)示例:执行某个任务
- hosts: servers
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify: systemctl restart nginx
tags: check_nginx_configure
handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted

命令:ansible-playbook -i hosts.cfg tags.yaml -t "check_nginx_configure"

2)示例:除了某个任务都执行(忽略某个任务)

命令:ansible-playbook -i hosts.cfg tags.yaml --skip-tags "check_nginx_configure"

 

5. 包含include(与nginx类似)
1)示例:部分包含
- hosts: servers
tasks:
- name: change configure for nginx
include: include.yaml


2)示例:全包含
- import_playbook: tags.yaml
- import_playbook: with_items.yaml


6. 错误忽略ignore_errors
有报错会终止脚本
加上这个会忽略掉此任务,接着执行下面的
1)示例:
- hosts: servers
tasks:
- name: Shella
shell: /bin/false
ignore_errors: yes
- name: Shellb
shell: ls /root

命令:ansible-playbook -i hosts.cfg ignore_errors.yaml


7.错误处理 changed_when

1)示例:强制执行handlers(不常用)
- hosts: servers
force_handlers: yes
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify: systemctl restart nginx
handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted

2)changed_when发现则执行,否则不继续执行
- hosts: servers
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify: systemctl restart nginx

- name: check configure for nginx
shell: nginx -t
register: check_nginx
changed_when: ( check_nginx.stdout.find('successful') )
handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted


命令:ansible-playbook -i hosts.cfg changed_when.yaml

 

标签:systemctl,name,when,nginx,任务,ansible,hosts,playbook
From: https://www.cnblogs.com/circlecircle/p/18426113

相关文章

  • playbook脚本编写
    playbook脚本文件后缀.yml(.yaml)作用:找某个主机组[hosts]干某件事[tasks]-hosts:serversgrouptasks:脚本写完做检查语法:(在线效验:https://www.bejson.com/validators/yaml_editor/)ansible-playbook--syntaxplaybook.yam模拟演练检测运行是否报错:ansible-playbook-C--syn......
  • Java后端开发中的任务调度:使用Spring Batch实现批处理
    Java后端开发中的任务调度:使用SpringBatch实现批处理大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在现代企业应用中,批处理是处理大规模数据的重要方式。SpringBatch为我们提供了强大的工具来实现批处理任务。本文将详细介绍如何使用SpringBatch......
  • 事件循环如何处理微任务和宏任务
    在javascript中,微任务和宏任务是事件循环管理的两种类型的异步任务,但它们的处理方式不同。了解它们的工作原理对于预测异步代码的执行顺序至关重要。1.宏任务队列(任务队列)宏任务被放入自己的队列中,通常称为任务队列或宏任务队列。宏任务的示例包括:settimeout、setinterval......
  • UI 阻塞行为:微任务与宏任务
    你能找出下面两个代码片段的区别吗:functionhandleclick1(){settimeout(handleclick1,0);}functionhandleclick2(){promise.resolve().then(handleclick2);}登录后复制如果您无法确定选择其中一个的含义,那么这篇博文将教您一些新知识。背景settimeout用于安......
  • 1.JDK自带的线程池有哪些?2.线程池中核心线程数与最大线程数与缓冲任务队列的关系?3.为
    1.JDK自带的线程池有哪些?2.线程池中核心线程数与最大线程数与缓冲任务队列的关系?在Java中的线程池(如ThreadPoolExecutor)中,核心线程数(corePoolSize)、最大线程数(maximumPoolSize)以及缓冲队列(workQueue)之间存在着密切的关系,它们共同决定了线程池如何管理和调度任务。以下是......
  • 排查帝国CMS定时刷新任务失效问题,快速解决!
    当帝国CMS的定时刷新任务失效时,可以通过以下几个方面来进行排查和解决:1.检查计划任务设置访问计划任务页面:登录帝国CMS后台管理系统,进入“系统”->“计划任务”页面。确认任务设置:确保定时刷新任务已经正确设置,包括触发时间、执行频率等参数。2.校验服务器时间检查服务......
  • DataX--Web:图形化界面简化大数据任务管理
            在处理大数据任务时,频繁地修改配置文件或编写脚本可能会变得繁琐且容易出错。DataXWeb提供了一个图形化界面,旨在简化这些操作,让用户通过直观的界面管理数据同步任务。DataXWeb简介        DataXWeb是一个开源项目,它允许用户通过Web界面来配置和管......
  • 直播短视频源码,延迟任务的解决方法
    直播短视频源码,延迟任务的解决方法在直播短视频源码中,我们有时候会遇到这样的场景,比如下单之后超过30分钟未支付自动取消订单,还有就比如过期/生效通知等等,这些场景一般有两种方法解决:第一种可以通过定时任务扫描符合条件的去执行;第二种就是提前通过消息队列发送延迟消息到期自......
  • jsDoc npm 模块任务
    目前我正在工作/维护遗留的js/react应用程序,没有办法重新工作到typesript,这就是为什么我打开jsdoc作为js现有的开发时类型系统。太长了;typescriptnpm模块由jsdoc制作,useduck在70loc下带回了redux的黄金时代。该模块在开发时的主要用例,帮助您的复杂状态保持......
  • 【机器学习(九)】分类和回归任务-多层感知机 (MLP) -Sentosa_DSML社区版
    文章目录一、算法概念二、算法原理(一)感知机(二)多层感知机1、隐藏层2、激活函数sigma函数tanh函数ReLU函数3、反向传播算法三、算法优缺点(一)优点(二)缺点四、MLP分类任务实现对比(一)数据加载和样本分区1、Python代码2、Sentosa_DSML社区版(二)模型训练1、Python代码2、Sent......