Vars and register passing on to include playbooks

68 views
Skip to first unread message

Joe Louthan

unread,
May 19, 2016, 4:12:15 PM5/19/16
to Ansible Project
By all accounts that I can see, the following should work:

---
- hosts: server01
  remote_user: jlouthan
vars:
proddbhost: proddb01
  tasks:
      - name: Get current timestamp
        local_action: command bash -c 'date +%Y-%m-%d.%H%M'
        register: timestamp
        run_once: true

      - name: Get current datestamp
        local_action: command bash -c 'date +%Y-%m-%d'
        register: datestamp
        run_once: true
- include: playbook-01.yml

And within playbook-01.yml, I have:
---
- hosts: "{{ proddbhost }}"

Yet, I get:
ERROR! 'proddbhost' is undefined

Also, will the datestamp and timestamp I have registered pass on to the included playbook?

Brian Coca

unread,
May 20, 2016, 8:49:18 AM5/20/16
to ansible...@googlegroups.com
So vars seems to be incorrectly indented, also you should not need to add 'bash' to command

The registered var should persist but it is available only through the server01 host.


----------
Brian Coca

Joe Louthan

unread,
May 20, 2016, 9:03:45 AM5/20/16
to Ansible Project
indented var: that is a copy/paste mistake.

So those vars and register will not persist to any of the other include playbooks?

Joe Louthan

unread,
May 20, 2016, 9:04:42 AM5/20/16
to Ansible Project
---
- hosts: server01
  remote_user: jlouthan
 vars:
               proddbhost: proddb01
           
 tasks:
     - name: Get current timestamp
       local_action: command bash -c 'date +%Y-%m-%d.%H%M'
       register: timestamp
       run_once: true

      - name: Get current datestamp
       local_action: command bash -c 'date +%Y-%m-%d'
       register: datestamp
       run_once: true
                             
- include: playbook-01.yml




On Friday, May 20, 2016 at 7:49:18 AM UTC-5, Brian Coca wrote:

Brian Coca

unread,
May 20, 2016, 9:30:48 AM5/20/16
to ansible...@googlegroups.com
vars don't they are play specific, host vars (facts and registered vars) do persist, but they are associated to the specific host.

So in subsequent plays you might need to do hostvars['server01']['timestamp'] to get the var if they are not also targetting server01.



----------
Brian Coca

Joe Louthan

unread,
May 20, 2016, 9:48:38 AM5/20/16
to Ansible Project
So in order to access this var/register from server01, I could do something like in playbook-01.yml:

---
- hosts: "{{ hostvars['server01']['proddbhost'] }}"

(When I do that I get "ERROR! 'hostvars' is undefined"

Brian Coca

unread,
May 20, 2016, 9:56:35 AM5/20/16
to ansible...@googlegroups.com
hostvars are not accessible at the play level, but should be accessible within the play.


----------
Brian Coca

Joe Louthan

unread,
May 20, 2016, 10:31:35 AM5/20/16
to Ansible Project
When I do this:
{{ hostvars['server1']['timestamp.stdout'] }}

as in: 
      - name: "Backup {{ proddbname }} Database"
        mysql_db: "state=dump name={{ proddbname }} target={{ homedir }}{{ proddbname }}{{ hostvars['server01']['timestamp.stdout'] }}.sql.gz"

It gives me the error
fatal: [proddbhost]: FAILED! => {"failed": true, "msg": "'dict object' has no attribute 'timestamp.stdout'"}

Brian Coca

unread,
May 20, 2016, 12:56:12 PM5/20/16
to ansible...@googlegroups.com
you are mixing notations

{{ hostvars['server1']['timestamp']['stdout'] }}


----------
Brian Coca

Joe Louthan

unread,
May 20, 2016, 2:16:50 PM5/20/16
to Ansible Project
DING! That worked. Thank you so much!
Reply all
Reply to author
Forward
0 new messages