Discussion:
[ansible-project] Using results of RDS facts to update a template
Dave Thomas
2015-07-18 02:03:05 UTC
Permalink
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

- 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
​
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f27915cb-0909-4b82-a124-1baa19e5a250%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jon Hadfield
2015-07-18 17:02:12 UTC
Permalink
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
I'm also putting together a guide (nearly finished) that includes their use
here: http://lessknown.info.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/237cb674-5f4e-4680-b4bf-ccb5f59b9946%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jon Hadfield
2015-07-18 17:08:35 UTC
Permalink
Oops, that should have read:
{{ lookup('aws_rds_endpoint_*name*_from_instance_name', (region,
database_instance )) }}
The other returns the port.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/01c679a9-5f52-4e4a-be15-a4918bc8e7cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Baraa Basata
2015-07-18 17:33:39 UTC
Permalink
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
Post by Dave Thomas
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
- hosts: localhost
connection: local
gather_facts: False
- ../ec2/ec2_params.yml
command: facts
region: "{{ region }}"
instance_name: "{{ database_instance }}"
register: db_facts
- name: save remote host
content: "{{ db_facts.instance }}"
dest: /tmp/db_facts
- hosts: tag_class_apps
become: yes
become_user: "{{ app_user }}"
gather_facts: False
db_facts: "{{ lookup('file', '/tmp/db_facts')|from_json }}"
- name: Update database.yml with current rds endpoint
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
​
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2c4a80d0-0bcf-49bf-be07-b8723bc53b73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dave Thomas
2015-07-19 19:39:24 UTC
Permalink
Post by Baraa Basata
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
Post by Baraa Basata
​
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b1ee8edc-b5e1-4aea-95b9-525cb5a578e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...