这里的目标是从可用的变量文件动态构建菜单
在此示例中,我使用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
,但我不确定如何实现这一点。
感谢帮助
答案1
在这种情况下,直接在内联 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