设为首页 加入收藏

TOP

ansible 流程控制(一)
2019-09-24 11:16:25 】 浏览:73
Tags:ansible 流程 控制

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

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux环境下交叉编译器安装及运行 下一篇ansible jinja2模板概述

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目