如何在Ansible

时间:2018-04-09 09:23:02

标签: amazon-web-services ansible

我的var文件中有以下变量:

aws:
  profiles:
    - name: default
      properties:
        aws_access_key_id: changeme
        aws_secret_access_key: changeme
        region: eu-west-1
        format: json
    - name: playground
      properties:
        aws_access_key_id: changeme
        aws_secret_access_key: changeme
        region: eu-west-1
        format: json

我想用aws.profiles.name字段配置每个aws配置文件(properties属性),其中properties是一个字典,这意味着我想要为其设置密钥和值每个配置文件。

我尝试了多种方法,包括with_itemswith_dictwith_nested,但没有任何帮助。最合乎逻辑的案例如下:

- name: Configure accounts.
  shell: aws configure set {{ item.properties.key }} {{ item.properties.value }} --profile {{ item.name }}
  with_dict: "{{ aws.profiles }}"

但它引发了一个错误:

  

“该任务包含一个带有未定义变量的选项。错误   是:'dict对象'没有属性'属性'

另一种方法不起作用,但这次至少properties被识别出来了:

- name: Configure accounts.
  shell: aws configure set {{ item.properties.key }} {{ item.properties.value }} --profile {{ item.name }}
  with_items: "{{ aws.profiles }}"

这次的错误是:

  

该任务包含一个带有未定义变量的选项。错误是:   'dict object'没有属性'key'

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

好的,我尝试按照 @Konstantin Suvorov 建议的方式行事。

现在,重构变量如下所示:

aws:
  profiles:
    - name: default
      properties:
        - key: aws_access_key_id
          value: changeme
        - key: aws_secret_access_key
          value: changeme
        - key: region
          value: eu-west-1
        - key: format
          value: json
    - name: playground2
      properties:
        - key: aws_access_key_id
          value: changeme
        - key: aws_secret_access_key
          value: changeme
        - key: region
          value: eu-west-1
        - key: format
          value: json

任务本身如下:

- name: Configure accounts.
  shell: "aws configure set {{ item.1.key }} {{ item.1.value }} --profile {{ item.0.name }}"
  with_subelements:
    - "{{ aws.profiles }}"
    - properties
相关问题