Playbook剧本之流程控制上

toc

条件判断语句

在Playbook剧本中,只有 when 可以实现条件判断

[root@Ansible project]# vim when.yml
- hosts: web
  tasks:
    - name: Install Httpd Server
      yum:
        name: httpd 
        state: installed
      when: ansible_distribution == "CentOS"
    - name: Install httpd2 Server
      apt:
        name: httpd2
        state: installed
      when: ansible_distribution == "Ubuntu"
## 条件满足这执行,不满足则跳过
[root@Ansible project]# ansible-playbook --syntax-check when.yml 

playbook: when.yml
[root@Ansible project]# ansible-playbook when.yml 

PLAY [web] ********************************************************************************

TASK [Gathering Facts] ********************************************************************
ok: [web1]

TASK [Install Httpd Server] ***************************************************************
changed: [web1]

TASK [Install httpd2 Server] **************************************************************
skipping: [web1]

PLAY RECAP ********************************************************************************
web1 : ok=2 changed=1 unreachable=0 failed=0   

支持逻辑运算

## 逻辑或
when: (ansible_distribution == "CentOS") or (ansible_distribution == "Ubuntu")
## 逻辑与(第一种)
when: (ansible_distribution == "CentOS") and (ansible_distribution_major_version == "7")
## 逻辑与(第二种)
when:
  - ansible_distribution == "CentOS"
  - ansible_distribution_major_version == "7"
## 逻辑非
when: not ansible_distribution == "Ubuntu"

register赋予变量判断

## 受控端/tmp/目录里文件超过10个删除目录里全部内容(执行过程会有警告,不用在意)
- hosts: web
  tasks:
    - name: Check Directory File
      shell: ls -l /tmp/|wc -l
      register: file_total
    - name: Print File Total 
      debug:
        var: file_total.stdout
    - name: Remove File
      shell: rm -rf /tmp/*
      when: file_total.stdout > "10"

循环语句

## 使用循环启动多个服务
[root@Ansible project]# vim start_server.yml
- hosts: web
  tasks:
    - name: Install Server
      yum:
        name:
          - httpd
          - mariadb-server
        state: installed
    - name: Start Server
      systemd:
        name: {{ item }}   ## 调用特殊变量
        state: started
      with_items:   ## 指定变量值
        - httpd
        - mariadb

列表循环

- hosts: web
  tasks:
    - name: Crrate User
      user:
        name: '{{ item.0 }}'       ## 指定索引0
        groups: '{{ item.1 }}'
        shell: '{{ item.2 }}'
        create_home: '{{ item.3 }}'
      with_together:
        - [ dasha, ersha ]                    ## 这为索引0
        - [ root, nobody ]                     ## 这为索引1
        - [ /bin/bash, /sbin/nologin ]    ## 这为索引2
        - [ yes, no ]                             ## 这为索引3

字典循环

## 使用循环批量创建用户
[root@Ansible project]# vim create_user.yml
- hosts: web
  tasks:
    - name: Crrate User
      user:
        name: '{{ item.name }}'
        groups: '{{ item.groups }}'
        shell: '{{ item.shell }}'
        create_home: '{{ item.home }}'
      with_items:
        - {name: "dasha",groups: "root",shell: "/bin/bash",home: "yes"}
        - {name: "ersha",groups: "nobody",shell: "/sbin/nologin",home: "no"}

变量循环

## 使用循环批量创建用户
[root@Ansible project]# vim create_user.yml
- hosts: web
  vars:    
## 这为变量集
    users:
      dasha:
        groups: root
        shell: /bin/bash
        home: yes
      ersha:
        groups: nobody
        shell: /sbin/nologin
        home: no

  tasks:
    - name: Crrate User
      user:
        name: '{{ item.key }}'
        groups: '{{ item.value.groups }}'
        shell: '{{ item.value.shell }}'
        create_home: '{{ item.value.home }}'
      with_dict: "{{ users }}"    ## 指定调用变量

register赋予变量循环
find模块:

  • paths 或 name 或 path --- 指定在哪个目录中查找文件,多个用逗号隔开
  • recurse --- 递归查找
  • hidden --- 是否查找隐藏文件,默认不查找
  • file_type --- 查找文件类型
    • file --- 文件(默认)
    • directory --- 目录
    • any --- 所有
    • link --- 链接
  • patterns --- 指定查找文件名称,可以是用通配符
  • use_regex --- 是否使用解析 patterns 正则,默认不使用
  • contains --- 根据文件内容查找文件,可以正则
  • age --- 根据时间范围查找文件
    • +7d --- 表示7天以后,单位秒(s)、分(m)、时(h)、天(d)、星期(w)
    • -7d --- 表示7天以内,单位秒(s)、分(m)、时(h)、天(d)、星期(w)
    • 7d --- 表示7天,单位秒(s)、分(m)、时(h)、天(d)、星期(w)
## 查找所有以 .conf 结尾的文件
[root@Ansible project]# vim find_file.yml
- hosts: web
  tasks:
    - name: Find File
      find:
        paths: /etc
        recurse: yes
        patterns: "*.conf"
        file_type: file
      register: find_file
    - name: print Find File
      debug:
        var: find_file.files
## 执行Playbook(用grep过滤)
[root@Ansible project]# ansible-playbook find_file.yml| grep path

文件循环

with_file 是将每个文件的文件内容作为item的值(用不到)
with_fileglob 是将每个文件的全路径作为item的值

## 当前目录所有文件拷贝到web主机 /tmp/ 目录
- hosts: web
  tasks:
    - name: Copy tmp Directory File
      copy:
        src: "{{ item }}"
        dest: /tmp/
      with_fileglob: ./*
原文地址:https://www.cnblogs.com/songguoyou/p/11883341.html