如何在Ansible中注册一个从JSON输出提取的变量?

Ben*_*Ben 3 json amazon-web-services ansible ansible-2.x

我正在尝试使用Ansible自动化Amazon Cloudfront发行版的配置。当前,我需要在Amazon Certificate Manager中查找证书的ARN(Amazon资源名称),并将ARN存储为变量,以便以后在Cloudfront发行配置中使用。

我对此的查询如下:

- name: Check for existence of a certificate for this project in Amazon Certificate Manager
  command: >
    aws acm list-certificates 
      --profile "{{ project_name }}"-deploy
      --region us-east-1 
  register: cert_list
  ignore_errors: True

- name: Record list-certificates output to Json  
  set_fact: 
    this_project_arn: # I want to set this from the output of list-certficates

- debug: msg="{{ cert_list.stdout | from_json }}"
Run Code Online (Sandbox Code Playgroud)

该调试的输出当前如下:

TASK [configure-cloudfront : debug] ********************************************
ok: [localhost] => {
    "msg": {
        "CertificateSummaryList": [
            {
                "CertificateArn": "arn:aws:acm:us-east-1:123456789101:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", 
                "DomainName": "*.foo.com"
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我有两个目标:

  1. CertificateArn从那里返回的JSON 中提取出的值;
  2. 通过在结果中专门查找“ foo.com”(我将域作为可比较的Ansible var)并在该域的证书仅存储ARN的情况下使其具有弹性,以防万一返回了多个证书list-certificates

有什么方法可以记录set_fact遍历JSON输出的my ,cert_list.stdout并且仅返回DomainName包含foo.com 的ARN值?

谢谢!

gui*_*ido 5

您需要with_items对解析的数据进行循环,以对证书摘要进行循环,然后可以使用when进行筛选,以便有选择地set_fact

- name: Set ARN for passed in domain
  set_fact:
    project_arn: "{{ item.CertificateArn }}"
  when: item.DomainName == "*.foo.com"
  with_items: "{{ (cert_list.stdout|from_json).CertificateSummaryList }}"
Run Code Online (Sandbox Code Playgroud)