Can I set a variable on one host and use them on other hosts?

55 views
Skip to first unread message

Benjamin Bauer

unread,
Jul 23, 2014, 9:51:24 AM7/23/14
to ansible...@googlegroups.com
I'm writing a playbook to restore the latest backup of my DB on all my Database servers. This is done in two main steps:
  1. figure out which backup is the latest (once)
  2. download that file to all DB servers
  3. ...and run the restore

While step 2 and 3 are straight forward, my problem is to acquire the correct filename once and share it among all DB hosts.

I was trying something like this for testing, but I cant access `last_mongo` on the DB hosts.

---
- hosts: localhost
  tasks
:
   
- name: get filename of latest mongo backup
      shell
: s3cmd ls s3://my_backup_bucket/production/ | awk 'NF{p=$4}END{print p}'
     
register: lmongo
   
- set_fact: last_mongo="{{lmongo.stdout}}"
- hosts: db
  tasks
:
   
- debug:
        msg
: "mongo: {{latest_mongo}}"


Is there a (idiomatic) way to make this variable accessible to the db hosts?

Benjamin Bauer

unread,
Jul 23, 2014, 10:05:25 AM7/23/14
to ansible...@googlegroups.com
Update:

I figured it out:
---
- hosts: localhost
  tasks
:
   
- name: get filename of latest mongo backup
      shell
: s3cmd ls s3://my_backup_bucket/production/ | awk 'NF{p=$4}END{print p}'
     
register: lmongo
   
- set_fact: last_mongo="{{lmongo.stdout}}"
- hosts: db
  tasks
:
   
- debug:

        msg
: "mongo: {{{hostvars['localhost']['last_mongo']}}}"

Is there a better way?

Serge van Ginderachter

unread,
Jul 23, 2014, 1:19:02 PM7/23/14
to ansible...@googlegroups.com
Yes, use hostvars.localhost.lmongo.stdout


--
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-proje...@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/39e49d1f-3133-4aff-a987-7d3e0c213b04%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Benjamin Bauer

unread,
Jul 25, 2014, 8:30:54 AM7/25/14
to ansible...@googlegroups.com
Thanks for the good adivce!
Reply all
Reply to author
Forward
0 new messages