簡體   English   中英

Ansible,ec2_remote_facts。 在角色中使用var

[英]Ansible, ec2_remote_facts. Using vars in the roles

請給個建議。 如何在角色中使用來自ec2_remote_facts的var?

---

- 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

在這種情況下,我想將運行實例的一些變量用於httpd角色。

提前致謝!

我至少看到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 }}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM