如何使用ansible显示AWS自动缩放组名称?

问题描述 投票:0回答:3

我正在编写 Ansible 剧本来根据标签显示 AWS 的自动缩放组,下面是我的剧本。

- name: Find the green asg with matching tags
  ec2_asg_info:
    tags:
      service_name: cps_wallet
      Environment: "{{ name_env }}"
      service_state: green
  register: asgs_payment

- name: Show the ASG Payment name
  debug: 
    msg: "{{ asgs_payment.results[0].auto_scaling_group_name}}"

- set_fact:
    asg_payment_name: "{{ asgs_payment.results[0].auto_scaling_group_name}}"

我在 Jenkins 上运行它,但出现错误

fatal: [127.0.18.34]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 0\n\nThe error appears to be in '/opt/software/jenkins/workspace/payment-react-ui/srv/payment-ui-110/payment-ui/roles/inspect/tasks/cps_green_deploy.yml': line 9, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Show the ASG Payment name\n ^ here\n"}

我认为结果没有任何 asg 名称,所以我将上面的代码更新为

- name: Find the green asg with matching tags
  ec2_asg_info:
    tags:
      service_name: cps_wallet
      Environment: "{{ name_env }}"
      service_state: green
  register: asgs_payment

- name: Show the ASG Payment name
  debug:
    msg: "{{ asgs_payment}}"

- set_fact:
    asg_payment_name: "{{ asgs_payment}}"

它没有给我任何错误,但结果是空的。

"msg": { "changed": false,  "failed": false,  "results": [] }

我确实在我的 ASG 中添加了标签,如下所示。并且没有其他asg具有相同的标签 标签图片

同样的剧本对我的其他应用程序来说就像一个魅力

amazon-web-services ansible ansible-2.x aws-auto-scaling
3个回答
1
投票

这对我有用

- name: Display Auto Scaling group name
  ec2_asg_info:
    region: us-east-1
    name: my-asg
  register: asg_info

- debug:
    msg: "Auto Scaling group name: {{ asg_info.auto_scaling_groups[0].name }}"

编辑:

这是剧本的模板

---
- name: Display Auto Scaling group of AWS instance
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Get instance information
      ec2_instance_info:
        region: us-east-1
        filters:
          "tag:Name": "my-instance"
      register: instance_info

    - name: Get Auto Scaling group information
      ec2_asg_info:
        region: us-east-1
        name: "{{ instance_info.instances[0].asg_name }}"
      register: asg_info

    - name: Display Auto Scaling group name
      debug:
        msg: "Auto Scaling group name: {{ asg_info.auto_scaling_groups[0].name }}"

ec2_instance_info 模块用于检索 us-east-1 区域中标签名称设置为 “my-instance” 的实例的信息。然后,ec2_asg_info 模块用于使用实例的 asg_name 属性检索有关实例所属 Auto Scaling 组的信息。最后通过debug模块显示Auto Scaling组的名称。

您可以修改此


1
投票
  • name:获取Auto Scaling组信息 ec2_asg_facts: 地区:“{{地区}}” 注册:asg_facts

  • name:显示 Auto Scaling 组名称 调试: msg:“Auto Scaling 组名称为 {{ asg_facts.autoscaling_groups[0].name }}”


0
投票

这是另一种方法:

---
- name: Display Auto Scaling group of AWS instance
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Get AWS facts
      amazon.aws.ec2_metadata_facts:

    - name: Get instance information
      ec2_tag_info:
        resource: "{{ ansible_ec2_instance_id }}"
        region: "{{ ansible_ec2_placement_region }}"
      register: instance_tags

    - name: Display Auto Scaling group name
      debug:
        msg: "{{ instance_tags.tags['aws:autoscaling:groupName'] }}"
© www.soinside.com 2019 - 2024. All rights reserved.