ansible-playbook task specified location execution

order of execution

insert image description here
1. Check whether there is a pre_tasks definition in the play. If it exists, execute all the tasks defined in pre_tasks sequentially. 2.
If there is a pre_tasks definition, check whether there is a trigger handler. If it exists, execute related trigger handlers sequentially.
3. Check whether there is a roles definition , if it exists, execute all tasks under roles sequentially
4. Check whether there is a task, if it exists, execute all defined tasks sequentially
5. Check whether there is a trigger handler in roles and tasks, if it exists, execute sequentially
6. Check whether there is a definition of post_tasks If it exists, execute all tasks defined in post_tasks sequentially.
7. If post_tasks exists, check whether the tasks under post_tasks exist to trigger handlers, and execute them sequentially if they exist

An instance containing pre_tasks, roles and 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'


specified execution

The official generally provides the following features: It is very helpful for testing or debugging new playbooks.

  • 1:tag
  • 2:start-at
  • 3:skip-tags
  • 4:step

Demo playbook:

---
- name: shutdown etcd
  service: name=etcd state=stopped enabled=no
  ignore_errors: yes
  tags:
      - shutdown

- name: del etcd dir
  shell: 'rm -rf {
    
    { item }}'
  with_items:
      - {
    
     ETCD_DIR }
  tags:
      - deldir

- name: create etcd dir
  file:
       path: '{
    
    { item }}'
       state: directory
       mode: 755
  with_items:
      - {
    
     ETCD_DIR }
  tags:
      - mkdir

–tags

If you just want to run "shutdown" and "mkdir" in the playbook, you can do

ansible-playbook example.yml – tags “shutdown,mkdir”
ansible-playbook -i inventory/local/inventory.ini --become --become-user=root cluster.yml --tags  etchosts

The tag feature is a good feature, but if you really want to maintain a large playbook, it is recommended to split the playbook into multiple playbooks by function or application, and then include other sub-playbooks in the main playbook, which is convenient for maintenance It is also easy to manage.

–start-at

Run the playbook from the specified task and run the playbook step by step. If you want to start the playbook from the specified task, you can use the --start-at option: The following command will start
your playbook in the task named "deldir".

ansible-playbook playbook.yml --start-at="deldir"

–skip-tags

If you only want to execute all tasks in the playbook except a specific task, you can do this:

ansible-playbook example.yml – skip-tags “deldir”

–step

ansible-playbook playbook.yml --step

比如你有个名为``deldir``的任务,playbook执行到这里会停止并询问:

Perform task: deldir (y/n/c):

“y”回答会执行该任务,
”n”回答会跳过该任务,
而”c”回答则会继续执行剩余的所有任务而不再询问你.

mix

ansible-playbook playbook.yml --tags myrole --start-at-task "task_name"

reference:

Guess you like

Origin blog.csdn.net/xixihahalelehehe/article/details/130096634