This error usually comes in Ansible versions below 2.5. The loop command is not available in Ansible versions less than 2.5.
If you are facing this error, you have two options to solve this error
- Upgrade Ansible version to 2.5+
- Use with_items instead of loop command in the script. The same code should work.