Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 287
Derek Mahar Avatar asked Nov 13 '15 20:11

Derek Mahar


People also ask

How does register work in Ansible?

Ansible register is a way to capture the output from task execution and store it in a variable. This is an important feature, as this output is different for each remote host, and the basis on that we can use conditions loops to do some other tasks. Also, each register value is valid throughout the playbook execution.

How do you call a register variable in Ansible?

Ansible registers are used when you want to capture the output of a task to a variable. You can then use the value of these registers for different scenarios like a conditional statement, logging etc. The variables will contain the value returned by the task. The common return values are documented in Ansible docs.

How do you store output of any task into a variable from playbook?

Register Task Output: Create the playbook to execute the “df” command to check the /boot usage. Use “register” to store the output to a variable. 2. Run the playbook to see the result.


2 Answers

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.

like image 190
Bruce P Avatar answered Sep 16 '22 13:09

Bruce P


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 }}"
like image 20
mcaulifn Avatar answered Sep 20 '22 13:09

mcaulifn