Playbook判断 | 循环语句 | 异常处理

一,判断语句when


1.1 不同的主机名称进行判断

  1. 根据不同的操作系统进行判断 Apache
  2. centOS httpd
  3. Ubuntu httpd2

[root@manager ansible_tasks]# cat t1.yml

  1. - hosts: webservers
  2. tasks:
  3. - name: Installed HTTP Server
  4. yum:
  5. name: httpd
  6. state: present
  7. when: (ansible_distribution == "CentOS")
  8. - name: Installed HTTP Server
  9. yum:
  10. name: httpd2
  11. state: present
  12. when: (ansible_distribution == "Ubuntu")

1.2 特定主机安装

  1. 思路:
  2. 1.配置一个yum仓库 yum_repository
  3. 2.安装Nginx yum
  4. 实现:
  5. 100台主机里面只有10台主机是运行nginx的。难道我们需要给100台主机都配置nginx的官方源?
  6. 只有主机名称是 web 我们就安装 nginx的官方仓库 以及 安装nginx的软件。
  7. 代码:
  8. 组件这套集群架构时:
  9. 主机名称是web的,我们就安装nginx、.....

[root@manager ansible_tasks]# cat t2.yml

  1. - hosts: all
  2. tasks:
  3. - name: Installed Nginx Web Server
  4. yum:
  5. name: nginx
  6. state: present
  7. when: ( ansible_hostname is match("web*"))
  8. when: ( ansible_hostname is match("web*")) or
  9. ( ansible_hostname is match("lb*"))

*1.3 综合应用案例

  • 对nginx服务进行安装 配置 检查 启动
  • 每次修改配置,都需要检查语法,如果语法错误,则不重启Nginx ```bash [root@mananger tasks]# cat t4.yaml
  • hosts: webservers tasks:
    • name: Installed Nginx Server yum: name: nginx state: present
    • name: Configure Nginx Server copy: src: nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: Restart Nginx Server
    • name: Check Nginx Status shell: /usr/sbin/nginx -t register: Check_ngx ignore_errors: yes
    • name: Systemd Nginx Started systemd: name: nginx state: started enabled: yes when: ( Check_ngx.rc == 0 ) handlers:
    • name: Restart Nginx Server systemd: name: nginx state: restarted when: ( Check_ngx.rc == 0 ) ```

二,循环语句loop | with_items


2.1 一个tasks安装多个软件 (列表)

  1. [root@manager ansible_tasks]# cat t3.yml
  2. #方法一:
  3. - hosts: webservers
  4. tasks:
  5. - name: Install Rpm All
  6.  yum:
  7.    name: "{{ item }}" #固定的变量(会在loop列表中依次提取对应的值
  8.    state: present
  9.   loop:
  10.    - httpd
  11.    - httpd-tools
  12. #方法二:
  13. - name: ensure a list of packages installed
  14. yum:
  15.  name: "{{ packages }}"
  16. vars:
  17.   packages:
  18.    - httpd
  19.    - httpd-tools


2.2 一个tasks启动多个服务 (列表)

  1. [root@manager ansible_tasks]# cat t4.yml
  2. - hosts: webservers
  3. tasks:
  4. - name: Started Nginx And PHP-FPM Server
  5. systemd:
  6. name: "{{ item }}"
  7. state: started
  8. enabled: yes
  9. loop:
  10. - nginx
  11. - php-fpm

2.3 一个tasks拷贝多个文件 (字典)

  1. [root@manager ansible_tasks]# cat t5.yml
  2. - hosts: webservers
  3. tasks:
  4. - name: Configure Rsync Deamon
  5. copy:
  6. src: "{{ item.src }}"
  7. dest: "{{ item.dest }}"
  8. mode: "{{ item.mode }}"
  9. loop:
  10. - { src: rsyncd.conf.j2, dest: /opt/rsyncd.conf, mode: "0644" }
  11. - { src: rsync.pass.j2, dest: /opt/rsync.pass, mode: "0600" }

*2.4 综合应用案例

  1. 批量创建用户,(字典)
  2. testuser1  基本组 bin    8989 /bin/bash
  3. testuser2  基本组 root    7878 /bin/sh
  4. [root@manager ansible_tasks]# cat t6.yml
  5. - hosts: webservers
  6. tasks:
  7. - name: Create Users
  8. user:
  9. name: "{{ item.name }}"
  10. uid: "{{ item.uid }}"
  11. group: "{{ item.group }}"
  12. shell: "{{ item.shell }}"
  13. loop:
  14. - { name: testuser1 , uid: 8989 , group: bin , shell: /bin/bash }
  15. - { name: testuser2 , uid: 7878 , group: root , shell: /bin/sh }

三, 标签Tags

3.1 简单使用

#使用方法
-  name: xxx
   ....
     ...
  tags:  alibaba

 #排错具体task任务
[root@m01 ~/jms]#  ansible-playbook  site.yml  -t alibabal  -vvv   

(三个任务   Install Configure Started )
  对一个task打一个标签、 1  2  3
     对一个task打多个标签、
     对多个task打一个标签、 install nfs

3.2 执行示例

可打相同标签,执行多个tasks

[root@manager  ansible_tasks]#   cat t7.yml
- hosts: webservers
  tasks:
    - name: Install Nfs Server
      yum:
        name: nfs-utils
        state: present
      tags: nfs_mod

    - name: Service Nfs Server
      service:
        name: nfs-server
        state: started
        enabled: yes
      tags: nfs_mod
  • 指定执行 playbook中的某一个标签 ( 通常是用来快速解决问题 )

[root@manager ansible_tasks]# ansible-playbook t7.yml -t install_nfs

  • 指定 排除某个tags ,其余都正常执行

[root@manager ansible_tasks]# ansible-playbook t7.yml --skip-tags install_nfs

四,Include包含

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

[root@manager ansible_tasks]# cat restart_nginx.yml

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

[root@manager ansible_tasks]# cat a_project.yml

- hosts: webservers
  tasks:
    - name: A Project command
      command: echo "A"
    - name: Restart Nginx
      include: restart_nginx.yml

[root@manager ansible_tasks]# cat b_project.yml

- hosts: webservers
  tasks:
    - name: B Project command
      command: echo "B"
    - name: Restart Nginx
      include: restart_nginx.yml

ps: 不同playbook可以共同包含一个playbook

五, Ignore_errors 忽略错误

[root@manager ansible_tasks]# cat errors.yml

- hosts: webservers
  tasks:

    - name: Shell Command
      command: /bin/false
      ignore_errors: yes #忽略错误

    - name: Create File
      file:
         path: /tmp/oldux_tt
         state: touch

ps:ignore_errors使用当我们发现 某个task 偶尔会执行失败,但该task并不影响后续的tasks正常运行,那么此时可以 添加一个ignore_errors忽略经常出错的这个task

六,Debug异常处理模块

❤❤❤❤❤ 使用方法

  1. 执行模块, 获得输出结果
  2. 捕获被控端数据的结果,并注册到 register
  3. debug模块调试,获得具体的值——->>> 应用到 config.j2模板文件中

**

① 控制task报告的状态
“changed”——-“OK”
[root@manager ansible_tasks]# cat t8.yml

 hosts: webservers
  tasks:

    - name: Get Nginx Port Status
      shell:  netstat -lntp | grep nginx  #被控端的执行结果
      register: ngx_status                #注册一大堆的结果并打个标签

    - name: Debug Nginx Status
      debug:
        msg: "{{ ngx_status.stdout_lines }}"    #定位一大堆结果中的具体的值

[root@manager ansible_tasks]# cat t8.yml

- hosts: webservers
  tasks:

    - name: Get Nginx Port Status
      shell:  netstat -lntp | grep nginx
      register: ngx_status        #将执行结果保存到ngx_status变量中--->再调用检查变量
      changed_when: false         #该tasks任务不会发生changed提示了

②使用changed_when检查tasks任务返回的结果

[root@manager ansible_tasks]# cat t9.yml
- hosts: webservers
  tasks:
    - name: Install Nginx Server
      yum:
       name: nginx
       state: present
      tags: Install_Nginx_Server

    - name: Configure Nginx Server
      copy:
        src: ./nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: Restart Nginx Server

    - name: Check Nginx Configure File
      shell: nginx -t
      register: check_ngx       #将nginx -t的结果存储至check_ngx变量中
      changed_when:
        - false    
         #由于没有在被控端执行任何操作,所以可以将其修改为false,这个任务每次执行就ok状态
        - check_ngx.stdout.find('successful')
           #检查变量中是否存在successful的字符串,如果存在则继续,不存在则停止,调用检测结果并报错。

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

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

经典案例:使用shell输出随机数到变量
技巧:

  • -vvv 调试模式
  • register变量取值调试的时候,范围由大到小。