您现在的位置是:首页 > 正文

ansible 流程控制

2024-04-01 00:31:23阅读 5

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

转载于:https://www.cnblogs.com/1naonao/p/11574448.html

网站文章

  • angular路由传递参数

    angular路由传递参数

    1、 查询参数中传递数据 在a标签上添加一个参数queryParams <a [routerLink]="['/tab4']" [queryParams]="{id:3}" >tab4</a> 此时此时除了rout

    2024-04-01 00:31:17
  • linux shell 除法运算符,Linux shell 基本运算符详解

    shell 支持多种运算符1.算数运算符2.关系运算符3.布尔运算符4.字符串运算符5.文件测试运算符原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr...

    2024-04-01 00:30:50
  • 《奔跑吧Linux内核(第二版)》第三章笔记

    《奔跑吧Linux内核(第二版)》第三章笔记

    ARM64架构基础知识

    2024-04-01 00:30:40
  • 【网络编程】更强的IO复用模型:epoll

    【网络编程】更强的IO复用模型:epoll

    epoll可以说是和poll非常相似的一种I/O多路复用技术,epoll通过监控注册的多个描述字,来进行I/O事件的分发处理。不同于poll的是,epoll不仅提供了默认的level-triggere...

    2024-04-01 00:30:18
  • 缓存穿透、缓存击穿、缓存雪崩概念及解决方案

    缓存穿透概念访问一个不存在的key,缓存不起作用,请求会穿透到DB,流量大时DB会挂掉。解决方案采用布隆过滤器,使用一个足够大的bitmap,用于存储可能访问的key,不存在的key直接被过滤;访问key未在DB查询到值,也将空值写进缓存,但可以设置较短过期时间。缓存雪崩概念大量的key设置了相同的过期时间,导致在缓存在同一时刻全部失效,造成瞬时DB请求量大、压力骤增,引起雪崩...

    2024-04-01 00:30:12
  • python if和条件判断

    逻辑关键字==, !=, >, <, >=, <= 这些都和C一样and, or 逻辑与或in, not in 在不在列表中ifif True: print("Tru...

    2024-04-01 00:30:04
  • html5 css3 实现连线功能

    猫 height: 50px; width: 100px; border: solid 3px #f00;"> dog 狗 height: 50px; width: 100px; border: solid 3px #f00;"> cow 牛

    2024-04-01 00:29:34
  • NS3数据包时延计算-插入时间戳Tag

    NS3数据包时延计算-插入时间戳Tag

    如果要在接收端得到数据包的传输时延,那么就需要在接收端得到数据包的发送时间和数据包到达时间,后者非常简单,直接在收到包的时候Simulator::Now ().GetSeconds ()即可,而怎样得...

    2024-04-01 00:29:25
  • 山东大学计算机技术复试体验

    博主7月开始的准备考研,现在比较难受,希望大家早做准备,暑假开始复习,慎重择校。 博主是初试375分,70名,双非二本。一志愿收27个,400以上的20+,复试比接近4:1,复试线360,抱着我已经录...

    2024-04-01 00:29:18
  • LeetCode 125.验证回文串 C++

    题目描述 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。 说明:本题中,我们将空字符串定义为有效的回文串。 示例 1: 输入: "A man, a plan...

    2024-04-01 00:28:55