-1

I have create ansible role to create multiple lambda function, where I am passing some parameters from variable file. My variable file looks like

Variable file

S3BucketName: "test_bucket"
S3Key1: "test.zip"
runtime: "python3.6"
handler1: "test.lambda_handler"
role1: "test_role_arn"
memory_size: "128"
timeout: "180"
s3_key2: "temp.zip" 
role2: "temp_role_Arn"
handler2: "temp.lambda_handler" 

In my playbook, I am using ansible loop to create multiple aws lambda functions at the same time. when I am using variable in with_items.

Playbook file

- hosts: localhost
  roles:
    - ansible-lambda
  vars_files:
    - "ansible-lambda/vars/cf_vars.yaml" 
    lambda:
        name: '{{ item.name }}'
        region: "{{ aws_region }}" 
        state: "{{state}}" 
        runtime: "{{ runtime }}" 
        timeout: "{{timeout}}"
        memory_size : "{{memory_size}}"
        s3_bucket: "{{ S3BucketName}}"
        s3_key: '{{ item.s3_key }}'
        role: '{{ item.role }}'
        handler: '{{ item.handler }}' 
    with_items:
         - name: test
           s3_key: "{{ S3Key1 }}" #refering to variable 1 
         - name: temp
           s3_key: "{{ S3Key2 }}" #refering to variable 2
    - debug:
        msg: "Lambda creation Complete!!"

Following is the error:

fatal: [localhost]: FAILED! => {"msg": "'S3Key1' is undefined"}

This playbook works, when I pass the absolute values instead of variables. I mean s3_key: test.zip

how to use variables in with item?

James Z
  • 12,209
  • 10
  • 24
  • 44
sonali shah
  • 39
  • 2
  • 6
  • 1
    Please bother to format your question properly. And read: https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest – Konstantin Suvorov Apr 12 '18 at 14:15

1 Answers1

1
-------------- var file ---------------
aws_region: austin
lambda_list:
  - name: lambda1
    state: "UR STATE HERE"
    S3BucketName: "test_bucket"
    S3Key: "test.zip"
    runtime: "python3.6"
    handler: "test.lambda_handler"
    role_desc: "test_role_arn"
    memory_size: "128"
    timeout: "180"

  - name: lambda2
    state: "UR STATE HERE"
    S3BucketName: "test_bucket"
    S3Key: "test2.zip"
    runtime: "python2.7"
    handler: "test.lambda_handler"
    role_desc: "test_role_ARN"
    memory_size: "256"
    timeout: "150"
---------------playbook------------------------ 
- hosts: localhost
  vars_files: "ansible-lambda/vars/cf_vars.yaml"
  tasks:
    lambda:
      name: '{{ item.name }}'
      region: "{{ aws_region }}"
      state: "{{ item.state }}"
      runtime: "{{ item.runtime }}"
      timeout: "{{ item.timeout }}"
      memory_size : "{{ item.memory_size }}"
      s3_bucket: "{{ item.S3BucketName }}"
      s3_key: "{{ item.s3_key }}"
      role: "{{ item.role_desc }}"
      handler: "{{ item.handler }}"
    with_items:
    - "{{ lambda_list }}" 

Here's a snippet of the correct way IMHO how to achieve what you're trying to do, sure there are few other ways but to be both efficient and easy config in example above you can see that there's a dict which holds every lambda's info as a key dict, when u use with_items it iterates each key to the task while using the item's data as {{ item.name }}.

You could even put a dict/list in a dict. for exmaple:

lambda_list:
  - name: lambda1 # <--- each dash('-') is a key with a value, that value is a dict
    S3: #
      S3BucketName: "test_bucket"
      S3Key: "test.zip"

  - name: lambda2
    S3: # <-- without the dash its indicated as a list inside the dict.
      S3BucketName: "test_bucket"
      S3Key: "test2.zip"

in this case to access your nested list you would use {{ item.S3.S3BucketName }} or {{ item['S3']['S3BucketName'] }}

if it was a dict in a dict you would get the key/value of each key without a proper way to access a specific key(with loops you can iterate the dict and use 'when' to get the desired key.)

Here's few references worth reading about loops, dicts and how to access them.

http://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_loops.html#nested-loops

levich
  • 628
  • 1
  • 9
  • 15