Monday, June 18, 2018

reverse task sequence in Ansible

I found this solution to be handy for my need that I need to reverse the task sequence when I remove the instance from the cloud, basically, all task are the same just like when creating the instance, only for removing it, the task should start from the last one, here is my ansible playbook, as you can see I have list of task that needs to be performed in sequence, put them in to the list is the trick here, so I can have them sequenced in reverse.

============================

- name: Create/Destroy instance
  hosts: localhost
  vars:

    cmd_sequence:  [  "projects.yml", "members.yml", "groups.yml", "rules.yml", "networks.yml", "subnets.yml", "ports.yml", "servers.yml", "router.yml", "properties.yml" ]

  tasks:

    - name: DESTROY Instance from the Cloud
      vars:
        task_name: "tasks/{{ item }}"
      include_tasks: "{{ task_name }}"
      with_items:
        - "{{ cmd_sequence[::-1] }}"
      when: deploytype=="DESTROY"

    - name: CREATE Instance in the Cloud
      vars:
        task_name: "tasks/{{ item }}"
      include_tasks: "{{ task_name }}"
      with_items:
        - "{{ cmd_sequence }}"
      when: deploytype=="CREATE"

=============================

No comments:

current

last archive