56. ansible playbook------ 判断 | 循环语句 | 异常处理

    技术2022-07-11  159

    文章目录

    一,判断语句when二,循环语句loop | with_items① 一个tasks启动多个服务 (列表)②一个tasks拷贝多个文件 (字典) 三,触发器 Handlers①标签 Tag 四,Include包含五, Ignore_errors 忽略错误六, 异常处理

    一,判断语句when

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

    根据不同的主机名称进行判断 [root@manager ansible_tasks]# cat t1.yml

    - hosts: webservers tasks: - name: Installed HTTP Server yum: name: httpd state: present when: (ansible_distribution == "CentOS") - name: Installed HTTP Server yum: name: httpd2 state: present when: (ansible_distribution == "Ubuntu")

    nginx官方的仓库安装 思路:

    1.配置一个yum仓库 yum_repository 2.安装Nginx yum 实现: 100台主机里面只有10台主机是运行nginx的。难道我们需要给100台主机都配置nginx的官方源? 只有主机名称是 web的 我们就安装 nginx的官方仓库 以及 安装nginx的软件。

    代码: 组件这套集群架构时: 主机名称是web的,我们就安装nginx、…

    [root@manager ansible_tasks]# cat t2.yml

    - hosts: all tasks: - name: Installed Nginx Web Server yum: name: nginx state: present when: ( ansible_hostname is match("web*")) when: ( ansible_hostname is match("web*")) or ( ansible_hostname is match("lb*"))

    二,循环语句loop | with_items

    #一个tasks安装多个软件 (列表) [root@manager ansible_tasks]# cat t3.yml

    - hosts: webservers tasks: - name: Install Rpm All  yum:    name: “{{ item }}” #固定的变量(会在loop列表中依次提取对应的值 )    state: present   loop:    - httpd   - httpd-tools

    对比官方的另一种方法 - name: ensure a list of packages installed yum:  name: “{{ packages }}” vars:   packages:    - httpd    - httpd-tools

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

    [root@manager ansible_tasks]# cat t4.yml - hosts: webservers tasks: - name: Started Nginx And PHP-FPM Server systemd: name: "{{ item }}" state: started enabled: yes loop: - nginx - php-fpm

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

    [root@manager ansible_tasks]# cat t5.yml

    - hosts: webservers tasks: - name: Configure Rsync Deamon copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" loop: - { src: rsyncd.conf.j2, dest: /opt/rsyncd.conf, mode: "0644" } - { src: rsync.pass.j2, dest: /opt/rsync.pass, mode: "0600" }

    批量创建用户,(字典) testuser1  基本组 bin    8989 /bin/bash testuser2  基本组 root    7878 /bin/sh

    [root@manager ansible_tasks]# cat t6.yml

    - hosts: webservers tasks: - name: Create Users user: name: "{{ item.name }}" uid: "{{ item.uid }}" group: "{{ item.group }}" shell: "{{ item.shell }}" loop: - { name: testuser1 , uid: 8989 , group: bin , shell: /bin/bash } - { name: testuser2 , uid: 7878 , group: root , shell: /bin/sh }

    三,触发器 Handlers

    ①标签 Tag

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

    [root@manager ansible_tasks]# ·cat t7.yml·

    - hosts: webservers tasks: - name: Install Nfs Server yum: name: nfs-utils state: present tags: install_nfs - name: Service Nfs Server service: name: nfs-server state: started enabled: yes tags: start_nfs

    指定执行 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包含

    [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


    六, 异常处理

    ① 控制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
    Processed: 0.012, SQL: 9