簡體   English   中英

如何在創建 openstack 備份時重復相同的命令?

[英]How to repeat the same command in creating the openstack backup?

我獲取了 OpenStack 實例in-use ”卷,並將這些卷 ID 過濾到一個文件中,它必須從中進行備份

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

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

錯誤顯示像

**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": []}**

有人可以說如何在 ansible 劇本中從該文件(具有卷 ID)創建卷備份嗎?

目前,您只為with_items,/home/volumeid ,這意味着您的循環將只針對文件名而不是其內容迭代一次。

如果您在 localhost 或遠程主機上的slurp模塊上,則需要使用file查找。 例子:

對於localhost

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

對於遠程主機:

  - 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() }}"

只需一行 shell 命令:

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 .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM