ansibleansible-inventoryansible-facts

Ansible how to convert boolean types true and false to corresponding strings


Here is my playbook, I didn't use the boolean type directly for compatibility with the previous version which was fetched from Inventory, I looked at the playbook and it was when: cluster_allinone == "true", which I might have forgotten to change when executing the script, so I wanted to override the value of cluster_allinone without modifying the previous playbook.

- name: check node counnt
  hosts: all:!gaop-vip-addresses
  tasks:
    - name: Get the number of nodes in group2
      set_fact:
        worker_count: "{{ groups['k8s-worker'] | length }}"

    - debug:
        var: worker_count

    - name: set cluster_allinone var
      set_fact:
        cluster_allinone: 'true'
      when: (worker_count | int) == 0

    - name: set cluster_allinone var
      set_fact:
        cluster_allinone: 'false'
      when: (worker_count | int) > 0

    - debug:
        var: cluster_allinone

    - debug:
        msg: single
      when: cluster_allinone == "true"

    - debug:
        msg: mutil
      when: cluster_allinone  == "false"

But cluster_allinone seems to be of boolean type, I've tried to_json and ternary('true', 'false').

    - name: set cluster_allinone var
      set_fact:
        temp: 'true'
      when: (worker_count | int) == 0

    - name: set cluster_allinone var
      set_fact:
        temp: 'false'
      when: (worker_count | int) > 0


--- try but fail


    - name: set cluster_allinone var
      set_fact:
        cluster_allinone: "{{temp | to_json}}"

    - name: set cluster_allinone var
      set_fact:
        cluster_allinone: "{{(worker_count | int) == 0}}"

    - name: set cluster_allinone var
      set_fact:
        cluster_allinone: "{{temp | ternary('true', 'false')}}"
ansible --version
ansible 2.4.5.0
  config file = /data/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /data/ansible/lib/ansible
  executable location = /data/ansible/bin/ansible
  python version = 2.7.12 (default, Nov 19 2018, 06:48:02) [GCC 5.4.0 20160609]

Spent way too much time on this one, any help is appreciated!


Solution

  • See Boolean variables. Indeed, the type of cluster_allinone is bool

        - set_fact:
            cluster_allinone: 'true'
        - debug:
            msg: "{{ cluster_allinone | type_debug }}"
    

    gives

      msg: bool
    

    Use it as a boolean. This will simplify the code

        - debug:
            msg: single
          when: cluster_allinone
    
        - debug:
            msg: multi
          when: not cluster_allinone
    

    gives

      msg: single
    

    Example of a complete playbook for testing

    - hosts: localhost
    
      tasks:
    
        - set_fact:
            cluster_allinone: true
    
        - debug:
            msg: single
          when: cluster_allinone
    
        - debug:
            msg: multi
          when: not cluster_allinone