Ansible : Task output redirection to the next task as input

ansibleansible-playbook

Hi I am able to get the desired output in my playbook below where I am using 2 tasks as follows aws-create-rds and aws-create-route53-record

---
# Playbook for creating aws rds instance and then creating route53 dns record.

- name: setup aws-rds-instances
  hosts: localhost
  roles:
     - aws-create-rds

task definition for the aws-create-rds is as below.

---

- name: create an rds instance
  rds:
    command: create
    aws_access_key: "{{ aws_create_rds.access_key }}"
    aws_secret_key: "{{ aws_create_rds.secret_key }}"
    db_name: "{{ aws_create_rds.db_name }}"
    instance_name: "{{ aws_create_rds.name }}"
    db_engine: "{{ aws_create_rds.db_engine }}"
    size: "{{ aws_create_rds.db_size }}"
    instance_type: "{{ aws_create_rds.instance_type }}"
    username: "{{ aws_create_rds.username }}"
    password: "{{ aws_create_rds.password }}"
    subnet: "{{ aws_create_rds.subnet }}"
    region: "{{ aws_create_rds.region }}"
    zone: "{{ aws_create_rds.zone }}"
    publicly_accessible: "{{ aws_create_rds.access }}"
    backup_retention: "{{ aws_create_rds.retention }}"
    vpc_security_groups: "{{ aws_create_rds.aws_sg_name }}"
    port: "{{ aws_create_rds.port }}"
    wait: yes
    wait_timeout: 900
    tags:
      created_by: ansible
  register: rds



 - name: Create a route53 record for RDS instance.
      route53:
        state: present
        aws_access_key: "{{ aws_create_route53_record.access_key }}"
        aws_secret_key: "{{ aws_create_route53_record.secret_key }}"
        zone: "{{ aws_create_route53_record.zone }}"
        hosted_zone_id: "{{ aws_create_route53_record.id }}"
        type:  "{{ aws_create_route53_record.type }}"
        value: "{{ rds.instance.endpoint }}"
        record: "{{  aws_create_route53_record.record }}"
        private_zone: "{{  aws_create_route53_record.private_zone }}"
        ttl: 30

Now since the there are 2 task in this task I want them to split into 2 different roles. 1st as aws-create-rds and 2nd as aws-create-route53-record
I would want to use them as independent roles in ansible in future however I am not sure how I can pass endpoint value coming from aws-create-rds task and pass it to aws-create-route53-record and use it as value in route53 dns record. I've checked rds module documentation and there is no return value for endpoint in it. Also there are 2 use cases for this given as below.

 1. route53 record value can be passed through group_vars OR
 2. route53 record value can be passed from any previous ansible task executed.

I want to handle both the condition in aws-create-route53-record task. Any clue how this can be achieved. Environment variable or anything. Thanks in advance.

Best Answer

In order to achieve this I wrote 2 different roles/tasks and calling them in a playbook. Below are the tasks. First is called aws-create-rds

- name: create an rds instance
  rds:
    command: create
    aws_access_key: "{{ aws_create_rds.access_key }}"
    aws_secret_key: "{{ aws_create_rds.secret_key }}"
    db_name: "{{ aws_create_rds.db_name }}"
    instance_name: "{{ aws_create_rds.name }}"
    db_engine: "{{ aws_create_rds.db_engine }}"
    size: "{{ aws_create_rds.db_size }}"
    instance_type: "{{ aws_create_rds.instance_type }}"
    username: "{{ aws_create_rds.username }}"
    password: "{{ aws_create_rds.password }}"
    subnet: "{{ aws_create_rds.subnet }}"
    region: "{{ aws_create_rds.region }}"
    publicly_accessible: "{{ aws_create_rds.publicly_access }}"
    backup_retention: "{{ aws_create_rds.retention }}"
    vpc_security_groups: "{{ aws_create_rds.aws_sg_name }}"
    multi_zone: "{{ aws_create_rds.multi_zone }}"
    port: "{{ aws_create_rds.port }}"
    wait: yes
    wait_timeout: 900
    tags:
      created_by: ansible
  register: rds

- name: Assigning rds endpoint value to variable.
  set_fact:
    endpoint_host: "{{ rds.instance.endpoint }}"

And second task is aws-create-route53-record.

---
-
  name: "Set facts of record value"
  set_fact:
    record_value: "{{ aws_create_route53_record.value }}"
-
  name: "If record value not present, Look for endpoint-host variable"
  set_fact:
    record_value: "{{ endpoint_host }}"
  when: "aws_create_route53_record.value == \"\""
-
  name: "Create a route53 record for RDS instance."
  route53:
    aws_access_key: "{{ aws_create_route53_record.access_key }}"
    aws_secret_key: "{{ aws_create_route53_record.secret_key }}"
    hosted_zone_id: "{{ aws_create_route53_record.hosted_zone_id }}"
    private_zone: "{{  aws_create_route53_record.private_zone }}"
    record: "{{  aws_create_route53_record.record }}"
    state: present
    ttl: 30
    type: "{{ aws_create_route53_record.type }}"
    value: "{{ record_value }}"
    zone: "{{ aws_create_route53_record.zone }}"
Related Topic