How to run Ansible playbook tasks in tag order

I want to run tasks in an inaccessible play in the order of tags given in --tags

My puzzle game

---
- hosts: all
  remote_user: root
  vars:
    file_path: '{{filename}}'
  tasks:
    - name: Delete user
      user:
        name: "{{username}}"
        state: absent
        remove: yes
      tags:
        - delete_user

    - name: Create user
      user: 
        name: "{{username}}"
        shell: /bin/bash
        groups: "{{groupname}}"
        password: "{{ password |password_hash('sha512') }}"
      tags:
        - create_user

    - name: Add ssh key
      authorized_key:
        user: "{{username}}"
        key: "{{lookup('file', 'file_path')}}"
        exclusive: yes
      tags:
        - add_ssh_key

Launch Ansible

ansible-playbook createuser.yml --extra-vars "username=hello password=helloworld groupname=something filename=/path/to/filename" --tags=create_user,add_ssh_key,delete_user

Expected Result

TASK: [Create user] *********************************************************** 
changed: [ip address]

TASK: [Add ssh key] *********************************************************** 
changed: [ip address]

TASK: [Delete user] *********************************************************** 
ok: [ip address]

Output

TASK: [Delete user] *********************************************************** 
ok: [ip address]

TASK: [Create user] *********************************************************** 
changed: [ip address]

TASK: [Add ssh key] *********************************************************** 
changed: [ip address]

Order tagged tags

create_user, add_ssh_key, delete_user

But done ok

delete_user, create_user, add_ssh_key,

+4
source share
2 answers

These are not the same tags, and in Ansible there is no way to do this. Tasks are always performed in the order in which they were defined in the task files and / or in the order that roles were added to the playbook / play.

If you want to target specific tasks so that you can call up the playlist several times using a single tag.

ansible-playbook ... --tags=create_user
ansible-playbook ... --tags=add_ssh_key
ansible-playbook ... --tags=delete_user

bash script, . ( , )

+4

, , playbook, . , playbook, , - . , , .

playbook:

  - hosts: localhost

    tasks:
      - name: Third task
        shell:
        tags: "third"

      - name: Second task
        shell:
        tags: "second"

      - name: First task
        shell:
        tags: "first"

:

ansible-playbook -c 'local' test.yml --tags=first,second,third --list-tasks

:

playbook: test.yml

play #1 (localhost): localhost        TAGS: []
  tasks:
    Third task        TAGS: [third]
    Second task       TAGS: [second]
    First task        TAGS: [first]

:

ansible-playbook -c 'local' test.yml --tags=third,first,second --list-tasks

( ):

playbook: test.yml

play #1 (localhost): localhost        TAGS: []
  tasks:
    Third task        TAGS: [third]
    Second task       TAGS: [second]
    First task        TAGS: [first]

:

- hosts: localhost

  tasks:
    - name: First task
      shell:
      tags: "first"

    - name: Second task
      shell:
      tags: "second"

    - name: Third task
      shell:
      tags: "third"

:

ansible-playbook -c 'local' test.yml --tags=third,first,second --list-tasks

( ):

playbook: test.yml

play #1 (localhost): localhost        TAGS: []
  tasks:
    First task        TAGS: [first]
    Second task       TAGS: [second]
    Third task        TAGS: [third]
+2

Source: https://habr.com/ru/post/1622062/


All Articles