Tips & Tricks

1) How to check Ansible version

# ansible --version

2) Only one argument with Setup module

We can give only one command line argument with setup  module using –a option. If you give two options then ansible will consider the one which is in the last.


3) How do I see all the inventory vars defined for my host?

You can see the resulting vars you define in inventory running the following command:

ansible -m debug -a "var=hostvars['hostname']" localhost

4) How become_user works

When using become_user to a user other than root, the module arguments are briefly written into a random tempfile in /tmp. These are deleted immediately after the command is executed. This only occurs when changing privileges from a user like ‘Dave’ to ‘shegal’, not when going from ‘Dave’ to ‘root’, or logging in directly as ‘Dave’ or ‘root’. If it concerns you that this data is briefly readable (not writable), avoid transferring unencrypted passwords with become_user set. 

5) How to list the available modules in the system

# ansible-doc -l

To know more about module like man page

# ansible doc <module name>

6) How to access environmental variables

if you just need to access existing variables, use the ‘env’ lookup plugin. For example, to access the value of the HOME environment variable on management machine:

vars:
   local_home: "{{ lookup('env','HOME') }}"

To make remote environment variables available via facts in the ‘ansible_env’ variable:

{{ ansible_env.SOME_VARIABLE }}

7) How to disable retry files creation by Ansible

[defaults]
retry_files_enabled = True  # Create them - the default
retry_files_enabled = False # Do not create them
retry_files_save_path = "~/" # The directory they will go into
# (home directory by default)

8) How to create a alias name for an ansible host

Suppose you have just static IPs and want to set up some aliases that live in your host file, or you are connecting through tunnels. You can also describe hosts like this:

jumper ansible_port=5555 ansible_host=192.0.2.50

No comments:

Post a Comment