Ansible中的任务执行控制

######实验环境设置######

设置ansible主机网段为39网段ip为172.25.39.39(设置主机网桥时关闭虚拟机否则无法成功)

Ansible中的任务执行控制_第1张图片

Ansible中的任务执行控制_第2张图片

新建3台受控主机设置网络为39网段ip分别为172.25.39.10  ;20 ;  30Ansible中的任务执行控制_第3张图片

 ansible主机建立用户devops

devops用户下cd ansible

ssh-keygen生成密钥

for i in 10 20 30
> do
> ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected].$i
> done


openssl passwd -6 生成密钥

ansible all -m user -a 'name=devops state=present password=”密钥“' u root

for i in 10 20 30;do ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected].$i; done

ansible all -m inventory -a ‘path=/etc/sudoers line="devops ALL=(ALL) NOPASSWD: ALL"’ -u root

ansible all -m ping 检测

Ansible中的任务执行控制_第4张图片

##一.循环##
#循环迭代任务#
#1.简单循环#
loop:        ##赋值列表
  - value1
  - value2
  - ...

{ {item}}    ##迭代变量名称

#实例#
---
- name: create file
  hosts: all
  tasks:
    - name: file module
      file:
        name: /mnt/{ {item}}
        state: touc    

     loop:
        - westos_file1
        - westos_file2

Ansible中的任务执行控制_第5张图片

Ansible中的任务执行控制_第6张图片

#2.循环散列或字典列表#
---
- name: create file
  hosts: all
  tasks:
    - name: file module
      service:
        name: "{ { item.name}}"
        state: "{ { item.state }}"
      loop:
        - name: httpd
          state: started
        - name: vsftpd
          state: stopped

##二.条件##
when:
  - 条件1
  - 条件2

#条件判断#
=            value == "字符串",value == 数字
<            value <  数字
>            value >  数字
<=            value <= 数字
>=            value >= 数字
!=            value != 数字
is defined value    value is defined    变量存在
is not defined        value is not defined    变量不存在
in                      value is in value       变量为    
not in                  value is not in value   变量不为
bool变量 为true        value            value的值为true
bool变量 false        not value        value的值为false
            value in value2        value的值在value2列表中
#多条条件组合#
when:
  条件1 and 条件2
  - 条件1
  - 条件2

when:
  条件1 or 条件2

when: >
  条件1
  or
  条件2
测试题:
建立playbook  ~/ansibles/lvm.yml要求如下:
*建立大小为1500M名为exam_lvm的lvm 在westos组中
*如果westos不存在请输出:
 vg westos  is not exist
*如果westos大小不足1500M请输出:
 vg westos is less then 1500M
 并建立800M大小的lvm

Ansible中的任务执行控制_第7张图片

##三.触发器##
notify:        触发器当遇到更改是触发handlers

handlers:    触发器触发后执行的动作

#实例#
---
- name: create virtualhost for web server
  hosts: all
  vars_files:
    ./vhost_list.yml
  tasks:
    - name: create document
      file:
        path: "{ {web2.document}}"
        state: directory
    - name: create vhost.conf
      copy:
        dest: /etc/httpd/conf.d/vhost.conf
        content:
          "\n\tServerName { {web1.name}}\n\tDocumentRoot { {web1.document}}\n\tCustomLog logs/{ {web1.name}}.log combined\n\n\n\n\tServerName { {web2.name}}\n\tDocumentRoot { {web2.document}}\n\tCustomLog logs/{ {web2.name}}.log combined\n"
      notify:
        restart apache

  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

#四.处理失败任务#
#1.ignore_errors#
#作用:
当play遇到任务失败是会终止
ignore_errors: yes    将会忽略任务失败使下面的任务继续运行


#实例#
- name: test
  dnf:
    name: westos
    state: latest
  ignore_errors: yes
 
- name: create file
  file:
    path: /mnt/westos
    state: touch


#2.force_handlers#
#作用:
#当任务失败后play被终止也会调用触发器进程
#example

---
- name: install ftp server
  hosts: all
  force_handlers: yes
  tasks:
    - name: install vsftpd
      dnf:
        name: vsftpd
        stste: latest
      notify:
        - restsrt vsftpd
        - firewalld

    - name: configure ftp
      lineinfile:
        path: /etc/vsftpd/vsftpd.conf
        regexp: "^anonymous_enable="
        line: "anonymous_enable={ {state}}"
      notify:
        restart vsftpd

  handlers:
    - name: restart vsftpd

      service:
          name: vsftpd
          state: restarted
          enable: yes

    - name: firewalld
      firewalld:
        service: ftp
        state: enabled
        permanent: yes
        immediate: yes

#3.changed_when#
#作用:
#控制任务在何时报告它已进行更改

---
- name: apache change port
  hosts: 172.25.0.254
  force_handlers: yes
  vars:
    http_port: 8080
  tasks:
    - name: configure apache conf file
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen { { http_port }}"
      changed_when: true
      notify: restart apache


  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
        enabled: yes


#4.failed_when#
#当符合条件时强制任务失败

---
- name: test
  hosts: all
  tasks:
    - name: shell
      shell: echo hello
      register: westos
      failed_when: "'hello' in westos.stdout"

Ansible中的任务执行控制_第8张图片

Ansible中的任务执行控制_第9张图片 

Ansible中的任务执行控制_第10张图片 

Ansible中的任务执行控制_第11张图片 

 


#5.block#
block:         ##定义要运行的任务
rescue:        ##定义当block句子中出现失败任务后运行的任务
always:    ##定义最终独立运行的任务

Ansible中的任务执行控制_第12张图片

Ansible中的任务执行控制_第13张图片


#测试练习#
建立playbook  ~/westos.yml要求如下:
建立大小为1500M名为/dev/vdb1的设备
如果/dev/vdb不存在请输入:
     /dev/vdb  is not exist
如果/dev/vdb大小不足2G请输出:
     /dev/vdb is less then 2G
并建立800M大小的/dev/vdb1
此设备挂载到/westos上

---
- name: test
  hosts: all
  tasks:
    - name: check vdb
      debug:
        msg: /dev/vdb is not exist
      when: ansible_facts['devices']['vdb'] is not defined
    - name: create /dev/vdb1
      block:
        - name: check size 2G
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 2GiB
          when: ansible_facts['devices']['vdb'] is defined
          notify:
            - remove 2G
            - create vdb1
            - create filesystem
            - mount /dev/vdb1
      rescue:

- name: create 800M
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 800MiB
          notify:
            - create filesystem
            - mount /dev/vdb1
          when: ansible_facts['devices']['vdb'] is defined
      always:
        - name: create mount point
          file:
            path: /westos
            state: directory


  handlers:
    - name: remove 2G
      parted:
        device: /dev/vdb
        number: 1
        state: absent

    - name: create vdb1
      parted:
        device: /dev/vdb
        number: 1
        state: present
        part_end: 1500MiB

- name: create filesystem
      filesystem:
        fstype: xfs
        dev: /dev/vdb1
        force: yes

    - name: mount /dev/vdb1
      mount:
        path: /westos
        src: /dev/vdb1
        fstype: xfs
        state: mounted

Ansible中的任务执行控制_第14张图片

Ansible中的任务执行控制_第15张图片

你可能感兴趣的:(linux,udp)