how print multiple name from multiple location in one playbook i

ghz 8months ago ⋅ 150 views

how print multiple name from multiple location in one playbook in localhost

---
- name: Generate Final Output
  hosts: localhost
  vars_files:
    - bharath_details.yml
    - mohan_details.yml
  tasks:
    - name: to print the varialble
      ansible.builtin.debug:
        msg: "{{ item.name }}, {{ item.location }}, {{item.qualification }}"
        loop:
          - "{{bharath}}"
          - "{{mohan}}" 


TASK [to print the varialble] ******************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined. 'item' is undefined\n\nThe error appears to be in '/home/ubuntu/name_vars/playbook_details.yml': line 8, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - name: to print the varialble\n      ^ here\n"}

PLAY RECAP *************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Answers

The error message indicates that the variable item is undefined. This is because the loop directive is inside the ansible.builtin.debug task, and Ansible does not recognize item within this context. Instead, you need to loop over the list of items outside of the task.

Here's the corrected playbook:

---
- name: Generate Final Output
  hosts: localhost
  vars_files:
    - bharath_details.yml
    - mohan_details.yml
  tasks:
    - name: to print the variable
      ansible.builtin.debug:
        msg: "{{ item.name }}, {{ item.location }}, {{ item.qualification }}"
      loop:
        - "{{ bharath }}"
        - "{{ mohan }}"

In this updated playbook, the loop directive is outside of the ansible.builtin.debug task, allowing Ansible to correctly loop over the list of items (bharath and mohan) and access their properties (name, location, qualification).