Ansible 2.x:ec2_remote_facts

问题描述 投票:2回答:6

我正在尝试解决如何列出并使用EC2停止的实例执行某些操作。 ec2_remote_facts是我的朋友,我可以从中获得结果。但作为一个安心的菜鸟我不能为我的生活找到如何获得返回对象的'id'组成部分。该文档是粗略的,不包括返回什么类型的东西以及如何使用它。

这是我正在尝试的非常基本的剧本:

---
- name: remote facts
  hosts: localhost

  tasks:

   - ec2_remote_facts:
      region: ap-southeast-2
     register: info

   - name: print
     debug: var=info

这很好用。

这也有效:

---
- name: remote facts
  hosts: localhost

  tasks:

   - ec2_remote_facts:
      region: ap-southeast-2
     register: info

   - name: print
     debug: var=info.instances[0].id

我无法理解的是咒语给了我所有当前实例的'id'元素(3)所以我可以对所有这些实现其他的东西 - 删除,配置,启动等等。我已经看过所有循环结构和with_ *选项,我不能让它们中的任何一个做我想要的。

毫无疑问,这将是'd'哦!任何知道答案的人都会遇到问题,但我现在担心的是我不知道答案。返回的对象如下所示:

ok: [localhost] => {
    "info": {
        "changed": false, 
        "instances": [
            {
                "ami_launch_index": "0", 
                "architecture": "x86_64", 
            "client_token": "vpOGxxxxxxxxxxxxxx", 
            "ebs_optimized": false, 
            "groups": [
                {
                    "id": "sg-xxxxxxxx", 
                    "name": "launch-wizard-3"
                }
            ], 
            "hypervisor": "xen", 
            "id": "i-01xxxxxxxxxxxxxxxx", 

[...]

        "tags": {}, 
        "virtualization_type": "hvm", 
        "vpc_id": "vpc-xxxxxxxx"
    }, 
        {
            "ami_launch_index": "1", 
            "architecture": "x86_64", 

[snip,另外2个实例]

人们有什么想法?

loops amazon-ec2 ansible
6个回答
1
投票

我怀疑它有点晚了,但我认为你只是忘了在你的with_items变量周围添加“{{}}”。如果我这样做,对我有用,所以:

ec2_remote_facts:
        region: "{{ vpc_region }}"
        filters:
        instance-state-name: running
        vpc-id: "{{ vpc_id }}"         
  register: ec2_info

然后:

set_fact:
  jenkins_master_instances: "{{ jenkins_master_instances|default([]) + [ item.id ]}}"
with_items: "{{ ec2_info.instances }}"

似乎对我有用。


1
投票

这应该是有效的:

  tasks:

    - ec2_remote_facts:
        region: ap-southeast-2
      register: thefacts

    - debug: var=item.id
      with_items: "{{thefacts.instances}}"

但是产生了很多不必要的调试输出,因为没有理由。所以让我们过滤掉它:

  tasks:

    - ec2_remote_facts:
        region: ap-southeast-2
      register: thefacts

    - debug: var=item.0
      with_together:
        - "{{ thefacts.instances|map(attribute='id')|list }}"

0
投票

试试这个它应该工作:

   - ec2_remote_facts:
      region: ap-southeast-2
     register: info

   - name: print
     debug: var=item.id 
     with_items: info_instances

如果不共享debug: var=info的完整输出。所以我可以告诉你确切的代码。

此外,如果你想使用任何任务,如ec2停止实例使用它作为变量{{item.instances.id}}与相同的循环with_items: info_instances


0
投票

你有第二次尝试,有趣的是我有完全相同的问题,但是我的JSON经验让我看到它。

你的第二个代码片段:

---
- name: remote facts
  hosts: localhost

  tasks:

   - ec2_remote_facts:
      region: ap-southeast-2
     register: info

   - name: print
     debug: var=info.instances[0].id

我的补充:

---
- name: remote facts
  hosts: localhost

  tasks:

   - ec2_remote_facts:
      region: ap-southeast-2
     register: info

   - name: print
     debug: var=info.instances[0].id

   - name: Create AMI from Instance ID
     ec2_ami:
       aws_access_key: "{{ aws_access_key }}"
       aws_secret_key: "{{ aws_secret_key }}"
       region: "{{ region }}"
       instance_id: "{{ ec2_remote_facts.instances[0].id }}"        

欢呼帮助我解决这个问题哈哈。

Ansible的输出:

 (ansible27) user@user:/etc/ansible/playbooks$ sudo ansible-playbook ec2_facts.yml
 [WARNING]: Found both group and host with same name: localhost


PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [EC2 Remote Facts.] *******************************************************
ok: [localhost]

TASK [Display results in JSON format.] *****************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "Most upto date instance is: i-1234567abcdefg "
}

TASK [Create AMI from instance ID.] ********************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0   

对于循环问题,我假设它只是传递[1]和[2]元素说明符的情况。


0
投票

花了很多时间研究这个问题,使用Ansible Jinja2过滤器实际上非常简单。

这应该工作:

- name: DEBUG
  debug:
    var: info |json_query('instances[*].id')

这里描述了一些非常有用的过滤:qazxsw poi


0
投票

谢谢@ hit3k

我已经更改了一些代码,使其在Ansible 2.7.10下运行

http://docs.ansible.com/ansible/latest/playbooks_filters.html#other-useful-filters

结果:

- name: print the variable name
  debug: msg= "{{ item.0 }}"
  with_items: "{{ ec2_metadata.instances|map(attribute='instance_id')|list }}"
© www.soinside.com 2019 - 2024. All rights reserved.