0

Here are the tasks:

        - name: set value
          set_fact:
            is_enabled: 'yes'

        - name: debug is_enabled value
          debug:
            var: is_enabled

This prints value as "true" instead of "yes". I tried typecasting to string. Also tried:

        - name: set value
          set_fact:
            is_enabled: >
              yes

        - name: debug is_enabled value
          debug:
            var: is_enabled

This prints "yes/n".

Is there any way I can set a variable value to "YES".

codec
  • 7,978
  • 26
  • 71
  • 127
  • 1
    you can use `var` over `set_fact` , also see https://stackoverflow.com/questions/47877464/how-exactly-does-ansible-parse-boolean-variables – P.... Mar 08 '22 at 19:04
  • I have a when clause to set the variable. I can pass a variable to my playbook but cannot set it conditionally I beleive – codec Mar 08 '22 at 19:07
  • 2
    what you are expecting is discussed here and closed as a language feature. https://github.com/ansible/ansible/issues/12519 – P.... Mar 08 '22 at 19:11
  • 1
    Although you can do `msg: "{{ 'yes' if is_enabled}}"` – P.... Mar 08 '22 at 19:48

1 Answers1

0

As mentioned in the comment about Ansible Issue #12519 it seems to be a language feature of YAML. According Ansible set_fact type cast one could set DEFAULT_JINJA2_NATIVE

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    yes1: yes
    yes2: 'yes'
    yes3: "yes"

  tasks:

  - name: Set yes
    set_fact:
      yes4: 'yes'
      yes5: "yes"

  - name: Show values
    debug:
      msg: |
        yes1 is {{ yes1 | type_debug }} and has value {{ yes1 }}
        yes2 is {{ yes2 | type_debug }} and has value {{ yes2 }}
        yes3 is {{ yes3 | type_debug }} and has value {{ yes3 }}
        yes4 is {{ yes4 | type_debug }} and has value {{ yes4 }}
        yes5 is {{ yes5 | type_debug }} and has value {{ yes5 }}

resulting into an output of

ANSIBLE_JINJA2_NATIVE=True ansible-playbook set_yes_string.yml
[WARNING]: jinja2_native requires Jinja 2.10 and above. Version detected: 2.7.2. Falling back to default.
...
TASK [Show values] *************************
ok: [localhost] =>
  msg: |-
    yes1 is bool and has value True
    yes2 is AnsibleUnicode and has value yes
    yes3 is AnsibleUnicode and has value yes
    yes4 is AnsibleUnicode and has value yes
    yes5 is AnsibleUnicode and has value yes

Furthermore, just only setting a bool to string filter will not work.

U880D
  • 8,601
  • 6
  • 24
  • 40