Ansible从ec2实例(Linux)获取标签信息和事实



对于windows,我们使用powershell脚本来获取标记和事实,并将其存储在变量中。

---
- name: "Read Tags from the current Instance"
win_shell: |
$InstanceId = (Invoke-RestMethod -Uri "http://169.254.169.254/latest/meta-data/instance-id" -UseBasicParsing)
$Instance = ((Get-EC2Instance -Instance $InstanceId).RunningInstance)
$Instance = $Instance | Where-Object { $_.InstanceId -eq $InstanceId }
($Instance.Tags | Where-Object { $_.Key -eq "Namespace"}).Value
($Instance.Tags | Where-Object { $_.Key -eq "Application"}).Value
($Instance.Tags | Where-Object { $_.Key -eq "Environment"}).Value
($Instance.Tags | Where-Object { $_.Key -eq "Component"}).Value
(Invoke-RestMethod -UseBasicParsing -TimeoutSec 5 -Method Get -Uri "http://169.254.169.254/latest/dynamic/instance-identity/document").Region
register: instance_output
retries: 5
delay: 2
until: instance_output.changed

对于Linux,我尝试使用下面的战术手册

- name: check if we can get metadata
uri:
url: http://169.254.169.254/latest/meta-data
timeout: 3
register: meta_check
failed_when: False
- name: store result
set_fact:
inside_aws: "{{ meta_check.status == 200 }}"
- name: install aws cli
become: true
command: yum install -y awscli
when: inside_aws
- name: get the list of tags
shell: REGION=$(curl -q http://169.254.169.254/latest/meta-data/placement/availability-zone) INSTANCE=$(curl -q http://169.254.169.254/latest/meta-data/instance-id); aws ec2 describe-tags --region ${REGION%?} --filters "Name=resource-id,Values=$INSTANCE"
register: tag_list
when: inside_aws
- name: create facts out of tags
ignore_errors: true
set_fact:
"{{'ec2_tag_' + tag.Key.replace(':','_').replace('-','_') }}": "{{ tag.Value }}"
with_items: "{{ (tag_list.stdout | from_json)['Tags'] }}"
when: inside_aws
loop_control:
loop_var: tag
label: "{{ tag.Key }} {{ tag.Value }}"

基本上我应该存储以下5个标签

  • 命名空间:

  • 应用程序:

  • 环境:

  • 组件:

  • 对于所有实例,我们都有上述标签

  • 实例区域

上述剧本的输出(部分(:

TASK [debug] **************************************************************************************************************************************************************
ok: [localhost] => {
"tag_list.stdout": {
"Tags": [
{
"Key": "Application",
"ResourceId": "i-0cf3a8942e7b77ec",
"ResourceType": "instance",
"Value": "mac"
},
{
"Key": "Business Unit",
"ResourceId": "i-0cf3a894e7b77ec",
"ResourceType": "instance",
"Value": "trc"
},
{
"Key": "Component",
"ResourceId": "i-0cf3a894e7b77ec",
"ResourceType": "instance",
"Value": "tower"
},
{
"Key": "Domain",
"ResourceId": "i-0cf3a894e7b77ec",
"ResourceType": "instance",
"Value": "user.compio.io"
},
{
"Key": "Environment",
"ResourceId": "i-0cf3a894e7b77ec",
"ResourceType": "instance",
"Value": "ops1"
},

我找到了一种方法。

- set_fact: Application="{{ tag_list.stdout | from_json | json_query('Tags[0].Value') }}"
- set_fact: Component="{{ tag_list.stdout | from_json | json_query('Tags[2].Value') }}"
- set_fact: Environment="{{ tag_list.stdout | from_json | json_query('Tags[4].Value') }}"
- set_fact: Namespace="{{ tag_list.stdout | from_json | json_query('Tags[8].Value') }}"
- debug: var=Application
- debug: var=Component
- debug: var=Namespace
- debug: var=Environment

我怎样才能使用";应用程序";并找到";值";。这样我就不需要索引了

万一它对任何人都有帮助。我正在下面粘贴我的代码。

我根据@mdaniel的指示完成了任务。非常感谢。

- ec2_metadata_facts:
register: instance_meta
- ec2_instance_info:
instance_ids:
- "{{ instance_meta.ansible_facts.ansible_ec2_instance_id }}"
region: "{{ instance_meta.ansible_facts.ansible_ec2_placement_region }}"
register: instance_info
- name: Gather and Save Instance Info
set_fact:
Application: "{{ instance_info.instances | map(attribute='tags.Application') | list | join('n') }}"
Component: "{{ instance_info.instances | map(attribute='tags.Component') | list | join('n') }}"
Namespace: "{{ instance_info.instances | map(attribute='tags.Namespace') | list | join('n') }}"
Environment: "{{ instance_info.instances | map(attribute='tags.Environment') | list | join('n') }}"
Region: "{{ instance_meta.ansible_facts.ansible_ec2_placement_region }}"

使用ec2_metadata_facts和ec2_instance_facts

- name : Playbook to Check Tags
hosts: "{{host}}"
gather_facts: True
tasks:
- ec2_metadata_facts:
- ec2_instance_facts:
instance_ids: '{{ ansible_ec2_instance_id }}'
region: '{{ ansible_ec2_placement_region }}'
register: my_instance
delegate_to: localhost
- debug:
msg: my tags "{{ my_instance.instances | map(attribute='tags.YOUR_TAG_NAME') | list | join('n') }}"

您想要的是ec2_metadata_facts:,后面跟着ec2_instance_facts:,这有助于将标记作为dict返回

tasks:
- ec2_metadata_facts:
- ec2_instance_facts:
instance_id: '{{ ansible_ec2_instance_id }}'
region:  '{{ ansible_ec2_placement_region }}'
register: my_instance
- debug:
msg: my tags {{ my_instance.tags }}

最新更新