My main objective is, I want to create a playbook with one task which will take list of docker images and list of hosts and then run one image on one host respectively or run list of images on one of host list supplied.
To make it clear on understanding consider below declaration
hostImageList:
- { hostList: { host: 'abc' }, imageList: { cName: 'tomcatNewImg', iName: 'tomcat:latest', iPort: 9999, volumes: '/temp' } }
- { hostList: { host: 'xyz'}, imageList: { cName: 'nginxNewImg', iName: 'nginx:latest', iPort: 8888, volumes: '/data' } }
abc and xyz are defined in my ansible/hosts file. Now tomcat image should be run on abc host and
nginx image on xyz.
There might be a need where I might need both tomcat , nginx on abc .
So, my playbook should be able to achieve this.
I was having challenges to loop hosts and image list together in a task in playbook.
So to achieve this, I am trying as below -
Wrote a playbook with a task which will take list of docker images to be run and run the images on the host name supplied through command prompt. I am using docker-container module of ansible for this.
I am have file img_vars.yml in which I have defined my list of items to be used in my playbook.
I need to pass the variable defined in this yml to playbook from commmand prompt to with_items
ANSIBLE VERSION
Ansible: 2.7.10 on RHEL 7.x
STEPS TO REPRODUCE
Created below yml files:
img_vars.yml
---
imageList:
- { cName: 'tomcatImg', iName: 'tomcat:latest', iPort: 9999, volumes: '/temp' }
imageNginxList:
- { cName: 'nginxImg', iName: 'nginx:latest', iPort: 8001, volumes: '/data' }
hostImageList:
- { hostList: { host: 'abc', user: 'tcs', pwd: 'tcs@12345' }, imageList: { cName: 'tomcatNewImg', iName: 'tomcat:latest', iPort: 9999, volumes: '/temp' } }
- { hostList: { host: 'xyz', user: 'tcs', pwd: 'tcs@12345' }, imageList: { cName: 'nginxNewImg', iName: 'nginx:latest', iPort: 8888, volumes: '/data' } }
my main playbook yml file
---
- name: Ansible Looping Expirement
hosts: "{{ gethostname }}"
gather_facts: true
become: yes
become_method: sudo
vars_files:
- img_vars.yml
tasks:
- name: Installing docker images using loops in Ansible
docker_container:
name: "{{ item.cName }}"
image: "{{ item.iName }}"
state: started
# exposed_ports:
# - "{{ item.iPort }}"
volumes: "{{ item.volumes }}"
with_items: "{{ ' getwithlist' }}"
Running my playbook with below command
ansible-playbook --ask-become-pass main.yml --extra-vars "gethostname=abc getwithlist=imageList"
Here I am trying to pass “imageList” which is defined in img_vars.yml from command prompt through getwithlist variable.
EXPECTED RESULTS
Playbook executed successfully and run docker image “tomcat” on host supplied through gethostname .
getwithlist in with_items should be replaced by imageList and the value of imageList in img_vars.yml should be picked up in with_items. So if this variable replacement happens correctly, tomcat image will be running on remote host.
When I want to install another image on another host, i will run playbooks with changed parameters as below :
ansible-playbook --ask-become-pass main.yml --extra-vars "gethostname=xyz getwithlist=imageNginxList"
ACTUAL RESULTS
Am getting below error
"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'iName'\n\nThe error appears to have been in '/u01/HomeDir/e166110/ansible_poc/loops/dynamic_loop_docker.yml': line 10, column 6, 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: Installing docker images using loops in Ansible\n ^ here\n"
Am doing a mistake but not sure where am doing. Can someone guide me please ?
Is my main objective which I described in beginning is achievable ?