I am dealing with nested loops inorder to build dynamic host using add_host.
Outer Loop: with_items: "{{ command_result.stdout_lines }}" // gets me the list of users
Inner Loop: with_items: "{{ dest_ip.split(',') }}" // gets me the list of IP addresses seperated by comma (,)
The below code works fine.
- name: Add hosts
include_tasks: "{{ playbook_dir }}/gethosts.yml"
vars:
dest_ip: "{{ item.split('\t')[0] }}"
file_dets: "{{ item.split('\t')[1] }}"
USER: "anystring"
# USER: "{% if item.split('\t')[3] == 'FrontEnd' %}user1{% elif item.split('\t')[3] == 'BackEnd' %}user2{% else %}{% endif %}"
ansible_host: localhost
install_dir_pass: "anystring"
# install_dir: "{{ item.split('\t')[2] }}"
with_items: "{{ command_result.stdout_lines }}"
Below is my include_task gethost.yml file:
---
- name: Generate JSON data
add_host:
name: "{{ item }}"
groups: dest_nodes
ansible_user: "{{ USER_pass }}"
install_dir: "{{ install_dir_pass }}"
with_items: "{{ dest_ip.split(',') }}"
I get the below error if I uncomment either USER_pass or install_dir_pass and comment the existing value:
TASK [Generate JSON data] ***********************************************************************************************************************************
task path: /app/deployment/gethosts.yml:2
[WARNING]: 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.
fatal: [localhost]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: list object has no element 2\n\nThe error appears to be in '/app/gethosts.yml': line 2, column 4, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n - name: Generate JSON data\n ^ here\n"
}
NO MORE HOSTS LEFT ******************************************************************************************************************************************
PLAY RECAP **************************************************************************************************************************************************
localhost : ok=12 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Requesting a solution to this issue and an explanation of a few questions I have.
1. The dest_ip is read and works fine with .split(,) method inside of include_task get_hosts.yml file when other variables like install_dir_pass dont seem to work.
2. When USER_pass and install_dir_pass are given simple string "anystring" it works and is read fine inside of get_hosts.yml where as if they are assigned values using item.split('\t')[<num>] the playbook errors as above.
I have already tested using debug that all the entries in command_result are good and the values should be populated correctly as below.
- debug:
msg: "{{ item.split('\t')[0] }}"
with_items: "{{ command_result.stdout_lines }}"
- debug:
msg: "{{ item.split('\t')[1] }}"
with_items: "{{ command_result.stdout_lines }}"
- debug:
msg: "{{ item.split('\t')[2] }}"
with_items: "{{ command_result.stdout_lines }}"
- debug:
msg: "{{ item.split('\t')[3] }}"
with_items: "{{ command_result.stdout_lines }}"