Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - The error was: 'item' is undefined

Tags:

ansible

esxi

I am new to ansible. I am trying to get info from an esxi about the VM installed on it. No vcentre available.

I have created the playbook. Connection is working however I am getting the error in the Task debug. Any help is much appreciated. Thanks

Playbook:

  - hosts: esxi
   gather_facts: false
   become: false
   tasks:
     - name: Gather all registered virtual machines
       community.vmware.vmware_vm_info:
         hostname: '{{ hostname }}'
         username: '{{ ansible_user }}'
         password: '{{ password }}'
         validate_certs: no
       delegate_to: localhost
       register: vminfo

     - debug:
        msg: "{{ item.guest_name }}, {{ item.ip_address }}"
        with_items:
        - "{{ vminfo.virtual_machines }}"

The Error is:

TASK [debug] *************************************************************************************************************************
fatal: [192.168.233.202]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/home/sciclunam/task4/playbook11.yaml': line 14, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n     - debug:\n       ^ here\n"}

PLAY RECAP ***************************************************************************************************************************
192.168.233.202            : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
like image 575
sciclunam Avatar asked Oct 29 '25 17:10

sciclunam


1 Answers

The correct indentation is very necessary in Ansible. Your with_items is not correctly indented.

Please notice the indentation of wiith_items

Here is my sample

# Create Directory Structure
- name: Create directory application
  file:
    path: "{{item}}"
    state: directory
    mode: 0755
  with_items:
     - "/opt/project1"
     - "/opt/project1/script"
     - "/opt/project1/application"
like image 54
Muhammad Tariq Avatar answered Nov 01 '25 13:11

Muhammad Tariq