遍历json和concat字符串为value

时间:2018-12-26 10:25:15

标签: json amazon-web-services ansible

我正在尝试从从AWS获得的JSON输出中创建refind JSON 这是AWS的输出(我减少了行数):

{
    "Regions": [
        {
            "Endpoint": "ec2.ap-south-1.amazonaws.com",
            "RegionName": "ap-south-1"
        },
        {
            "Endpoint": "ec2.eu-west-3.amazonaws.com",
            "RegionName": "eu-west-3"
        }
    ]
}

这是我的烦人的剧本:

- hosts: localhost
  gather_facts: false
  tasks:
  - name: JSON Me
    shell: 'aws --profile="{{ AWS_PROFILE }}" --region="{{ AWS_REGION }}" ec2 describe-regions'
    register: regions_out

  - set_fact:
      regions: "{{ regions_out.stdout | from_json }}"

  - name: print
    shell: echo '"{{ item }}" = abc'
    loop: "{{ regions | json_query('Regions[*].RegionName') }}"

我想要实现的是打印出RegionName和concat文本(= abc

但是,myoutput看起来像这样:

changed: [localhost] => (item=ap-south-1)
changed: [localhost] => (item=eu-west-3)

尝试使用| join(" = abc")进行过滤时出现错误,因为输出变为一个长字符串而不是一个列表。

非常感谢您的建议, 谢谢

1 个答案:

答案 0 :(得分:0)

- hosts: localhost
  gather_facts: false
  tasks:
  - name: JSON Me
    shell: 'aws --profile="{{ AWS_PROFILE }}" --region="{{ AWS_REGION }}" ec2 describe-regions'
    register: regions_out

  - set_fact:
      regions: "{{ regions_out.stdout | from_json }}"

  - name: print
    debug:
      msg: "{{ item }} = \"ami-...\""
    loop: "{{ regions | json_query('Regions[*].RegionName') }}"