cancel
Showing results for 
Search instead for 
Did you mean: 
Henry_Maine
Flight Engineer Flight Engineer
Flight Engineer
  • 17.1K 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
vj3k
Flight Engineer Flight Engineer
Flight Engineer
  • 5,996 Views

One of the suggestion which i usually ensure to discuss is the name of each task.I suggest the task name should be name: Task 1 - TaskName by placing task numbers you have two benefits, 1. You know how many task you have in play 2. Troubleshooting, you can simply search for the failed task number and fix it.

Cheers !!
Vijay
DanK
Moderator
Moderator
  • 5,963 Views

I used to do this too, but I don't any longer.  I found that if I needed to add a task or delete a task, I had to refactor a lot.

I also found that it made using roles more complicated.

I now use the "--list-tasks" and "--list-tags" extensively.

If playbooks are using roles instead of long tasks lists, the Task title will be prefixed with the name.  So you get "<role>: Task title".  This makes it easier to find tasks as well.

vj3k
Flight Engineer Flight Engineer
Flight Engineer
  • 5,942 Views

Yeah that's true, there would be way too much of editing in case you need to make a change.I prefer --list-tasks for listing the plays,tasks and tags.Have you noticed any extra info with --list-tags ?

Cheers !!
Vijay
0 Kudos
RickMartins
Flight Engineer Flight Engineer
Flight Engineer
  • 5,939 Views

Top tip from me - Create/Define a style guide. It makes life so much easier if everyone in your team/group/organization is working to the same standard.

Mine has things in it like:

  1. Always define booleans a particular way i.e. true/false, yes/no etc. in our case true/false.
  2. Always quote strings
  3. Add some 'metadata' at the top of the playbook to provide extra info - like the role it belongs to, it's relative path, the author's info and a description of what it does e.g. metadata.png
  4. Always use map syntax. It's easier to read and reduces potential changeset collisions in SCM
  5. etc. etc.

Our style guide is much longer than that but you get the idea. 

DanK
Moderator
Moderator
  • 5,885 Views

I think this is a good tip- one of the design goals of ansible is readability for people that are running playbooks.  Having a style guide helps keep playbooks readable and consistent.

0 Kudos
jpettorino
Flight Engineer Flight Engineer
Flight Engineer
  • 5,720 Views

I have a question on your point about booleans. Do all booleans accept the various forms you mention? Yes/No, True/False, 1/0 ?
I don't remember specifics, but I know I had a situation once where I had used one form and they play failed, but when I changed to another form it worked...
(Like I said, I don't recall specifics and I could be mistaken in my memory of the issue).
-- Jaffo

UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.
-- Dennis Ritchie
0 Kudos
DanK
Moderator
Moderator
  • 5,665 Views

All modules are supposed to accept all forms of booleans.  Where I've notice [someone needs to verify this...] that booleans behave a little differently is when variables are defined in an INI-style inventory file.   I think I've had problems in an inventory file using booleans with these different styles.

I prefer for these booleans, to use the "yes" or "no" paradigm.  Keeping that in mind, it helps me define better variable names knowning that the answer will be "yes" or "no".

Again - the aim for ansible is readability.  Can you use True/False and 1/0 - yes.  But from a stylistic point-of-view, I prefer yes/no for booleans to help improve readability.

buvaneshkumar
Mission Specialist
Mission Specialist
  • 5,815 Views

ricardodacosta
Moderator
Moderator
  • 5,764 Views

Good tip! ansible-pull is part of the standard ansible package. Thanks!

0 Kudos
JayK
Cadet
Cadet
  • 5,722 Views

Please go through these sprints to clear the EX407

1. How to configure ansible environment ?
2. After configuring ansible environment how to associate the passwordless client to ansible server ?
3. Ansible all -m ping
4. ansible all -m setup --> to see the factor values from the clients
5. ansible all -m command -a "yum install httpd -y" --> for this you must have the privilaged escalation section in the ansible.cfg file

6. yum module
7. copy module --> from ansible server to all clients
8. copy module --> within client to the client in different directory
9. copy module --> while copying from ansible server to clients you must take the back of that source file


10. service module --> how to start the service
11. service module --> how to make persistant for the service across reboots

12. handlers --> whenever you use replace module or lineinfile module to replace any word in configuration file you can use this handlers section.For this in replace or lineinfile module play you need to use notify section.

13.Tagging ---> whenever you want to control the particular playbook execution then you need to add the tag value.so that while executing playbooks call the releavent tags.

14. local variable --> you can use the variable in every playbook inside file

15. Global variable --> you can mention it in inventory file and you can use it from any playbook

16. jinja2 template --> you can create jinja2 template file with ansible factor varaibles and then use jinja2 file (.j2 file) via template module

17. i want to download some file from the web url using get_url module

18. i want to monitor web urls using uri module

19. if i want to run any 2 linux commands sametime then you can use shell module.

20.If you get re-occur activities, say every week 100 server build and in every server build you need to keep install and configure set of similier tasks and packages.for this kinds of secnario then you need to use role type 1 method.

21.If you get bulk activities then apply role type2 with (item,vars and with_items) the necessary parameters.

22. If you really want to control your datacenter infrastructure then you create your own register and then whenever you create the register then print with jason format using debug: var option and then create the condition from the output.

23. if you really want to secure the file and use ansible-vault command to view,encrypt,decrypt,edit,create the file

24. insert_file --> will help you to understand the word manipulation via ansible.

25. while takeing any file backup you want to take back up with time date stamp,then use the below format in the playbook...

vars:
date: "{{ lookup('pipe', 'date +%Y%m%d-%H%M') }}"


26. I want to configure NFS Server and nfs client and nfs partition disk.

mount.yml --> refer this file

27. Want to implement ansible in windows infrastructure,then u install ansible in linux and configure the same --> LIP for ansible administration for windows machines v1.0


28. ansible-galaxy init jay --> just create role folder skeleton

29. we have to create AWS EC2 instance via ansible.for this you need ACCESS KEY and SECRET ACCESS KEY ID

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