I have written a global role to spin-up ec2 instance at the address: playbook/aws/spin-up-ec2/tasks/main.yaml and i am calling the role in a main file at the location playbook/servers/cluster/tasks/main.yaml to deploy three servers by calling the role in a loop for elastic_ips in the below fashion.
playbook/aws/spin-up-ec2/tasks/main.yaml
- name: test
hosts: localhost
become: false
gather_facts: false
vars_files:
- "vars/{{ env }}/main.yaml"
pre_tasks:
- name: Set all
add_host: hostname={{ item }} groupname=all-servers
with_items: '{{ elastic_ips }}'
- name: add new instance to host group at runtime
add_host: hostname={{ item }} groupname=nodes
with_items: '{{ elastic_ips[1:] }}'
tasks:
- name: Run the role for each elastic IP
include_role:
name: ../../roles/aws/spin-up-ec2-instance
vars:
elastic_ip: "{{ elastic_ips }}"
loop: "{{ elastic_ips }}"
- name: On all servers
hosts: all-servers
become: false
gather_facts: false
roles:
- install-dependencies
- install-package
- configure-package
- name: On all nodes
hosts: nodes
become: false
gather_facts: false
roles:
- setup-cluster
I am using the variable file at the below location: playbook/servers/cluster/vars/<prod/uat>/main.yaml
The var file contains the some variables which are common for all the three servers. But there are some variables which are different for different instances. So i want these variables to be passed in the loop. I am not able to achieve this. The variables which are different for different instances are mentioned below;
I have tried different approaches but i get errors like item is undefined or we were unable to convert to dict: dictionary requested, could not parse JSON or key=value.
Please help me resolve this issue and optimize the approach.
Note: I can’t use the host/inventory file also the playbook/aws/spin-up-ec2/tasks/main.yaml role is a global role and uses variables as defined in the var file. No changes can be made in the variable key parameter.
So maybe this section needs to be changed like so?
- name: Run the role for each elastic IP
include_role:
name: ../../roles/aws/spin-up-ec2-instance
vars:
#elastic_ip: "{{ elastic_ips }}"
elastic_ip: "{{ item }}"
loop: "{{ elastic_ips }}"
@jrglynn2 I tried this as well, but it gives me the below mentioned error.
TASK [../../roles/aws/spin-up-ec2-instance : Spin up the EC2 instance] *************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "argument 'tags' is of type <class 'list'> and we were unable to convert to dict: <class 'list'> cannot be converted to a dict"}
What is your argument ‘tags’ ? What are you trying to do with it in your role ?
You obviously have a type mismatch, trying to pass a list to something expecting a dict, but it’s hard to debug without seeing what your role is doing. Could you share you role’s relevant task(s) ? Also, how was your playbook changed exactly for you to get this error ?