jsjw
jsjw

Reputation: 317

Use a variable name inside an Ansible lookup

I'm using the AWS SSM lookup plugin to grab a value from SSM — but wanted to dynamically set the path to the SSM variable depending on the environment.

Hardcoding the environment works, so, I could write a task and use a when: condition for each possible environment, which feels a bit clunky.

- name: Get CloudWatch Log Group Name from SSM for use in CloudWatch role
  set_fact:
    log_group_name: "{{ lookup('aws_ssm', '/production/cloudwatch/log_group_name') }}"

I'm trying to do something like the below:

- name: Get CloudWatch Log Group Name from SSM for use in CloudWatch role
  set_fact:
    log_group_name: "{{ lookup('aws_ssm', environment + '/cloudwatch/log_group_name') }}"

But I get the following

FAILED! => {"msg": "Unexpected templating type error occurred on ({{ lookup('aws_ssm', environment + '/cloudwatch/log_group_name') }}): can only concatenate list (not "str") to list"}

Are there other ways to use string formatting, such as:

- name: Get CloudWatch Log Group Name from SSM for use in CloudWatch role
  set_fact:
    log_group_name: "{{ lookup('aws_ssm', '{}/cloudwatch/log_group_name'.format(Environment) }}"

Upvotes: 0

Views: 1312

Answers (1)

jsjw
jsjw

Reputation: 317

Turns out it was due to the usage of the environment variable - changed this to capital 'E' and everything works as expected:

    - name: Get CloudWatch Log Group Name from SSM for use in CloudWatch role
      set_fact:
        log_group_name: "{{ lookup('aws_ssm', '/' + Environment + '/cloudwatch/log_group_name') }}"

Upvotes: 0

Related Questions