ansible playbook play task执行顺序

当我们使用ansible-playbook执行playbook,我们可以指定一个或者多个playbook,而一个playbook中可以有多个play,这些play下面又会存在一个或者多个task,这些task会调用module去执行shell脚本,或者文件拷贝等等各种命令,具体可参考ansible playbook基本概念
我们知道在配置play的task时,可以配置pre_tasks, task和post_tasks,还可以配置roles,那这些task具体是怎么执行的呢?
下面这个图将解释具体执行过程
ansible playbook play task执行顺序_第1张图片

1.检查play中是否存在pre_tasks定义,存在的话则顺序执行pre_tasks中定义的所有tasks
2.如果存在pre_tasks定义,则检查是否存在触发handler,如存在则顺序执行相关触发handlers
3.检查是否存在roles定义,如存在则顺序执行roles下的所有tasks
4.检查是否存在task, 如存在则顺序执行所有定义的task
5.检查roles和task中是否存在触发handler,如存在顺序执行
6.检查是否存在post_tasks定义,存在则顺序执行post_tasks中定义的所有tasks
7.如果存在post_tasks,则检查post_tasks下面的tasks是否存在触发handlers,如存在则顺序执行

一个包含pre_tasks, roles和post_tasks的实例

---
- hosts: tag_ansible_group_windows_webservers
  serial: 1
  gather_facts: False
  connection: winrm
  vars:
    ansible_ssh_port : 5986

  # These are the tasks to run before applying updates:
  pre_tasks:
  - name: Remove host from load balancing pool
    local_action:
      module: ec2_elb
      region: us-east-1
      instance_id: "{{ ec2_id }}"
      ec2_elbs: "ansible-windows-demo-lb"
      wait_timeout: 330
      state: 'absent'

  roles:
  - web

  # These tasks run after the roles:
  post_tasks:
  - name: Wait for webserver to come up
    local_action: wait_for host={{ inventory_hostname }} port=80 state=started timeout=80

  - name: Add host to load balancing pool
    local_action:
      module: ec2_elb
      region: us-east-1
      instance_id: "{{ ec2_id }}"
      ec2_elbs: "ansible-windows-demo-lb"
      wait_timeout: 330
      state: 'present'

你可能感兴趣的:(DEVOPS,devops)