Skip to content

Calcul

Addition

Convertion implicite avec la commande int

Calcul
- name: Addition
  hosts: localhost
  gather_facts: false

  vars:
    my_number: "1"

  tasks:
    - name: Add + 1
      ansible.builtin.set_fact:
        new_number: "{{ my_number | int + 1 }}"

    - name: Display
      ansible.builtin.debug:
        var: new_number
TASK [Display] ***************************
ok: [localhost] => {
    "new_number": "2"
}

Exemples

YAML
- name: Calcul
  hosts: localhost
  gather_facts: false

  tasks:

    - name: Ansible addition4 + 3 = 7
      ansible.builtin.debug:
        msg: "addition{{ 4 + 3 }}" # Ansible addition 7

    - name: Ansible arithmetic substraction 1
      ansible.builtin.debug:
        msg: "substraction {{ 4 - 3 }}" # Ansible arithmetic substraction 1

    - name: Multiplication 12
      ansible.builtin.debug:
        msg: "multiplication {{ 4 * 3 }}" # Multiplication 12

    - name: Ansible Modulo operation - find remainder 3
      ansible.builtin.debug:
        msg: "Modulo operation {{ 7 % 4 }}" # Ansible Modulo operation - find remainder 3

    - name: Ansible floating division 1.33333333333
      ansible.builtin.debug:
        msg: "floating division {{ 4 / 3 }}" # Ansible floating division 1.33333333333

    - name: Ansible arithmetic cube root 3.0
      ansible.builtin.debug:
        msg: "cube root {{ 27 | root(3) }}" # Ansible arithmetic cube root 3.0

    - name: Ansible arithmetic cube root 3.0
      ansible.builtin.debug:
        msg: "square root {{ 16 | root }}" # Square root 3.0

    - name: Ansible arithmetic power of a number 27
      ansible.builtin.debug:
        msg: "power {{ 3 | pow(3) }}" # Ansible arithmetic power of a number 27

    - name: Absolute value of a number 16.7
      ansible.builtin.debug:
        msg: "Absolute value {{ -16.7 | abs }}" # Absolute value of a number 16.7

    - name: Int conversion of a float value -19
      ansible.builtin.debug:
        msg: "Int conversion of float value {{ -19.23 | int }}" # Int conversion of a float value -19

    - name: Absolute value of a number 43
      ansible.builtin.debug:
        msg: "Multiple filters for getting absolute integer value of negative number {{ -43.33 | int | abs }}" # Absolute value of a number 43

    - name: 39.7 Round 40
      ansible.builtin.debug:
        msg: "Common Ansible round of a number {{ 39.7 | round }}" # Round 40

    - name: 39.4 Round 39
      ansible.builtin.debug:
        msg: "Common Rounding of a number {{ 39.4 | round }}" # Round 39

    - name: 39.5 Round 40
      ansible.builtin.debug:
        msg: "Common Rounding of a number {{ 39.5 | round }}" # Round 40

    - name: 39.779 'common' Round 40
      ansible.builtin.debug:
        msg: "Another way of Common Rounding of a number{{ 39.779 | round(0, 'common') }}" # Round 40

    - name: 39.779 (0, 'floor')  Round 39.0 - - still a float. Use integer filter to convert to integer
      ansible.builtin.debug:
        msg: "floor Rounding of a number {{ 39.779 | round(0, 'floor') }}" # Round 39.0 - - still a float. Use integer filter to convert to integer

    - name: 39.779 (1, 'floor') Round 39.7
      ansible.builtin.debug:
        msg: "Ansible floor Rounding of a number {{ 39.779 | round(1, 'floor') }}" # Round 39.7

    - name: 39.779 (2,' floor') Round 39.77
      ansible.builtin.debug:
        msg: "floor Rounding of a number {{ 39.779 | round(2, 'floor') }}" # Round 39.77

    - name: 39.779 (0, 'floor') Round 40.0 - still a float. Use integer filter to convert to integer
      ansible.builtin.debug:
        msg: "Ceiling Rounding of a number {{ 39.779 | round(0, 'floor') }}" # Round 40.0 - still a float. Use integer filter to convert to integer

    - name: 39.779 (1, 'floor') Round 39.8
      ansible.builtin.debug:
        msg: "Ceiling Rounding of a number {{ 39.779 | round(1, 'floor') }}" # Round 39.8

    - name: 39.779 (2, 'ceil') Round 39.78
      ansible.builtin.debug:
        msg: "Ceiling Rounding of a number {{ 39.779 | round(2, 'ceil') }}" # Round 39.78

    - name: Log 4
      ansible.builtin.debug:
        msg: "log of a number {{ 16 | log(4) }}" # 2.0
TASK [Ansible addition 7] ***************************************
    "msg": "addition7"
}

TASK [Ansible arithmetic substraction 1] ***************************************
ok: [localhost] => {
    "msg": "substraction 1"
}

TASK [Multiplication 12] ***************************************
ok: [localhost] => {
    "msg": "multiplication 12"
}

TASK [Ansible Modulo operation - find remainder 3] ***************************************
ok: [localhost] => {
    "msg": "Modulo operation 3"
}

TASK [Ansible floating division 1.33333333333] ***************************************
ok: [localhost] => {
    "msg": "floating division 1.3333333333333333"
}

TASK [Ansible arithmetic cube root 3.0] ***************************************
ok: [localhost] => {
    "msg": "cube root 3.0"
}

TASK [Ansible arithmetic cube root 3.0] ***************************************
ok: [localhost] => {
    "msg": "square root 4.0"
}

TASK [Ansible arithmetic power of a number 27] ***************************************
ok: [localhost] => {
    "msg": "power 27.0"
}

TASK [Absolute value of a number 16.7] ***************************************
ok: [localhost] => {
    "msg": "Absolute value 16.7"
}

TASK [Int conversion of a float value -19] ***************************************
ok: [localhost] => {
    "msg": "Int conversion of float value -19"
}

TASK [Absolute value of a number 43] ***************************************
ok: [localhost] => {
    "msg": "Multiple filters for getting absolute integer value of negative number 43"
}

TASK [39.7 Round 40] ***************************************
ok: [localhost] => {
    "msg": "Common Ansible round of a number 40.0"
}

TASK [39.4 Round 39] ***************************************
ok: [localhost] => {
    "msg": "Common Rounding of a number 39.0"
}

TASK [39.5 Round 40] ***************************************
ok: [localhost] => {
    "msg": "Common Rounding of a number 40.0"
}

TASK [39.779 'common' Round 40] ***************************************
ok: [localhost] => {
    "msg": "Another way of Common Rounding of a number40.0"
}

TASK [39.779 (0, 'floor')  Round 39.0 - - still a float. Use integer filter to convert to integer] ***************************************
ok: [localhost] => {
    "msg": "floor Rounding of a number 39.0"
}

TASK [39.779 (1, 'floor') Round 39.7] ***************************************
ok: [localhost] => {
    "msg": "Ansible floor Rounding of a number 39.7"
}

TASK [39.779 (2,' floor') Round 39.77] ***************************************
ok: [localhost] => {
    "msg": "floor Rounding of a number 39.77"
}

TASK [39.779 (0, 'floor') Round 40.0 - still a float. Use integer filter to convert to integer] ***************************************
ok: [localhost] => {
    "msg": "Ceiling Rounding of a number 39.0"
}

TASK [39.779 (1, 'floor') Round 39.8] ***************************************
ok: [localhost] => {
    "msg": "Ceiling Rounding of a number 39.7"
}

TASK [39.779 (2, 'ceil') Round 39.78] ***************************************
ok: [localhost] => {
    "msg": "Ceiling Rounding of a number 39.78"
}

TASK [Log 4] ***************************************
ok: [localhost] => {
    "msg": "log of a number 2.0"
}