Nishant Singh
Nishant Singh

Reputation: 3209

How to use list variables defined using Ansible?

I have defined a variable in my defaults as a list:

aws-sec-group-name:
    - "es-external"
    - "elasticsearch-production"

I am trying to use the above variable in my task playbook as follows:

---
  - name: Create EC2 instances
    ec2:
      keyname: "{{ aws-key-name }}"
      #group: "{{ aws-sec-group-name }}"
      instance_type: "{{ aws-instance-type }}"
      image: "{{ aws-ami }}"
      wait: yes
      wait_timeout: 500
      count: 2
      instance_tags:
        Name: "{{ aws-tag-name }}"
      vpc_subnet_id: "{{ subnet-id }}"
      group: ["{{ aws-sec-group-name.[0] }}","{{ aws-sec-group-name.[1] }}"].

Which is not the right way.

Can someone tell me how to use list variable?

And also since the count is 2, I am also interested in knowing if i can add -1- and -2- in the Name tag?

Upvotes: 0

Views: 76

Answers (1)

techraf
techraf

Reputation: 68649

Can someone tell me how to use list variable?

Here you are:

  1. Fix the name of the variable as they cannot contain dashes:

    aws_sec_group_name:
      - "es-external"
      - "elasticsearch-production"
    
  2. Use the variable:

    group: "{{ aws_sec_group_name }}"
    

Upvotes: 2

Related Questions