# Using results of RDS facts to update a template

**URL:** <https://forum.ansible.com/t/using-results-of-rds-facts-to-update-a-template/18803>\
**Category:** Ansible Project\
**Created:** [July 18, 2015, 2:03am UTC](https://forum.ansible.com/t/using-results-of-rds-facts-to-update-a-template/18803 "2015-07-18T02:03:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Thomas](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dave\_Thomas](https://forum.ansible.com/u/Dave_Thomas)\
**Post date:** [July 18, 2015, 2:03am UTC](https://forum.ansible.com/t/using-results-of-rds-facts-to-update-a-template/18803/1 "2015-07-18T02:03:05Z")

</div>

I’d trying to get the database endpoint into a Rails database.ml file. I know the RDS id, so I can get the endpoint using reds/command: facts.

Unfortunately, the RDS play has to use localhost, while the template write uses the actual target host.

So far I’ve done it using

```auto
- hosts: localhost
  connection: local
  gather_facts: False
  vars_files:
    - ../ec2/ec2_params.yml

  tasks:
    - rds:
        command: facts
        region: "{{ region }}"
        instance_name: "{{ database_instance }}"
      register: db_facts

    - name: save remote host
      copy:
        content: "{{ db_facts.instance }}"
        dest: /tmp/db_facts

- hosts: tag_class_apps
  become: yes
  become_user: "{{ app_user }}"
  gather_facts: False

  vars:
     db_facts: "{{ lookup('file', '/tmp/db_facts')|from_json }}"

  tasks:
    - name: Update database.yml with current rds endpoint
      template:
        src: ../roles/app_directories/templates/config/database.yml # this template uses db_facts.endpoint
        dest: "{{ shared_path }}/config/database.yml"I can’t help feeling I’m making this too complex. What should I be doing?

```

I can’t help feeling I’m making this too complicated. What magic am I missing?

Cheers

Dave

​

---

<div class="post-metadata">

**Author:** ![jon2](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jon2](https://forum.ansible.com/u/jon2)\
**Post date:** [July 18, 2015, 5:02pm UTC](https://forum.ansible.com/t/using-results-of-rds-facts-to-update-a-template/18803/2 "2015-07-18T17:02:12Z")

</div>

Rather than register the facts and write to a file, you can use a custom lookup to query the endpoint based on the instance name. Then you can put the following in your template file:

{{ lookup(‘aws\_rds\_endpoint\_port\_from\_instance\_name’, (region, database\_instance )) }}

where region is a variable containing the region you use and database\_instance is the name.

I’ve written some additional lookups, including one to get the endpoint of an RDS instance based on name here: [https://github.com/jonhadfield/ansible-lookups](https://github.com/jonhadfield/ansible-lookups)  
I’m also putting together a guide (nearly finished) that includes their use here: [http://lessknown.info](http://lessknown.info).

---

<div class="post-metadata">

**Author:** ![jon2](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jon2](https://forum.ansible.com/u/jon2)\
**Post date:** [July 18, 2015, 5:08pm UTC](https://forum.ansible.com/t/using-results-of-rds-facts-to-update-a-template/18803/3 "2015-07-18T17:08:35Z")

</div>

Oops, that should have read:  
{{ lookup(‘aws\_rds\_endpoint\_ **name** \_from\_instance\_name’, (region, database\_instance )) }}  
The other returns the port.

---

<div class="post-metadata">

**Author:** ![Baraa\_Basata1](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@Baraa\_Basata1](https://forum.ansible.com/u/Baraa_Basata1)\
**Post date:** [July 18, 2015, 5:33pm UTC](https://forum.ansible.com/t/using-results-of-rds-facts-to-update-a-template/18803/4 "2015-07-18T17:33:39Z")

</div>

Hi Dave,

I use a single play to get an RDS endpoint and write database.yml. It uses `local_action` to delegate the rds facts lookup to localhost.

For the example that you’ve shared, it would look like the following:

`

- hosts: tag\_class\_apps  
become: yes  
become\_user: “{{ app\_user }}”  
gather\_facts: False

tasks:

- local\_action:  
module: rds  
command: facts  
region: “{{ region }}”  
instance\_name: “{{ database\_instance }}”  
register: db\_facts

- name: Update database.yml with current rds endpoint  
template:  
src: …/roles/app\_directories/templates/config/database.yml

`

I hope this is helpful.

-Baraa

---

<div class="post-metadata">

**Author:** ![Dave\_Thomas](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dave\_Thomas](https://forum.ansible.com/u/Dave_Thomas)\
**Post date:** [July 19, 2015, 7:39pm UTC](https://forum.ansible.com/t/using-results-of-rds-facts-to-update-a-template/18803/5 "2015-07-19T19:39:24Z")

</div>

> Hi Dave,
> 
> I use a single play to get an RDS endpoint and write database.yml. It uses `local_action` to delegate the rds facts lookup to localhost.

Of course. Thank you!

Dave
