0

I took volumes 'in-use' of OpenStack instance and filtered those volume ids into a file from which it has to make a backup

shell: openstack volume list | grep 'in-use' | awk '{print $2}' > /home/volumeid   

shell: openstack volume backup create {{ item }}
with_items:
- /home/volumeid

error shows like

**failed: [controller2] (item=volumeid) => {"ansible_loop_var": "item", "changed": true, "cmd": "openstack volume backup create volumeid", "delta": "0:00:03.682611", "end": "2022-09-26 12:01:59.961613", "item": "volumeid", "msg": "non-zero return code", "rc": 1, "start": "2022-09-26 12:01:56.279002", "stderr": "No volume with a name or ID of 'volumeid' exists.", "stderr_lines": ["No volume with a name or ID of 'volumeid' exists."], "stdout": "", "stdout_lines": []}
failed: [controller1] (item=volumeid) => {"ansible_loop_var": "item", "changed": true, "cmd": "openstack volume backup create volumeid", "delta": "0:00:04.020051", "end": "2022-09-26 12:02:00.280130", "item": "volumeid", "msg": "non-zero return code", "rc": 1, "start": "2022-09-26 12:01:56.260079", "stderr": "No volume with a name or ID of 'volumeid' exists.", "stderr_lines": ["No volume with a name or ID of 'volumeid' exists."], "stdout": "", "stdout_lines": []}**

Can someone say how to create the volume backup from that file (which has volume ids) in the ansible playbook?

P....
  • 17,421
  • 2
  • 32
  • 52

2 Answers2

0

Currently, you are supplying only one element to the with_items, that is, /home/volumeid, meaning your loop will iterate only once for the file name and not its contents.

You need to use the file lookup if you are on localhost or the slurp module on the remote host. Example:

For the localhost:

- name: Show the volume id from the file
  debug:
    msg: "{{ item }}"
  loop: "{{ lookup('file', '/home/volumeid').splitlines() }}"

For the remote host:

  - name: Alternate if the file is on remote host
    ansible.builtin.slurp:
      src: /home/volumeid
    register: vol_data

  - name: Show the volume id from the file
    debug:
      msg: "{{ item }}"
    loop: "{{ (vol_data['content'] | b64decode).splitlines() }}"
P....
  • 17,421
  • 2
  • 32
  • 52
0

Just one line shell command:

openstack volume list --status in-use -c ID -f value | xargs -n1 openstack volume backup create

One advice, don't use the hardcode command like this grep 'in-use' or awk '{print $2}', openstack has it's optional arguments and output formatters, check it by openstack command [sub command] -h.

Victor Lee
  • 2,467
  • 3
  • 19
  • 37