oddity with command arguments

I am using the command module to run an AWS CLI command. The command has an argument in it that takes a space-delimited set of parameters, like this:

–subnets-mappings this that theother

When I issue the command on the command line, all the items in the lists are used. When I run the command via Ansible, only the last one in the list is used.

This is how I create the list of subnet mappings:

  • set_fact:
    snm: “{{ snm |default(‘’) ~ ‘SubnetId=’ ~ item.SubnetId ~ ‘,AllocationId=’ ~ item.AllocationId ~ ’ ’ }}”
    with_items: “{{ nlb_subnet_mappings }}”

This is the command I am running:

  • command: aws elbv2 create-load-balancer --name “{{ nlb_name }}” --type network --subnet-mappings “{{ snm }}” --region “{{ nlb_region }}”
    register: nlb

Here is the “cmd” part returned by the command:

“cmd”: [“aws”, “elbv2”, “create-load-balancer”, “–name”, “ADFS-ext-nlb”, “–type”, “network”, “–subnet-mappings”, "SubnetId=subnet-c1580ba5,AllocationId=eipalloc-09644c10a5aac6a39 SubnetId=subnet-3cd85265,AllocationId=eipalloc-09c95f8e4f8e111b2 ", “–region”, “ap-southeast-2”]

Because of the way I build the command, there is a space at the end of the list, that is the only difference I can see. And I do build the lists as strings which are then passed as arguments - all the tags are one argument, all the subnets are one argument.

The Ansible command does work and issues no errors, but it only uses the last in the list of subnet mappings. If I do this on the command line, it DOES use both subnet mappings:

aws elbv2 create-load-balancer --name ADFS-ext-nlb --type network --subnet-mappings SubnetId=subnet-c1580ba5,AllocationId=eipalloc-09644c10a5aac6a39 SubnetId=subnet-3cd85265,AllocationId=eipalloc-09c95f8e4f8e111b2 --region ap-southeast-2

Any clues?

Regards, K.

It’s even odder than I thought.

The first SubnetID is being paired with the last AllocationID.

These parameters:

"SubnetId=subnet-c1580ba5,AllocationId=eipalloc-09644c10a5aac6a39 SubnetId=subnet-3cd85265,AllocationId=eipalloc-09c95f8e4f8e111b2 "

…are somehow ending up interpreted as

"SubnetId=subnet-c1580ba5,AllocationId=eipalloc-09c95f8e4f8e111b2 "

OK - that was it. Passing all the elements in one string doesn’t work.

So now the big question - is it possible to pass a list of arguments to command and if so how?

Or a more general question, can I turn a list of strings (or output a list of strings) into a list that the command module can use?

Regards, K.

To make it work you need to remove the double quotes in the command module.

Oh…

After all the thousands of times Ansible has told me I need to wrap Jinja up in quotes, here’s a time I don’t.

Thanks, that simple change solved the issue completely. I did learn a LOT about all sorts of other stuff though, in the hours I have spent trying to work this out :slight_smile:

Regards, K.

Oh....

After all the thousands of times Ansible has told me I need to wrap Jinja
up in quotes, here's a time I don't.

It is only in one occasion Ansible or more specifically YAML need that.
And that's when a { comes after a colon, or else YAML will take it as a dictionary.

It has been documented for a long time a go here BTW :slight_smile:
https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#gotchas

Thanks, that simple change solved the issue completely. I did learn a LOT
about all sorts of other stuff though, in the hours I have spent trying to
work this out :slight_smile:

Problems tends to do that :slight_smile: