Search code examples
ansiblejinja2

Ansible set_fact value of No converted into False


I'm having an annoying issue with set_fact, I set a var with value No but once I use the var in a template Ansible converts it to False. Is there a way around this YAML Norway problem? A single-quoted 'No' is converted to an unquoted False in the file.


Solution

  • We can use multi line scalar YAML syntax to get around this and have the text No in the file contents.

    Consider a simple text file sample.txt as below:

    works? Yes
    

    I am using an example with lineinfile (Linux), but should work similarly for Windows. A simple playbook with below tasks:

    - set_fact:
        var1: >
          No
    - lineinfile:
        path: /tmp/sample.txt
        line: "works? {{ var1 }}"
        regexp: "^works"
    

    When this playbook is run:

    -works? Yes
    +works? No