Ansible
如何使用 ansible.builtin.pause 模組動態建構菜單?
這裡的目標是從一個可用的變數文件動態地建構一個菜單
在此範例中,我使用
ansible.builtin.pause
模組,但我不確定這是最好的方法變數文件:
vars.yml
--- menu: ansible: main: - option: 1 name: "Add..." - option: 2 name: "Delete..." - option: 3 name: "Empty..." add: - option: 1 name: "Add something..." - option: 2 name: "Add something to..." delete: empty: ssh: main:
劇本:
test.yml
- name: "PLAY: > TEST" hosts: localhost gather_facts: no vars_files: vars.yml pre_tasks: - name: Dynamicaly construct menu pause: prompt: "\n Ansible options:\n =====================================\n {{item.option}}- {{item.name}}" register: result loop: "{{menu.ansible.main}}" - debug: msg: "Option 1 was selected" when: result.user_input == '1'
輸出:
PLAY [PLAY: > TEST] ******************************************************************************************************************************************************************************************************************************************************* TASK [Dynamicaly construct menu] ****************************************************************************************************************************************************************************************************************************************** [Dynamicaly construct menu] Ansible options: ===================================== 1- Add...:
如您所見,它僅顯示主要部分,而不是全部。
問題:
如何一次顯示所有可用選項並保存使用者選擇,以便根據條件執行下一個任務?
我很確定必須先生成菜單並將其保存在一個變數下,然後再發送到,
ansible.builtin.pause
但我不確定如何實現。感謝幫助
在這種情況下,直接在內聯的 jinja2 模板中包含循環幾乎是不可避免的(因此是可以接受的):
- name: Dynamicaly construct menu pause: prompt: |- Ansible options: ===================================== {% for option in menu.ansible.main %} {{ option.option }}- {{ option.name }} {% endfor %} register: result
該固定任務的結果:
$ ansible-playbook test.yml PLAY [PLAY: > TEST] *************************************************************************************************************** TASK [Dynamicaly construct menu] ************************************************************************************************** [Dynamicaly construct menu] Ansible options: ===================================== 1- Add... 2- Delete... 3- Empty... : 1^Mok: [localhost] TASK [debug] ********************************************************************************************************************** ok: [localhost] => { "msg": "Option 1 was selected" } PLAY RECAP ************************************************************************************************************************ localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 $ ansible-playbook test.yml PLAY [PLAY: > TEST] *************************************************************************************************************** TASK [Dynamicaly construct menu] ************************************************************************************************** [Dynamicaly construct menu] Ansible options: ===================================== 1- Add... 2- Delete... 3- Empty... : 2^Mok: [localhost] TASK [debug] ********************************************************************************************************************** skipping: [localhost] PLAY RECAP ************************************************************************************************************************ localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0