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

Ansible从ec2实例获取标记信息和事实(Linux),ansible,Ansible,对于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) $I

对于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个标签

  • 名称空间:

  • 应用程序:

  • 环境:

  • 组成部分:

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

  • 实例区

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

我找到了一个办法

    - 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
如何使用“应用程序”键并找到“值”。因此,我不需要索引

您想要的东西后面是有用的返回

任务:
-ec2_元数据_事实:
-ec2_实例_事实:
实例id:“{ansible\u ec2\u实例id}”
区域:“{ansible_ec2_placement_region}”
register:my_实例
-调试:
msg:my tags{{my_instance.tags}

以防对任何人都有帮助。我正在下面粘贴我的代码

我是通过@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 }}"

真的很有用@mdanielt这不起作用,你需要在msg中有双引号。
- 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 }}"