Ansible - Tips and tricks

Posted on March 19, 2018, 5:15 pm by about-dev.com


The documentation from the Ansible website it's awsome, but when you don't have enough time to read it and you just want an quick answer this is not the best place to go because there is no search feature available.

That's why here I'll write about some of the stuffs I needed and had a long search for the solution.

 

How to check the syntax of a playbook

To check the syntax of a playbook, use ansible-playbook with the --syntax-check flag. This will run the playbook file through the parser to ensure its included files, roles, etc. have no syntax problems.

>>ansible-playbook my_playbook.yml --syntax-check

 

How to see which hosts would be affected by a playbook

>>ansible-playbook my_playbook.yml --list-hosts

 

How does variable precedence works 

(from the official Ansible website)

Child groups override parent groups and hosts always override their groups.

1. Group variables
---
# file: /etc/ansible/group_vars/boston
ntp_server: boston-time.example.com

2. Host variables
---
# file: /etc/ansible/host_vars/xyz.boston.example.com
ntp_server: override.example.com

3. Role variables
---
# file: roles/x/defaults/main.yml
# if not overridden in inventory or as a parameter, this is the value that will be used
http_port: 80

---
# file: roles/x/vars/main.yml
# this will absolutely be used in this role
http_port: 80

 

How to use variables from inventories group_vars into roles

Well, you can't :). I've discovered this the hard way.

If you define a variable in an inventory group_vars file, that variable will only be available to the tasks section from a playbook.

To use a variable in an include_role / import_role section from a playbook you have to use the vars_files in that playbook.


Leave a Comment:

User
Email
Website

Blog Search

Popular Blog Categories

Newsletter

Want to be informed about latest posts? Subscribe to our newsletter