字典作为 Ansible 任务中 YAML 键的值

字典作为 Ansible 任务中 YAML 键的值

我无法弄清楚如何将键值对的字典/哈希图作为 ansible 任务中选项的值传递。

默认选项:

# roles/ec2/defaults/main.yml
ec2:
    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
    instance_tags:
        Name: "{{ env_name }}-{{ group_name }}-{{ ansible_date_time.epoch }}"
        Type: "{{ env_name }}-{{ group_name }}-{{ ansible_date_time.epoch }}"

尝试 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'

第二次尝试

# 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

谢谢!

答案1

我不确定你想实现什么,但我认为它不是那样的。也许这就是你想要的?

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

ec2roles/ec2/defaults/main.yml只是一个变量名(我会用不同的名字来命名它,以避免与模块混淆ec2)。to_nice_yaml过滤器并不是为了做你想做的事情。

相关内容