I am currently replacing our chef setup with ansible. We have a set of web servers and a database server. The web servers are configured to connect to the database server and access different databases (1 database per webserver). These databases should be created by the db role and therein lies the problem. With chef I would have queried all web servers for their configured database and create those, but you cannot do that with ansible. I know, how to read facts of other hosts, but the database name is not a fact, but defined in the defaults/main.yml of the web role, so it is not available in the db role.
So what is the ansible way of reading a variable (not a fact) from another host?
Thanks
You might want to look at the fact caching feature that's been available as of version 1.8 of Ansible. It will cache facts in between runs of playbooks, so if you run your web server playbook then the facts regarding each web servers database should be cached and available when you then run the database playbook.
In the DB role, you can have a play with hosts being your webservers, but then use delegate_to to make the task (ensuring the DB's existence, or some such) on the DB server. The task would run for each webserver ON the DB server. See here for an example: http://docs.ansible.com/playbooks_delegation.html
Edit: Here's an brief example:
# db_role/tasks/main.yml
---
- name: Make sure web server DBs exist
hosts: webserver
tasks:
# This is contrived. I presume you have some way of knowing
# the DB(s) name for each web host
# This can come from host vars, discovered facts, etc
- set_facts:
db_name: {{ inventory_hostname}}
- name: Script that makes sure DB is there
command: create_db_if_not_exist.sh {{ db_name }}
delegate_to: db-server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With