The goal here is to dynamically construct a menu from an available variable file
In this example I use ansible.builtin.pause module but I'm not sure this is the best way
variable file: 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:
playbook: 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'
Output:
PLAY [PLAY: > TEST] *******************************************************************************************************************************************************************************************************************************************************
TASK [Dynamicaly construct menu] ******************************************************************************************************************************************************************************************************************************************
[Dynamicaly construct menu]
Ansible options:
=====================================
1- Add...:
As you can see it displays only part of the main, not all.
Question:
How can I display all available options at once and save user selection, so I can run next tasks based on conditions?
I'm pretty sure the menu must be first generated and saved under one variable before sending to ansible.builtin.pause but I'm not sure how to achieve that.
Thanks for help