Search code examples
ansibleintinteger-arithmetic

Ansible: How to do arithmetic for integer variable in set_fact module?


Does anyone know how to do arithmetic assignment for integer variables in set_fact module? Currently, I managed to do it by using String variable using Jinja2 template like this:

    - set_fact:
        flagStr: "0"

    - name: Add by one one one
      set_fact: 
        flagStr: "{{flagStr|int + 1 + 1 + 1}}"

    - name: debug total
      debug:
        msg:
          - "{{flagStr}}"

The result will return 3 as expected output. However, I don't know how to do that for integer variables as it will return an error when doing it the same way.

I have tried it to be like this:

- set_fact:
        flagInt: 0

    - name: Add by one
      set_fact: 
        flagInt: flagStr + 1           #will return as 'flagInt + 1' 
        flagInt: {{flagInt}} + 1       #will return as syntax error
        flagInt: "{{flagInt}}" + 1     #will return as syntax error
        flagInt: "{{flagInt}} + 1"     #will return as '0 + 1'

    - name: debug total
      debug:
        msg:
          - "{{flagInt}}"

Solution

  • Q: "I don't know how to do that for integer variables as it will return an error when doing it the same way."

    A: You haven't done it the same way. All four options in the second example are different compared to the first one. In fact, both string and integer give the same result when used in the first example.

    It's necessary to evaluate the expression "{{ }}". If the variable flagStr is an integer flagStr: 0 it's not necessary to convert it to an integer. It does not hurt either. The task below works as expected.

        - set_fact:
            flagStr: 0
        - set_fact:
            flagStr: "{{ flagStr + 1 + 1 + 1 }}"
        - debug:
            var: flagStr
    

    Notes

    1. It is useful to understand that flagStr: 0 is an integer, but flagStr: "{{ flagStr + 1 + 1 + 1 }}" is a string
        - set_fact:
            flagStr: 0
        - debug:
            var: flagStr|type_debug
        - set_fact:
            flagStr: "{{ flagStr + 1 + 1 + 1 }}"
        - debug:
            var: flagStr
        - debug:
            var: flagStr|type_debug
    

    gives

    TASK [set_fact] ****************************************************
    ok: [localhost]
    
    TASK [debug] *******************************************************
    ok: [localhost] => 
      flagStr|type_debug: int
    
    TASK [set_fact] ****************************************************
    ok: [localhost]
    
    TASK [debug] *******************************************************
    ok: [localhost] => 
      flagStr: '3'
    
    TASK [debug] *******************************************************
    ok: [localhost] => 
      flagStr|type_debug: str
    
    1. It's necessary to convert the string to an integer if you want to repeat the operation
        - set_fact:
            flagStr: "{{ flagStr|int + 1 + 1 + 1 }}"
    

    Otherwise, the task will fail

    msg: 'Unexpected templating type error occurred on ({{ flagStr + 1 + 1 + 1 }}): can only concatenate str (not "int") to str'