I am unable to figure out how to pass a dictionary/hash-map of key-value pairs as the value for an option in an ansible tasks.

Default Options:

# roles/ec2/defaults/main.yml
    key_name: "{{ key_name }}"
    instance_type: m3.medium
    image: ami-xxxx
    group_id: sg-xxxxx
    vpc_subnet_id: subnet-xxxxx
    region: us-east-1
    wait: yes
        Name: "{{ env_name }}-{{ group_name }}-{{ ansible_date_time.epoch }}"
        Type: "{{ env_name }}-{{ group_name }}-{{ ansible_date_time.epoch }}"

Attempt 1

# roles/ec2/tasks/main.yml
- name: launch single instance
  ec2: "{{ ec2 }}"
  register: instance


fatal: [localhost] => Traceback (most recent call last):
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/runner/__init__.py", line 590, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/runner/__init__.py", line 792, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/runner/__init__.py", line 994, in _executor_internal_inner
    num_args_post = self._count_module_args(module_args)
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/runner/__init__.py", line 434, in _count_module_args
    vargs = split_args(args)
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/module_utils/splitter.py", line 73, in split_args
    args = args.strip()
AttributeError: 'dict' object has no attribute 'strip'

Attempt 2

# roles/ec2/tasks/main.yml
- name: launch single instance
  ec2: "{{ ec2 | to_nice_yaml }}"
  register: instance


failed: [localhost] => {"failed": true}
msg: this module requires key=value arguments (['assign_public_ip:', 'true', 'group_id:', 'sg-xxxxx', 'image:', 'ami-xxxxx', 'instance_tags:', 'Environment:', 'stage', 'Name:', 'stage-analytics-stack', 'Type:', 'analytics-stack', 'instance_type:', 'm3.medium', 'key_name:', '2014-10-xxxxxx', 'region:', 'us-east-1', 'vpc_subnet_id:', 'subnet-xxxxx', 'wait:', 'true



I'm not certain what you are trying to accomplish, but I don't think it works that way. Maybe this is what you want?

- name: launch single instance
  ec2: key_name="{{ ec2.key_name }}" ...

The ec2 in your roles/ec2/defaults/main.yml is just a variable name (and I would name it differently in order to avoid confusion with the ec2 module). The to_nice_yaml filter isn't meant to do what you are trying.

  • That is currently what I have, but not what I want because then I need to duplicate all of my options. Attempt 1 would be the ideal way. – Mark Feltner Feb 23 '15 at 22:54
  • 1
    @MarkFeltner As far as I know this is not possible. – Antonis Christofides Feb 25 '15 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.