ansible 流程控制

使用when判断主机名

- hosts: rsync_server
tasks: - name: Install rsyncd Server
yum:
name: rsync
state: present - name: Config rsyncd Conf
copy:
src: ./rsyncd.j2
dest: /etc/rsyncd.conf
owner: root
group: root
mode: 0644
when: ansible_fqdn == 'backup' - name: Create dir
file:
path: /backup
state: directory
owner: www
group: www
mode: 0755
recurse: yes
when: ansible_fqdn == 'backup' - name: Create passwd file
copy:
content: "rsync_backup:123"
dest: /etc/rsync.passwd
owner: root
group: root
mode: 0600
when: ansible_fqdn == 'backup' #单条件判断
- name: Start rsyncd
systemd:
name: rsyncd
state: started
enabled: yes
when: ansible_fqdn == 'backup' #多条件判断,使用小括号分组
- name: copy shell
template:
src: ./backup.sh
dest: /root
when: (ansible_fqdn == 'web01') or (ansible_fqdn == 'web02') #多条件判断,使用list列表形式
- name: copy shell
template:
src: ./backup.sh
dest: /root
when:
- ansible_fqdn == 'web01'
- ansible_fqdn == 'web02' #多条件判断,使用is match 支持通配符
- name: Add Crontab
cron:
name: "backup"
minute: "00"
hour: "01"
job: "/bin/sh /root/backup.sh &>/dev/null"
when: ansible_fqdn is match 'web*'

2.使用when判断系统

- hosts: webs
tasks:
- name: Install CentOS Apache
yum:
name: httpd
state: present
when: ansible_distribution == 'CentOS' - name: Install Ubuntu Apache
apt:
name: apache2
state: present
when: ansible_distribution == 'Ubuntu'

3.使用when判断系统版本

- hosts: webs
tasks:
- name: Start CentOS6 Httpd
shell: "/etc/init.d/httpd start"
when: ansible_distribution_major_version == '6' - name: Start CentOS7 Httpd
shell: "systemctl start httpd"
when: ansible_distribution_major_version == '7'

4.使用注册变量对返回值进行判断

- hosts: web_group
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
ignore_errors: yes
register: check_httpd - name: debug outprint
debug: var=check_httpd - name: Httpd Restart
service:
name: httpd
state: restarted when: check_httpd.rc == 0
- name: pan duan rpm bao
shell: "rpm -qa|grep php"
register: check_php - name: Install php
shell: "cd /usr/local/src && rpm -Uvh *rpm"
when: check_php.rc != 0

ansible循环语句

1.with_items

    - name: start php and nginx
systemd:
name: "{{ item }}"
state: started
enabled: yes
with_items:
- nginx
- php-fpm

2.变量循环

- name: ensure a list of packages installed
yum:
name: "{{ packages }}"
vars:
packages:
- httpd
- httpd-tools

3.字典循环

- hosts: web_group
tasks:
- name: copy conf and code
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- { src: "./httpd.conf", dest: "/etc/httpd/conf/", mode: "0644" }
- { src: "./upload_file.php", dest: "/var/www/html/", mode: "0600" }
    - name: tar php and nginx and wordpress
unarchive:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ item.user }}"
group: "{{ item.user }}"
copy: yes
with_items:
- { src: "./php.tgz", dest: "/usr/local/src", user: "root" }
- { src: "./nginx-1.16.0.tar.gz", dest: "/root", user: "root" }
- { src: "./wordpress.tgz", dest: "/code", user: "www" }

ansible handlers(触发器)

    - name: scp nginx shell conf
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- { src: "./nginx.service", dest: "/usr/lib/systemd/system" }
- { src: "./nginx.conf", dest: "/usr/local/nginx/conf/" }
- { src: "./www.drz.com.conf", dest: "/usr/local/nginx/conf/conf.d" }
#添加触发器
notify:
- reload nginx
- reload php handlers:
- name: reload nginx
systemd:
name: nginx
state: reloaded
- name: reload php
systemd:
name: php
state: reloaded

注意:

1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。

2.Handlers只有在其所在的任务被执行时,才会被运行;如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,该任务未被执行,那么Handlers同样不会被执行。

3.Handlers只会在每一个play的末尾运行一次;如果想在一个playbook中间运行Handlers,则需要使用meta模块来实现。例如: -meta: flush_handlers。

4.如果一个play在运行到调用Handlers的语句之前失败了,那么这个Handlers将不会被执行。我们可以使用meta模块的--force-handlers选项来强制执行Handlers,即使Handlers所在的play中途运行失败也能执行。

5.不能使用handlers替代tasks

playbook复用include

1.只调用task include_tasks

[root@m01 web]# vim install_nginx.yml 

- name: Install nginx
yum:
name: nginx
state: present [root@m01 web]# vim conf_nginx.yml
- name: conf nginx
copy:
src: ./www.drz.com.conf
dest: /etc/nginx/conf.d
notify: reload nginx [root@m01 web]# vim main.yml
- hosts:
tasks:
- include_tasks: ./install_nginx.yml
- include_tasks: ./conf_nginx.yml
handlers:
- name: reload nginx
systemd:
name: nginx
state: reloaded [root@m01 m01]# ansible-playbook tag.yml --list-tags
[root@m01 m01]# ansible-playbook tag.yml -t httpd_server
[root@m01 m01]# ansible-playbook tag.yml -t install_httpd,confiure_httpd
[root@m01 m01]# ansible-playbook tag.yml --skip-tags httpd_server

2.直接复用写好的yml文件

  • 旧版:include
  • 新版:import_playbook
  • 在saltstack中,叫做top file入口文件
- import_playbook: ./lnmp.yml
- import_playbook: ../mariadb/mysql.yml

示例一:

[root@m01 m01]# cat task.yml
- hosts: web_group
vars:
- http_port: 8080 tasks:
- include_tasks: task_install.yml
- include_tasks: task_configure.yml
- include_tasks: task_start.yml handlers:
- name: Restart Httpd Server
systemd:
name: httpd
state: restarted [root@m01 m01]# cat task_install.yml
- name: Install Http Server
yum:
name: httpd
state: present [root@m01 m01]# cat task_configure.yml
- name: configure httpd server
template:
src: ./httpd.j2
dest: /etc/httpd/conf/httpd.conf
notify: Restart Httpd Server [root@m01 m01]# cat task_start.yml
- name: start httpd server
service:
name: httpd
state: started
enabled: yes

示例二:

- include: httpd.yml
- include: nfs.yml
- include: rsync.yml

示例三:

- include: httpd.yml
- include: nfs.yml
- include: rsync.yml

ansible 流程控制的更多相关文章

  1. Ansible--04 ansible 流程控制

    ansible 流程控制 playbook 条件语句 不管是 shell 还是各大编程预言中,流程控制,条件判断都是必不可少的,在我们使用 Ansible的过程中,条件判断的使用频率都非常高. 例如: ...

  2. Ansible流程控制

    Ansible流程控制 数据库操作问题: 数据库的操作问题,python需要依耐的模块MySQL-python . 数据库的操作 # 设置root的密码在,root的密码设置之后,创建用户和创建数据库 ...

  3. ansible-playbook流程控制-loops循环使用

    1. ansible-playbook流程控制-loops循环使用    有时你想要多次重复任务.在计算机编程中,这称为循环.common ansible循环包括使用文件模块更改多个文件和/或目录的所 ...

  4. 自动化运维工具之Puppet变量、正则表达式、流程控制、类和模板

    前文我们了解了puppet的file.exec.cron.notify这四种核心资源类型的使用以及资源见定义通知/订阅关系,回顾请参考https://www.cnblogs.com/qiuhom-18 ...

  5. 第10章 Shell编程(4)_流程控制

    5. 流程控制 5.1 if语句 (1)格式: 格式1 格式2 多分支if if [ 条件判断式 ];then #程序 else #程序 fi if [ 条件判断式 ] then #程序 else # ...

  6. Shell命令和流程控制

    Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...

  7. PHP基础知识之流程控制的替代语法

    PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch. 替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,e ...

  8. Python黑帽编程2.4 流程控制

    Python黑帽编程2.4  流程控制 本节要介绍的是Python编程中和流程控制有关的关键字和相关内容. 2.4.1 if …..else 先上一段代码: #!/usr/bin/python # - ...

  9. 使用yield进行异步流程控制

    现状 目前我们对异步回调的解决方案有这么几种:回调,deferred/promise和事件触发.回调的方式自不必说,需要硬编码调用,而且有可能会出现复杂的嵌套关系,造成"回调黑洞" ...

随机推荐

  1. LAMP环境搭建与配置(1)

    安装和配置MySQL.Apache.PHP 概念 LAMP是Linux Apache MySQL PHP 的简写,把Apache.MySQL以及PHP安装在Linux系统上,组成一个环境来运行PHP的 ...

  2. 通知advice

    基于注解的Spring AOP开发,来自https://www.cnblogs.com/junzi2099/p/8274813.html 1.定义目标类接口和实现类 2.编写Spring AOP的as ...

  3. C++ std::deque 基本用法

    #include <iostream> #include <string> #include <deque> // https://zh.cppreference. ...

  4. 未来已来:云原生 Cloud Native

    作者:天知,原文链接 前言 自 2013 年容器(虚拟)技术(Docker)成熟后,后端的架构方式进入快速迭代的阶段,出现了很多新兴概念: 微服务 k8s Serverless IaaS:基础设施服务 ...

  5. 【STM32-V6】STM32F429BIT6开发板开源, 丰富软件资源, 强劲硬件配置, 配套400多实例, 9套手册持续更新中2019-12-12

    淘宝购买地址:淘宝购买链接 次.当前标准库最新版本V2.3,HAL库最新版本V1.1 安富莱微信公共平台,欢迎大家关注(打造高质量公众号) 新版用户手册,重在BSP驱动包设计方法,HAL库的框架学习, ...

  6. 零基础想学习C语言,没资源、没人带、不知道从何开始?

    初学编程的小伙伴经常会遇到的问题,1.没资源 2.没人带 3.不知道从何开始 ? 小编也是从新手期过来的,所以很能理解萌新的难处,现在整理一些以前自己学习买来的一些资料送给大家,希望对广大初学小伙伴有 ...

  7. input输入框change和blur事件区别

    blur与change事件在绝大部分的情况下表现都非常相似,输入结束后,离开输入框,会先后触发change与blur,唯有两点例外. 1.没有进行任何输入时,不会触发change 在这种情况下失焦后, ...

  8. Consul初探-在深交之前先认识

    Consul 是什么? Consul 官方站点:https://www.consul.io/ 首先,官方介绍是:Consul 是一种服务网格的解决方案,在 Consul 中,提供了服务发现.配置.分段 ...

  9. Navicat远程连接MySQL8,必知防坑策略

    项目上线是每一个开发工程师面临收获前面抓紧时间开发的成果,但有时我们上线项目首先需要做一些相关的业务测试.通过Xshell远程连接后使用命令行的方式连接操作Mysql这个没什么太大的你问题.但每次通过 ...

  10. 海康Poe 摄像头尾线与8根网线连接方法

    家里海康POE摄像头铜丝断了一根,拆开自己接了个RJ44座,线序黑. 棕. 绿. 橙. 红. 黄. 紫. 蓝 以此 对应橙白.橙.绿白.蓝.蓝白.绿.棕白.棕经测试无误,可以正常使用