Ansible,ec2_remote_facts。在角色中使用vars

时间:2017-03-13 12:45:35

标签: amazon-web-services amazon-ec2 ansible

请提供建议。如何在角色中使用来自ec2_remote_facts的变量?

---

- name: Sandbox
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
   - name: Get facts by filter
     ec2_remote_facts:
       region: "{{ aws_default_region }}"
       filters:
         instance-state-name: running
         "tag:Group": "{{ aws_default_instance_tag_group }}"
     register: ec2_remote_facts

   - name: Add running sandbox instances to in-memory inventory host group
     add_host:
       hostname: "{{ item.public_ip_address }}"
       groups: running
     with_items: "{{ ec2_remote_facts.instances }}"

- name: prov
  hosts: running
  gather_facts: true
  user: ec2-user
  become: true
  roles:
    - httpd

在这种情况下,我想使用运行实例的一些vars作为httpd角色。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我至少看到3种方式:

add_host任务中定义变量:

- name: Add running sandbox instances to in-memory inventory host group
  add_host:
    hostname: "{{ item.public_ip_address }}"
    groups: running
    ec2_interfaces: "{{ item.interfaces }}"
  with_items: "{{ ec2_remote_facts.instances }}"

ec2_facts称为前任务:

- name: prov
  hosts: running
  gather_facts: true
  user: ec2-user
  become: true
  pre_tasks:
    - ec2_facts:
  roles:
    - httpd

使用hostvars

- name: prov
  hosts: running
  gather_facts: true
  user: ec2-user
  become: true
  roles:
    - role: httpd
      first_instance_interfaces: "{{ hostvars['localhost'].ec2_remote_facts.instances[0].interfaces }}"