0

I try to increment a variable for each item contained in "with_items".

For exemple, I would like that the "counter" variable to increment by 1 for each item

    - set_fact:
        number: 0
    - set_fact:
        esx_conf:
          counter : {{ number | int + 1 }}
          value1 : {{ item.ansible_facts.hardware.memorySize }}
          value2 : {{ item.ansible_facts.summary.quickStats.overallMemoryUsage }} 
          value3 :"{{ item.item.name }}
      with_items: "{{ esxi_infos.results }}"
      register: all_esx_conf

It would seem that this is not possible. I tried to play with "with_sequence" or "with_nested" as well.

Do you have any idea ?

Thanks & regards,

beta
  • 1
  • 3
  • Have you tried `with_items: "{{ range( esxi_infos.results | length) | zip(esxi_infos.results) | list }}"` which should do the same thing that `enumerate` does in python? – mdaniel Apr 16 '21 at 15:51
  • @Beta, Please view profile for contact info and get in touch. Thanks – Wilson Hauck Mar 13 '23 at 14:54

2 Answers2

0

Ansible is a declarative language, which makes that kind of thing tricky. The whole point of the language is to just describe the tasks that you want done, and not the way to do them, which makes procedural tasks like those difficult to achieve.

You can take a look a the following stack overflow answer, that might help you getting what you are trying to achieve:

Using set_facts and with_items together in Ansible

However, maybe if you could give a little more info on what the high level objective is, there might be a better way to achieve it using Ansible than looping and incrementing a counter.

Ricardo
  • 472
  • 3
  • 6
0

Thanks for your answer. My goal is to have a list like this :

  1. ESX 1 - RAM 50% - CPU 40%
  2. ESX 2 - RAM 50% - CPU 40%
  3. ESX 3 - RAM 50% - CPU 40% ....

Data is collected with "community.vmware.vmware_host_facts". Currently, here is my playbook :

    - name: "ESX information"
      community.vmware.vmware_host_facts:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        validate_certs: no
        esxi_hostname: "{{ item.name }}"
        schema: vsphere
        properties:
          - hardware.memorySize
          - hardware.cpuInfo.numCpuCores
          - hardware.cpuInfo.hz
          - summary.quickStats.overallMemoryUsage
          - summary.quickStats.overallCpuUsage
          - config.fileSystemVolume.mountInfo
      with_items: "{{ liste_ESX | json_query('*.value') }}"
      register: esxi_infos

    - set_fact:
        esx_conf: "{{ item.item.name }} - RAM : {{ (( item.ansible_facts.summary.quickStats.overallMemoryUsage / ( item.ansible_facts.hardware.memorySize / (1024*1024) ) ) * 100 ) | int }}%  - CPU : {{ (( item.ansible_facts.summary.quickStats.overallCpuUsage / ( item.ansible_facts.hardware.cpuInfo.numCpuCores * ( item.ansible_facts.hardware.cpuInfo.hz / 1000000 ) ) ) * 100) | int }}%"
      with_items: "{{ esxi_infos.results }}"
      register: all_esx_conf
      
    - set_fact:
        esx_conf: "{{ item.ansible_facts.esx_conf }}"
      with_items: "{{ all_esx_conf.results }}"
      register: all_esx_conf

    - debug:
        msg: "{{ all_esx_conf.results | map(attribute='ansible_facts.esx_conf') | list }}"

The result is :

TASK [debug] ***************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "ESX 1 - RAM : 74%  - CPU : 11%",
        "ESX 2 - RAM : 70%  - CPU : 11%",
        "ESX 3 - RAM : 65%  - CPU : 13%",
        ...
    ]
}

After that, I have a pause :

    - pause:
        prompt: "ESX chosen ?"    
      register: prompt

So, with a counter for each "ITEM", the user will type 1,2,3...to choose the ESX.

What do you think? Thanks !

beta
  • 1
  • 3