vars in palybook with roles

95 views
Skip to first unread message

Nicolas G

unread,
May 23, 2013, 12:36:49 PM5/23/13
to ansible...@googlegroups.com
Hi All,

I have the bellow playbook.yml file but  having some problems to use the vars defined under the vars_files section. If I comment out the vars the playbook runs but then the values are unknown to the roles tasks...

I have defined different environment and password variables to use accordingly with my host file for staging, I would like to be able to deploy the same way this setup for dev/qa/production by changing only the host file and the vars_files.

  1. ---
  2.   vars_files:
  3.   - vars/users.yml
  4.  
  5.   #Password $ Access keys
  6.   - private_vars/staging-secret-vars.yml
  7.  
  8.   # Enviroment values
  9.   - vars/staging-us-east.yml
  10.  
  11. # Apply common configuration to all hosts
  12. - hosts: all
  13.   user: root
  14.   roles:
  15.   - common
  16.  
  17. - hosts: webservers
  18.   user: root
  19.   roles:
  20.   - apache
  21.   - web-moniroing
  22.  
  23. - hosts: dbervers
  24.   user: root
  25.   roles:
  26.   - mysql
  27.   - data
      - db-monitoring





Can you please have a look and tell me how can I include the vars file under vars in the top level directory where I have the playbook ? any recommendations of doing this in a more efficient way is also welcome.




Serge van Ginderachter

unread,
May 23, 2013, 1:19:51 PM5/23/13
to ansible...@googlegroups.com
You noted the vars_files: section outside of your plays (which I suppose would yield an error).
You need to repeat them for every play, and note vars_files: on the same level as hosts:






--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nicolas G.

unread,
May 23, 2013, 1:54:50 PM5/23/13
to ansible...@googlegroups.com

Is there a way I could have the vars set for all plays like I'm trying ?

Michael DeHaan

unread,
May 23, 2013, 4:57:15 PM5/23/13
to ansible...@googlegroups.com
Sure.

You could move your variables into group_vars/all (or another group) as a file, and that will load in every playbook you use.

Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Nicolas G

unread,
May 23, 2013, 6:25:06 PM5/23/13
to ansible...@googlegroups.com
if group_vars/<file-name> , does <file-name> must much the name of a group in the host file I'm running the playbook ?
can I have the group_var dir in a different location than /etc/ansible/group_var ?

Michael DeHaan

unread,
May 23, 2013, 7:18:33 PM5/23/13
to ansible...@googlegroups.com
It must match a name of the group the host is in, but does not have to be explicitly mentioned in the play.  All hosts are automatically in the "all" group.  For instance if you said "hosts: webservers", the "all" variables are still loaded.

The group_vars directory can exist anywhere but is expected to be in the same place as your inventory file.

so like -i /here/is/my/inventory_file

would mean /here/is/my/group_vars/all

If you specify a "-i" as a directory path to use multiple inventory files at once, the group_vars then lives *inside* the directory.


Nicolas G

unread,
May 24, 2013, 1:35:02 AM5/24/13
to ansible...@googlegroups.com
thanks. besides using group_vars is there another alternative ?

Michael DeHaan

unread,
May 24, 2013, 11:29:15 AM5/24/13
to ansible...@googlegroups.com
Yes.

You can define them in your inventory file (less attractive, IMHO) or pull them from your external inventory data source.

group_vars is the best way.

Nicolas G

unread,
May 27, 2013, 5:46:38 PM5/27/13
to ansible...@googlegroups.com
thanks.
Reply all
Reply to author
Forward
0 new messages