简体   繁体   中英

How to display AWS autoscaling group name using ansible?

I was writing Ansible playbook to display the auto scaling group of AWS based on tags and below is my playbook.

- 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}}"

I was running it on Jenkins and I was getting error

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"}

I thought the result doesn't have any asg name so i updated the above code as

- 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}}"

It didn't give me any error, but the result was empty.

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

I do have tags added in my ASG as you can see below. And there is no other asg with same tags Tags image

The same playbook works like a charm for my other applications

  • name: Get information about Auto Scaling group ec2_asg_facts: region: "{{ region }}" register: asg_facts

  • name: Display Auto Scaling group name debug: msg: "The Auto Scaling group name is {{ asg_facts.autoscaling_groups[0].name }}"

this works for me

- 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 }}"

EDIT:

here is a template of playbook

---
- 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 }}"

the ec2_instance_info module is used to retrieve information about the instance with the tag Name set to "my-instance" in the us-east-1 region. The ec2_asg_info module is then used to retrieve information about the Auto Scaling group that the instance belongs to, using the asg_name attribute of the instance. Finally, the debug module is used to display the name of the Auto Scaling group.

You can modify this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM