3

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

1 Answer 1

2

This is a case where including loops directly in the inlined jinja2 template will be hardly avoidable (and is therefore acceptable):

  - name: Dynamicaly construct menu
    pause:
      prompt: |-
        Ansible options:
        =====================================
        {% for option in menu.ansible.main %}
        {{ option.option }}- {{ option.name }}
        {% endfor %}
    register: result

Result with that fixed task:

$ 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
1
  • Zeitounator that works perfectly fine thanks for your help! Jun 2, 2022 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .