cancel
Showing results for 
Search instead for 
Did you mean: 
Henry_Maine
Flight Engineer Flight Engineer
Flight Engineer
  • 17K Views

Ansible Tips and Tricks

Does anyone have any useful or clever Ansible tricks that you routinely use in the course of your normal  daily activities?

22 Replies
JorgeAndrade
Flight Engineer Flight Engineer
Flight Engineer
  • 12.8K Views

It will be very useful to check this video.
https://www.youtube.com/watch?v=5BhAJ4mEfZ8
It contains some good point for better practice.
ricardodacosta
Moderator
Moderator
  • 12.7K Views

In training, I recommend the following in ~/.vimrc:

autocmd FileType yaml setlocal ai ts=2 sw=2 et nu colorcolumn=1,3,5,7,9,11,13

But use a good editor! (Atom,. Visual Studio Code, Sublime, etc)

Also, I keep my playbooks in separate directories (see screenshot), and every playbook is called playbook.yml in order to take better advantage of searching through my command history when using ansible-playbook playbook.yml and ansible-playbook --syntax-check playbook.yml

Screen Shot 2018-08-03 at 2.40.57 PM.png

 

I always encourage my students to  "tell the story of your playbook" i.e. articulate their playbook goals in point form. Those then become the names of your tasks. 

In response to a VT class request, I created https://gitlab.com/rgdacosta/sync-work which uploads their class created playbooks to the cloud, so that they could download them from their physical machines. I opened vim, and told the story of my playbook in front of them, then noted the modules which I thought would best satisfy my task goals, then figured out the specifics offline. They enjoyed that!

Parametize your playbooks - keep them as dynamic as you can.

When creating your playbook consider creating the associated undo or rollback tasks and tag them accordingly.

Have a style guide that specifies how you do things, from using variables, to how you declare booleans, to the use of includes. Have guidelines as to how playbooks ought to be written.

Always keep your playbooks under version control!

And that leads me to my last point, always give examples which are not covered in the courseware - add value. Exposure to scenarios is important - add value!

 

Idzuwan
Cadet
Cadet
  • 12.6K Views

Nice! I the last instructor we have did the in-house training was purely by the book guy which was a bit boring

0 Kudos
SandraM
Mission Specialist
Mission Specialist
  • 12.5K Views

I notice you have a different ansible.cfg for each of your directories.  Do you find those settings change a lot from playbook to playbook?  Right now, I have a main ansible.cfg that I use for all my tests (but I'm still in the experimental phase).

DanK
Moderator
Moderator
  • 12.4K Views

I'm a fan of this approach as well.  Each of my roles as an "ansible.cfg" in the test directory [I keep each role in a separate code repository], and each of my playbook projects [again, separate code repository] contains one or more ansible.cfg   Depending on the environment, the ansible.cfg can change quite a bit [Connection properties to Dev vs Prod environmments, etc.]  I find that it is useful to have ansible.cfg local to my project.  I can see where sharing a common ansible.cfg would be useful - YOu then have to worry about how to "share" that shared resource.  I've done that in the past using symlinks, but I haven't been to thrilled about the "elegance" of the solution.

bonnevil
Starfighter Starfighter
Starfighter
  • 12.5K Views

@ricardodacosta: Another vim option that's been suggested by one of the course instructors (I forget who, my apologies) is to use cursorcolumn (cuc) instead of colorcolumn. The cursorcolumn option will highlight the column your cursor is currently on, and may be a bit less cluttered approach.  But yeah, no matter what it helps to have a solid text editor that you're comfortable with and that helps you with syntax and indentation.

Sergey
Flight Engineer Flight Engineer
Flight Engineer
  • 12.4K Views

cursorcolumn can be annoying if it's always there. I enable and disable it by pressing F2.

:map <F2> :se invcursorcolumn<CR>


DariuszPuchalak
Flight Engineer
Flight Engineer
  • 12.7K Views

For finding out what might run (just statically):

ansible-playbook --list-tasks production.yml --tags bareos,workstation
ansible-playbook --list-tasks production.yml --skip-tags nagios

ansible-playbook production.yml --list-tasks --limit windows

For better understand what is in inventory:

ansible-inventory --graph

For better understanding how variables from host_vars, group_vars

ansible-inventory --list

ansible-inventory --graph --vars

 

Make lot's of sens for bigger projects. But it doesn't know anything about roles.

DanK
Moderator
Moderator
  • 12.4K Views

These are great tips - I think the "ansible-playbook" command is often under utilized.  It has a rich set of options that allow a lot of different interactions with a particular playbook.

 

For clarification, what did you mean by "it doesn't know anything about roles"?   Is that refering to the ansible-playbook --list-tasks option?

 

0 Kudos
Join the discussion
You must log in to join this conversation.