Ansible from_json过滤不返回输出

时间:2018-10-05 16:09:28

标签: json amazon-web-services ansible ansible-2.x

我正在使用AWS CLI在剧本中创建动态EC2库存清单。我正在使用from_json jinja2过滤器来格式化标准输出。然后,我需要获取所有私有IP地址以创建主机列表。但是,我注意到尝试创建主机组时代码没有返回任何数据,因此出现以下错误:

{ "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'PrivateIpAddress'\n\nThe error appears to have been in '/var/lib/awx/projects/_668__symantec_cloud_workload_protection/main.yml': line 65, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Add instances to ansible group\n ^ here\n", "failed": true }

如何简单地提取PrivateIpAddress值?

Code:

---

- hosts: localhost
  gather_facts: False
  tasks:
    - name: assume role
      sts_assume_role:
        role_arn: "arn:aws:iam::{{ account_num }}:role/my_role"
        role_session_name: "Session"
        region: "{{ region }}"
      register: assumed_role

- hosts: localhost
  tasks:
    - name: Get instance details for environment
      command: aws ec2 describe-instances --region "{{ region }}" --filters Name=vpc-id,Values="{{ vpc_id }}" Name=tag:environment,Values="{{ env }}"
      register: instances_result
      changed_when: false

    - name: Set instances fact
      set_fact:
        instances_fact: "{{ instances_result.stdout|from_json }}"

    - debug: var=instances_fact verbosity=2

    - name: Add instances to ansible group
      add_host: name={{ item.PrivateIpAddress }} groups=vpc_instances
      with_items: "{{ instances_fact }}"

    - name: Print ansible host groups
      debug: var=groups verbosity=2

  environment:
    AWS_ACCESS_KEY_ID: "{{ assumed_role.sts_creds.access_key }}"
    AWS_SECRET_ACCESS_KEY: "{{ assumed_role.sts_creds.secret_key }}"
    AWS_SESSION_TOKEN: "{{ assumed_role.sts_creds.session_token }}"

- hosts: vpc_instances
  become: yes
  roles:
    - { role: foo_bar, yum_update: False }

我注意到debug任务被跳过,因为没有数据被返回:

TASK [debug] *******************************************************************11:30:33 23 skipping: [localhost] NO JSON data returned by the module

这是其上方的json任务的set_fact输出:

{
    "invocation": {
        "module_name": "set_fact",
        "module_args": ""
    },
    "changed": false,
    "ansible_facts": {
        "instances_fact": {
            "Reservations": [{
                    "Instances": [{
                        "Monitoring": {
                            "State": "disabled"
                        },
                        "PublicDnsName": "ec2-xxxxxx",
                        "State": {
                            "Code": 16,
                            "Name": "running"
                        },
                        "EbsOptimized": false,
                        "LaunchTime": "xxxxxxx",
                        "PublicIpAddress": "x.x.x.x",
                        "PrivateIpAddress": "x.x.x.x",
                        "ProductCodes": [{
                            "ProductCodeId": "axxxxxxxxxxxxxx",
                            "ProductCodeType": "marketplace"
                        }],
                        "VpcId": "vpc-xxxxxx",
                        "StateTransitionReason": "",
                        "SecurityGroups": [{
                             "GroupName": "va-xxxxxx",
                            "GroupId": "sg-xxxxxx"
                        }],
                        "ClientToken": "",
                        "SubnetId": "subnet-xxxxxxxx",
                        "InstanceType": "t2.micro",
                        "NetworkInterfaces": [{
                            "Status": "in-use",
                            "PrivateIpAddresses": [{
                                "PrivateDnsName": "ip-xxxxxxxxxxxx",
                                "PrivateIpAddress": "x.x.x.x",
                                "Primary": true,
                                "Association": {
                                    "PublicIp": "x.x.x.x",
                                    "PublicDnsName": "ec2-xxxxxxx",
                                    "IpOwnerId": "amazon"
                                }
                            }],
                             "Attachment": {
                                "Status": "attached"
                            },
                            "Groups": [{
                                "GroupName": "va-xxxxxxxx",
                                "GroupId": "sg-xxxxxxx"
                            }],
                            "Ipv6Addresses": [],
                            "OwnerId": "xxxxxxxx",
                            "PrivateIpAddress": "x.x.x.x",
                            "SubnetId": "subnet-xxxxxxxxx",
                            "Association": {
                                "PublicIp": "x.x.x.x",
                                "PublicDnsName": "exxxxxxxxxx",
                                "IpOwnerId": "amazon"
                            }
                        }],
                        "SourceDestCheck": true,
                        "Placement": {
                            "GroupName": "",
                            "Tenancy": "default",
                            "AvailabilityZone": "us-east-1a"
                        },
                        "Hypervisor": "xen",
                        "BlockDeviceMappings": [{
                            "DeviceName": "/dev/sda1",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": false,
                                "VolumeId": "vol-xxxxxxxxx",
                                "AttachTime": "2xxxxxxxxxxxx"
                            }
                        }],
                        "Architecture": "x86_64",
                        "RootDeviceType": "ebs",
                        "RootDeviceName": "/dev/sda1",
                        "VirtualizationType": "hvm",
                        "Tags": [{
                                "Value": "cloud_platform",
                                "Key": "application"
                            }

                        ],
                        "AmiLaunchIndex": 0
                    }],
                    "ReservationId": "r-xxxxxxxxxx",
                    "Groups": [],
                    "OwnerId": "xxxxx"
                }

            ]
        }
    },
    "_ansible_no_log": false
}

1 个答案:

答案 0 :(得分:0)

您将变量instances_fact注册为json字符串,我相信如果您打算对其进行迭代以添加主机,则只想注册实例列表即可。我也将使用模块ec2_instance_facts来收集您的详细信息,因此您不必担心解析json等问题。

尝试像这样注册.instances:

- hosts: localhost
  tasks:
    - name: Get instance details for environment
      ec2_instance_facts:
        filters:
          "tag:environment": "{{ env }}"
        region: "{{ region }}"
      register: instance_result

    - name: Set instances fact
      set_fact:
        instances_fact: "{{ instances_result.instances }}"
                              /\              /\    
    - debug: var=instances_fact verbosity=2

    - name: Add instances to ansible group
      add_host: name={{ item.PrivateIpAddress }} groups=vpc_instances
      with_items: "{{ instances_fact }}"