Ansible 流程控制

8.判断语句

  • 1.centos和ubuntu系统都需要安装httpd, 判断系统.
  • 2.安装软件仓库,只有web组的安装webtatic其他的主机全部跳过.
  • 3.TASK任务, TASK1任务执行成功,才会执行TASK2
#根据不同的系统,安装不同的服务
- hosts: webservers
  tasks:
    - name: CentOS Installed Httpd Server
      yum:
        name: httpd
        state: present
      when: ( ansible_distribution == "CentOS" )

    - name: Ubuntu Installed Httpd Server
      yum:
        name: httpd2
        state: present
      when: ( ansible_distribution == "Ubuntu" )

[[email protected] project1]# cat f16.yml
- hosts: all
  tasks:
  - name: Add Nginx Yum Repository
    yum_repository:
      name: nginx
      description: Nginx Repository
      baseurl: http://nginx.org/packages/centos/7/$basearch/
    when: ( ansible_hostname is match ("web*"))

[[email protected] project1]# cat f17.yml
- hosts: webservers
  tasks:

    - name: Check Httpd Server
      command: systemctl is-active httpd
      register: Check_Httpd
      ignore_errors: yes

    #判断Check_Httpd.rc是否等于0,如果为0则执行任务,否则不执行
    - name: Restart Httpd Server
      systemd:
        name: httpd
        state: restarted
      when: ( Check_Httpd.rc == 0 )

9.循环语句

#一次启动多个服务
[[email protected] project1]# cat f18.yml
- hosts: webservers
  tasks:
    - name: Systemd Nginx Status
      systemd:
        name: "{{ item }}"    #调用的变量也不变,也是固定
        state: started

    #固定的语法格式
      with_items:
        - nginx
        - php-fpm

#一次拷贝多个文件
[[email protected] project1]# cat f19.yml
- hosts: webservers
  tasks:
    - name: Configure nginx.conf
      copy:
        src: '{{ item.src }}'
        dest: '{{ item.dest }}'
        mode: '{{ item.mode }}'
      with_items:
        - { src: ./file/nginx.conf.j2, dest: /etc/nginx/nginx.conf, mode: '0644' }
        - { src: ./file/kold.oldxu.com.conf.j2, dest: /etc/nginx/conf.d/kold.oldxu.com.conf, mode: '0600' }

#创建多个用户,一次创建多个? 3个用户  TASK
[[email protected] project1]# cat f20.yml
- hosts: webservers
  tasks:
    - name: Create User
      user:
        name: "{{ item }}"

      with_items:
        - test1
        - test2
        - test3
        - test4

#1.创建tt1 --> bin  tt2 -->root tt3 --->adm   附加组
[[email protected] project1]# cat  f20.yml
- hosts: webservers
  tasks:
    - name: Create User
      user:
        name: "{{ item.name }}"
        groups: "{{ item.groups }}"

      with_items:
        - { name: tt1, groups: bin }
        - { name: tt2, groups: root }
        - { name: tt3, groups: adm }

1.标准循环                   --->居多
    item
    with_items:
       - test
2.字典循环:                   --->居多
    itme.name
    with_items:
        - { name: test }

3.变量循环
- hosts: webservers
  tasks:
    - name: ensure a list of packages installed
      yum: name={{ packages }} state=present
      vars:
        packages:
          - httpd
          - httpd-tools

10.handlers

[[email protected] project1]# cat f22.yml
- hosts: webservers
  tasks:

    - name: Installed Nginx and PHP Packages
      yum:
        name: nginx
        state: present

    - name: Configure nginx.conf
      template:
        src: ./file/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      #监控-->changed状态-->通知-->handlers--->name-->Restart Nginx Server
      notify: Restart Nginx Server
      #notify:
      #  - Restart Nginx Server
      #  - Restart php Server

    - name: Systemd Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes

#当nginx或php配置文件发生变更才会触发此操作
  handlers:
    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted

#3.handlers注意事项
    1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。
    2.只有task发生改变了才会通知handlers,没有改变则不会触发handlers.
    3.不能使用handlers替代tasks、因为handlers是一个特殊的tasks。

变量->facts-->判断-->循环

  • 2.安装Redis (bind 本地IP地址) facts bind {{ ansible_eth1.ipv4.address }}
  • 1.安装Rsyncd服务 (循环) rsyncd.conf 644 rsync.passwd 600
  • 3.安装NFS (配置文件,创建目录,客户端挂载) 变量
  • 4.安装Nginx+PHP
  • 1.变量
  • 2.facts
  • 3.register
  • 4.when 判断语句 ( facts )
  • 5.with_items 循环
    • 1.标准循环
    • 2.字典循环
    • 3.变量循环(忽略)

1.tag标签(调试)

[[email protected] tasks]# cat task_nfs.yml
- hosts: webservers
  tasks:

        #对一个任务打多个标签
    - name: Install Nfs Server
      yum:
        name: nfs-utils
        state: present
      tags:
        - install_nfs
        - install_nfs-server

        #对一个任务打一个标签
    - name: Service Nfs Server
      service:
        name: nfs-server
        state: started
        enabled: yes
      tags: start_nfs-server

ansible-playbook -i ../hosts  task_nfs.yml  -t start_nfs-server123
ansible-playbook -i ../hosts  task_nfs.yml  --skip-tags install_nfs-server

2.include包含

3.错误处理 ignore_errors: yes

3.异常处理

? 1.每次状态都是changed,纵使没有修改过被控端

        #Check_Redis_Status=$(netstat -lntp | grep redis)
    - name: Check Redis Status
      shell: netstat -lntp | grep redis
      register: Check_Redis_Status
      changed_when: false

        #echo ${Check_Redis_Status}
    - name: Debug Check_Redis Variables
      debug:
        msg: "Redis Status: {{ Check_Redis_Status.stdout_lines }}" 

? 2.nginx推送配置文件后,没有任何的检查功能

###########################################low版

[[email protected] tasks]# cat task_nginx.yml
- hosts: webservers
  tasks:

        #安装nginx
    - name: Installed nginx Server
      yum:
        name: nginx
        state: present

        #配置nginx
    - name: Configure nginx Server
      template:
        src: ./file/nginx.conf.j2
        dest: /etc/nginx/nginx.conf

        #检查nginx (Check_Nginx_Status=$(nginx -t))
    - name: Check Nginx Configure File
      shell: nginx -t
      register: Check_Nginx_Status
      ignore_errors: yes
      changed_when: false

        #启动Nginx
    - name: Systemd Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes
      when: ( Check_Nginx_Status.rc == 0 )  #只有Check_Nginx_Status.rc=0时,才会执行启动操作

##################################new版
[[email protected] tasks]# cat task_nginx.yml
- hosts: webservers
  tasks:
        #安装nginx
    - name: Installed nginx Server
      yum:
        name: nginx
        state: present

        #配置nginx
    - name: Configure nginx Server
      template:
        src: ./file/nginx.conf.j2
        dest: /etc/nginx/nginx.conf

        #检查nginx (Check_Nginx_Status=$(nginx -t))
    - name: Check Nginx Configure File
      shell: nginx -t
      register: Check_Nginx_Status
      changed_when:
        - Check_Nginx_Status.stdout.find('successful')
        - false

        #启动Nginx
    - name: Systemd Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes

##################################new版
[[email protected] tasks]# cat task_nginx.yml
- hosts: webservers
  tasks:
        #安装nginx
    - name: Installed nginx Server
      yum:
        name: nginx
        state: present

        #配置nginx
    - name: Configure nginx Server
      template:
        src: ./file/nginx.conf.j2
        dest: /etc/nginx/nginx.conf

        #检查nginx (Check_Nginx_Status=$(nginx -t))
    - name: Check Nginx Configure File
      shell: nginx -t

        #启动Nginx
    - name: Systemd Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes      

3.强制调用handlers 触发器

[[email protected] tasks]# cat task_nginx.yml
- hosts: webservers
  force_handlers: yes   #无论tasks失败与否,只要通过过handlers,那me一定会执行
  tasks:
        #安装nginx
    - name: Installed nginx Server
      yum:
        name: nginx
        state: present

        #配置nginx
    - name: Configure nginx Server
      template:
        src: ./file/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: Restart Nginx Server

        #检查nginx (Check_Nginx_Status=$(nginx -t))
    - name: Check Nginx Configure File
      shell: nginx -t

        #启动Nginx
    - name: Systemd Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes 

  handlers:
    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted

总结: Ansible Task 控制

  • 1.判断语句 when
  • 2.循环语句 with_items
  • 3.触发器 handlers
  • 4.标签 tag
  • 5.忽略错误 ignore_errors
  • 6.异常处理
    • 1.关闭TASK的changed状态 -->让该TASK一直处理OK状态 changed_when: false
    • 2.强制调用handlers: 当正常task调用过handlers,则无论后续的task成功还是失败,都会调用handlers触发器执行任务.
    • 3........ Check_Nginx_Status.stdout.find(‘successful‘)

原文地址:https://www.cnblogs.com/baozexu/p/11669932.html

时间: 2024-07-31 06:17:34

Ansible 流程控制的相关文章

汇编语言入门:流程控制

流程控制:顺序,分支,循环 程序计数器PC中存储当前执行的程序在EM中的位置 汇编里面,用比较.跳转实现流程控制. 1.顺序:PC+1(不一定加一,看指令长度) 2.分支循环,直接赋给PC值,执行指定地址的程序 有时候需要程序有一定的流程控制能力,它不是老老实实按照顺序来执行的,中间可能会跳过一些代码 修改PC值,不可用MOV指令,PC是特殊的寄存器,特殊对待,跳转指令修改其值. 跳转指令: 1 ja 大于时跳转 2 jae 大于等于 3 jb 小于 4 jbe 小于等于 5 je 相等 6 j

从头开始-04.C语言中流程控制

分支结构: if语句:当条表达式满足的时候就执行if后面大括号中语句 三种格式: if,if else , if else if else 特点:1.只有一个代码块会被执行 2.若有else那么必有一个代码会被执行 3.一个if语句中只能以if开头,最多只能有一个else,可以有任意个else if switch(表达式){ case 常量: ... break; case 常量1: ... break; default: ... break; } : 使用场景:当一个变量只有几个固定的取值的时

shell脚本之流程控制

shell脚本之流程控制 shell脚本之流程控制 条件语句 条件判断 循环语句for,while,until for循环 while循环 until循环 循环控制语句continue 循环控制语句break 循环控制shift命令 创建无限循环 while的特殊用法 for的特殊用法 select循环与菜单 select与case 信号捕捉trap 条件语句 选择执行: 注意:if语句可嵌套 单分支 if 判断条件;then 条件为真的分支代码 fi 双分支 if 判断条件; then 条件为

shell脚本编程——流程控制

shell脚本编程--流程控制 目   录 一. if 二. case 三. for 四. while 五. until 六. 综合应用 一.if 1.语法 (1)单分支 if  判断条件:then fi (2)双分支 if 判断条件; then 条件为真的分支代码 else 条件为假的分支代码 fi (3)多分支 if 判断条件1; then 条件为真的分支代码 elif 判断条件2; then 条件为真的分支代码 elif 判断条件3; then 条件为真的分支代码 else 以上条件都为假

shell脚本流程控制

shell 脚本变成使用过程中通常需要流程控制,一般情况下是顺序执行,在实际使用过程中根据不同情况需要执行不同命令,这时就用到选择执行比如if.case,有时需要重复执行多次,循环执行比如for.while.until 条件选择执行语句if 单分支 双分支 if 判断条件;then 条件为真的分支代码 fi if 判断条件; then 条件为真的分支代码 else 条件为假的分支代码 fi [[email protected] ~]# vim score.sh   1 #!/bin/bash  

Python基础--if流程控制与循环

流程控制之if...else 既然我们编程的目的是为了控制计算机能够像人脑一样工作,那么人脑能做什么,就需要程序中有相应的机制去模拟.人脑无非是数学运算和逻辑运算,对于数学运算在上一节我们已经说过了.对于逻辑运算,即人根据外部条件的变化而做出不同的反映. ###Python定义了缩进的机制.使用流程控制或循环时需要在执行语句前面缩进4个空格. if...else格式: if 条件: 执行语句 else: 执行语句 例如:判断日期,如果是周日则打游戏,不是周日则上班睡觉. date='Monday

程序流程控制

流程控制是所有编程语言的基础部分,在Java自然也不例外. 在程序设计时,我们经常需要改变程序的控制流程,也就是语句的执行顺序.有三种基本技术可以改变流程的控制流程: 1.调用方法.调用方法将导致控制流程离开当前方法,转移到被调用的方法. 2.选择.Java中有两种做出选择的机制:if/else语句和switch语句.三目去处符也可以用于选择.但是它通常只是if/else的简写版本. 3.循环.Java中有三种循环语句:for循环.while循环.do/while循环. 选择语句: if语句:一

Visual Basic快捷教程——流程控制

美籍匈牙利数学家冯·诺依曼于1946年提出"程序存储,顺序执行"的观念,为现代计算机奠定了重要基础.这一观点认为:应该把程序本身当作数据来对待,程序和该程序处理的数据用同样的方式储存.电子计算机的数制宜采用二进制:计算机应该按照程序顺序执行.所以现在开发人员在编写的程序代码时,其实都隐含地认为计算机将来会一行一行按顺序来执行这些指令.但是在顺序执行的过程中,有时我们希望计算机根据条件判断来选择性地执行(或者不执行)一些代码--这时就需要用到选择结构(或称分支结构).另外一些时候,我们希

PHP中的流程控制

PHP中的流程控制: 任何PHP脚本都是由一系列的语句构成.一条语句可以是一个赋值语句,一个函数调用,一个循环,一个条件语句或者甚至是一个什么也不做的语句(空语句).语句通常以分号结束.此外还可以用花括号将一组语句封装成一个语句组.语句本省可以当作是一行语句. if语句 if结构是很多语言包括php在内最重要的特性之一,他允许按照条件执行代码片段.php的if结构和C语言相似: <?phpif($a > $b) echo"a is bigger than b"; ?>