Vijayendar Gururaja
Vijayendar Gururaja

Reputation: 860

How to loop over a series of tasks in ansible

I am trying to unmount filesystems with the below playbook.

vars:
unmountlist:
  - "/DATA1"
  - "/DATA2"

tasks:
  - name: unmount
    mount:
    path: "{{ item }}"
    state: unmounted
    with_items:
     - "{{ unmountlist }}"
    register: output
    ignore_errors: true

  - debug:
    msg: "{{ output }}"

  - name: YE unmount persistant
    mount:
    path: "{{ item }}"
    state: absent
    with_items:
     - "{{ unmountlist }}"

 - name: Lazy unmount
   command: umount -l "{{ item }}"
   when: output.changed == false
   with_items: "{{ unmountlist }}"

The debug section looks like this:

ok: [host001] => {
"msg": "error is {'msg': u'All items completed', 'failed': True, 'changed': False, 'results': [{'_ansible_parsed': True, 'changed': False, '_ansible_no_log': False, 'item': u'/DATA1', '_ansible_item_result': True, u'failed': True, u'invocation': {u'module_args': {u'src': None, u'dump': None, u'boot': u'yes', u'fstab': None, u'passno': None, u'fstype': None, u'state': u'unmounted', u'path': u'/DATA1', u'opts': None}}, u'msg': u'Error unmounting /DATA1: umount.nfs: /DATA1: device is busy\\n'}, {'_ansible_parsed': True, 'changed': False, '_ansible_no_log': False, 'item': u'/DATA2', '_ansible_item_result': True, u'failed': True, u'invocation': {u'module_args': {u'src': None, u'dump': None, u'boot': u'yes', u'fstab': None, u'passno': None, u'fstype': None, u'state': u'unmounted', u'path': u'/DATA2', u'opts': None}}, u'msg': u'Error unmounting /DATA2: umount.nfs: /DATA2: device is busy\\n'}]}"

I am trying to achieve the below.

  1. lazy unmount the only the filesystem that returns error "device is busy". I cant see how to read the variable from above debug that contains string "device is busy" and how to unmount only the filesystem that returns this error.

Upvotes: 0

Views: 3343

Answers (1)

PrasadK
PrasadK

Reputation: 780

What you can do is put the tasks in a tasks file and loop over the tasks file using unmountlist.

This is how your tasks file (umounts_tasks.yml) will look like - tasks file

Then within your play you can use include_tasks to include the above tasks file and loop over it with the unmountlist. So your play will look like this - play.yml

Let me know if this worked. :)

Upvotes: 2

Related Questions