Interview Prep/Ansible/Variables, Facts and Precedence Problems

`[WARNING]: TASK: app : Configure vhost: The loop variable 'item' is already in use. You should set the `loop_var` value in the `loop_control` option for the task to something else to avoid variable collisions and unexpected behavior.`

IntermediateTroubleshootingVariables, Facts and Precedence Problems
Answer

Likely causes → An outer loop runs include_tasks or include_role, and the included file loops again. Both use item, so the inner loop shadows the outer one.

Fix →

- ansible.builtin.include_tasks: vhost.yml
  loop: "{{ sites }}"
  loop_control:
    loop_var: site

Inside vhost.yml, refer to site.name and keep item for the inner loop.

Prevent → Always set loop_var when looping over include_*. It also makes the included files easier to read.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence Problems