如何从include_tasks传回结果

时间:2018-04-02 10:34:59

标签: ansible

昨天开始学习ansible,所以我相信我可能会冒这个问题,但仍然......

主要的yml:

- hosts: localhost

  vars_files:
    [ "users.yml" ]
  tasks:
    - name: manage instances
      #include_tasks: create_instance.yml
      include_tasks: inhabit_instance.yml
      with_dict: "{{users}}"
      register: res
    - name: print
      debug: msg="{{res.results}}"

inhabit_instance.yml:

- name: Get instance info for {{ item.key }}
  ec2_instance_facts:
    profile: henryaws
    filters:
      "tag:name": "{{item.key}}"
      instance-state-name: running
  register: ec2
- name: print
  debug:
    msg: "IP: {{ec2.instances.0.public_ip_address}}"

这就是我想在顶层拥有的IP。没有立即找到包含块的返回值...

1 个答案:

答案 0 :(得分:0)

嗯,我找到了一些适合我的方式,也许它甚至是规范的?

main.yml:

- hosts: localhost

  vars_files:
    [ "users.yml" ]
  vars: 
    ec_results: {}
  tasks:
    - name: manage instances
      #include_tasks: create_instance.yml
      include_tasks: inhabit_instance.yml
      with_dict: "{{users}}"
      register: res

inhabit_instance.yml:

- name: Get instance info for {{ item.key }}
  ec2_instance_facts:
    profile: henryaws
    filters:
      "tag:name": "{{item.key}}"
      instance-state-name: running
  register: ec2
- name: update
  set_fact:
    ec_results: "{{ ec_results|combine({ item.key: ec2.instances.0.public_ip_address }) }}"