I am trying to update my ec2 instance tags with OS details using Ansible.
I am able to achieve it by using the below code. Is there a better/efficient way to do this.
Expecting tag format ex:
platform = "Linux-Centos-7"
Inventory:-
---
[dev]
127.0.0.1
127.0.0.2
playbook:
- hosts: dev
tasks:
- name: Update OS tags for EC2 Instances
block:
- name: Fetch instance_id form ipaddress
ec2_instance_info:
filters:
network-interface.addresses.private-ip-address: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"
register: instance_ids
delegate_to: localhost
- name: Create OS tags on a EC2 for every instance_id
ec2_tag:
resource: "{{instance_ids.instances[0].instance_id}}"
state: present
tags:
platform: "{{ hostvars[inventory_hostname].ansible_system}}-{{ hostvars[inventory_hostname].ansible_distribution }}-{{ hostvars[inventory_hostname].ansible_distribution_major_version }}"
delegate_to: localhost
question from:
https://stackoverflow.com/questions/65830548/ansible-how-to-fetch-os-details-and-use-them-as-tags 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…