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

ansible_playbook任务控制

时间:2024-09-22 23:34:09浏览次数:16  
标签: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......
  • 1.JDK自带的线程池有哪些?2.线程池中核心线程数与最大线程数与缓冲任务队列的关系?3.为
    1.JDK自带的线程池有哪些?2.线程池中核心线程数与最大线程数与缓冲任务队列的关系?在Java中的线程池(如ThreadPoolExecutor)中,核心线程数(corePoolSize)、最大线程数(maximumPoolSize)以及缓冲队列(workQueue)之间存在着密切的关系,它们共同决定了线程池如何管理和调度任务。以下是......
  • DataX--Web:图形化界面简化大数据任务管理
            在处理大数据任务时,频繁地修改配置文件或编写脚本可能会变得繁琐且容易出错。DataXWeb提供了一个图形化界面,旨在简化这些操作,让用户通过直观的界面管理数据同步任务。DataXWeb简介        DataXWeb是一个开源项目,它允许用户通过Web界面来配置和管......
  • 【机器学习(九)】分类和回归任务-多层感知机 (MLP) -Sentosa_DSML社区版
    文章目录一、算法概念二、算法原理(一)感知机(二)多层感知机1、隐藏层2、激活函数sigma函数tanh函数ReLU函数3、反向传播算法三、算法优缺点(一)优点(二)缺点四、MLP分类任务实现对比(一)数据加载和样本分区1、Python代码2、Sentosa_DSML社区版(二)模型训练1、Python代码2、Sent......