How can Ansible “register” in a variable the resul

2019-02-16 11:06发布

问题:

How can an Ansible playbook register in a variable the result of including another playbook?

For example, would the following register the result of executing tasks/foo.yml in result_of_foo?

tasks:
  - include: tasks/foo.yml
  - register: result_of_foo

How else can Ansible record the result of a task sequence?

回答1:

The short answer is that this can't be done.

The register statement is used to store the output of a single task into a variable. The exact contents of the registered variable can vary widely depending on the type of task (for example a shell task will include stdout & stderr output from the command you run in the registered variable, while the stat task will provide details of the file that is passed to the task).

If you have an include file with an arbitrary number of tasks within it then Ansible would have no way of knowing what to store in the variable in your example.

Each individual task within your include file can register variables, and you can reference those variables elsewhere, so there's really no need to even do something like this.



回答2:

I was able to do this by passing a variable name as a variable to be used in the task. I included my main.yaml and included cgw.yaml files below.

main.yaml:

- name: Create App A CGW
  include: cgw.yaml
  vars:
    bgp_asn: "{{ asn_spoke }}"
    ip_address: "{{ eip_app_a.public_ip }}"
    name: cgw-app-a
    region: "{{ aws_region }}"
    aws_access_key: "{{ ec2_access_key }}"
    aws_secret_key: "{{ ec2_secret_key }}"
    register: cgw_app_a

cgw.yaml:

- name: "{{ name }}"
  ec2_customer_gateway:
    bgp_asn: "{{ bgp_asn }}"
    ip_address: "{{ ip_address }}"
    name: "{{ name }}"
    region: "{{ region }}"
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
  register: "{{ register }}"