Variable to be selected from the options

18 views
Skip to first unread message

Veera

unread,
Jul 26, 2023, 8:39:40 AM7/26/23
to Ansible Project
Hi,

I have the below variable defined  in my playbook.
reboot_option:
- IfRequired
- Never
 - Always

while calling the same  variable from the playbook ,   the value "IfRequired'  must be the default.
 "rebootSetting": "{{ reboot_option | default(Ifrequired) }}" 

While executing the playbook without extra-vars reboot_option then by "ifrequired " need to the default,.
If the playbook is executed with -e "reboot_option=Never" or "Always"  then it should select the given option.

how to call the dictionary items with default as "Ifrequired"? 
Or I have to declare  the variables in a different way ?





Rowe, Walter P. (Fed)

unread,
Jul 26, 2023, 9:01:15 AM7/26/23
to ansible...@googlegroups.com

Using ansible's order of precedence you can set the default value in the playbook and override it with extra_vars.


override.yaml:


---

- name: test overriding playbook variable

  hosts: all

  become: false

  gather_facts: false

  vars:

    rebootSetting: "ifRequired"

  tasks:

    - name: reboot setting

      debug: var=rebootSetting



Execution: note the first test does not provide an extra_var value for rebootSetting, while the second and third do provide one.


% ansible-playbook -i localhost, override.yaml 


PLAY [test overriding playbook variable] *******************************************************************************


TASK [reboot setting] **************************************************************************************************

ok: [localhost] => {

    "rebootSetting": "ifRequired"

}


PLAY RECAP *************************************************************************************************************

localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


% ansible-playbook -i localhost, override.yaml -e rebootSetting=Never


PLAY [test overriding playbook variable] *******************************************************************************


TASK [reboot setting] **************************************************************************************************

ok: [localhost] => {

    "rebootSetting": "Never"

}


PLAY RECAP *************************************************************************************************************

localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


% ansible-playbook -i localhost, override.yaml -e rebootSetting=Always


PLAY [test overriding playbook variable] *******************************************************************************


TASK [reboot setting] **************************************************************************************************

ok: [localhost] => {

    "rebootSetting": "Always"

}


PLAY RECAP *************************************************************************************************************

localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   



Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/90154402-8d8b-4041-8a9f-d1bbea023a6en%40googlegroups.com.

Veera

unread,
Jul 26, 2023, 10:44:35 AM7/26/23
to Ansible Project
Thanks Walter
Reply all
Reply to author
Forward
0 new messages