Search code examples
ansibleansible-2.xansible-inventoryansible-template

The conditional check 'template is boolean' failed. while using the ansible assertion module


Here's my playbook.

- name: "Verify if the 'template' boolean has been set ?"  
  assert:
    that:      
      - template is defined
      - template is boolean    
    fail_msg: "'template' must be defined and be present as a boolean"

and i got the below error message,

MSG:
The conditional check 'template is boolean' failed. The error was: template error while templating string: no test named 'boolean'. String: {% if template is boolean %} True {% else %} False {% endif %}

Executing the ansible command like anisble-playbook -i ip "{'template': true}" sample.yml

How to do i fix this issue. without removing template is boolean parameter.


Solution

    • The parameter -e, --extra-vars is missing in your run-string
    shell> ansible-playbook -i ip "{'template': true}" sample.yml
    

    Because the first parameter without option is considered a playbook you should have seen the error message:

    ERROR! the playbook: {"template": true} could not be found


    • The Jinja test boolean should always be available the same way the test defined is. I can't reproduce the problem. The playbook below
    - hosts: localhost
    
      tasks:
    
        - assert:
            that:      
              - template is defined
              - template is boolean    
            fail_msg: template must be defined and type boolean
    

    works as expected. The test will pass if the variable template is defined and boolean

    shell> ansible-playbook -e '{"template": true}' pb.yml 
    
    PLAY [localhost] ******************************************************************************
    
    TASK [assert] *********************************************************************************
    ok: [localhost] => changed=false 
      msg: All assertions passed
    
    PLAY RECAP ************************************************************************************
    localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

    Otherwise, the play fails

    shell> ansible-playbook pb.yml 
    
    PLAY [localhost] ******************************************************************************
    
    TASK [assert] *********************************************************************************
    fatal: [localhost]: FAILED! => changed=false 
      assertion: template is defined
      evaluated_to: false
      msg: template must be defined and type boolean
    
    PLAY RECAP ************************************************************************************
    localhost: ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0