ansible变量--第四天

playbook条件语句

一:判断语句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")
      判断:如果是centos就执行httpd。
    • name: Installed HTTP Server
      yum:
      name: httpd2
      state: present
      when: (ansible_distribution == "Ubuntu")
      判断:如果是ubuntu就执行httpd2.

实例:如果有100台服务器,只需要其中的10台安装nginx,那么我们就可以将这10台主机放在一个web组,然后只安装web组里的。
就需要做判断:
[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*"))
      意思:是web的都安装,不是web的都跳过。

when: ( ansible_hostname is match("web")) or ( ansible_hostname is match("web"))
意思是:web和lb都安装。or是或的意思


二:循环语句loop|with_item

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

[root@manager ansible_tasks]# cat t3.yml

  • hosts: webservers
    tasks:
    • name: Install Rpm All
      yum:
      name: "{{ item }}"
      state: present
      loop:

      • 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" }
批量创建用户,使用key values字典的方式
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
[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 | include_tasks

[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


六:忽略错误 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

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

七: 异常处理

1.控制task报告的状态,不一定必须是"changed"
[root@manager ansible_tasks]# cat t8.yml

  • hosts: webservers
    tasks:
    • name: Get Nginx Port Status
      shell: netstat -lntp | grep nginx
      register: ngx_status
      changed_when: false #(该tasks任务不会发生changed提示了)
    • name: Debug Nginx Status
      debug:
      msg: "{{ ngx_status.stdout_lines }}"

2.使用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
      配置文件一旦拷贝错误,在重启的时候就会报错,服务挂掉,
      所以这个判断做语法判断是非常有限,又可以检测,又可以不影响服务的使用。
      它会在出现语法错误之后就停止执行。也就不会在去加载启动模块。

你可能感兴趣的:(ansible变量--第四天)