Ad-Hoc Ansible command's

Logging into Unix/Linux server where you have installed the Ansilbe. If you have not installed yet then refer from 

Let’s discusses one by one Ansible command’s:

ansible –version
[]$ ansible --version
ansible 1.7

ansible –version will display current installed version of ansible in your local system.

Type the ansible
[ ~]$ ansible
Usage: ansible  [options]

                        module arguments
  -k, --ask-pass        ask for SSH password
  --ask-su-pass         ask for su password
  -K, --ask-sudo-pass   ask for sudo password
  --ask-vault-pass      ask for vault password
  -B SECONDS, --background=SECONDS
                        run asynchronously, failing after X seconds
  -C, --check           don't make any changes; instead, try to predict some
                        of the changes that may occur
  -c CONNECTION, --connection=CONNECTION
                        connection type to use (default=smart)
  -f FORKS, --forks=FORKS
                        specify number of parallel processes to use
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory-file=INVENTORY
                        specify inventory host file
  -l SUBSET, --limit=SUBSET
                        further limit selected hosts to an additional pattern
  --list-hosts          outputs a list of matching hosts; does not execute
                        anything else
  -m MODULE_NAME, --module-name=MODULE_NAME
                        module name to execute (default=command)
  -M MODULE_PATH, --module-path=MODULE_PATH
                        specify path(s) to module library
  -o, --one-line        condense output
                        set the poll interval if using -B (default=15)
                        use this file to authenticate the connection
  -S, --su              run operations with su
  -R SU_USER, --su-user=SU_USER
                        run operations with su as this user (default=root)
  -s, --sudo            run operations with sudo (nopasswd)
  -U SUDO_USER, --sudo-user=SUDO_USER
                        desired sudo user (default=root)
  -T TIMEOUT, --timeout=TIMEOUT
                        override the SSH timeout in seconds (default=10)
  -t TREE, --tree=TREE  log output to this directory
                        connect as this user (default=ansible)
                        vault password file
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)
  --version             show program's version number and exit

free -g or free -m command in Unix/Linux, Similarly in Ansible:
[ ~]$ free -m
             total       used       free     shared    buffers     cached
Mem:         15951      15459        491          0       1613      11701
-/+ buffers/cache:       2144      13806
Swap:         1023          0       1023

[ ~]$ ansible local -a "free -m" -u ansible -k
SSH password: | success | rc=0 >>
             total       used       free     shared    buffers     cached
Mem:         15951      15481        470          0       1613      11701
-/+ buffers/cache:       2166      13785
Swap:         1023          0       1023

Where as local is the name of host configure in host config file for ansible
-u ansible is name of user
-k it will prompt the password of user ansible
For viewing host details see the

vi /etc/ansible/hosts
Due to that we are getting same result for what we have perform for unix command free -m
and ansible free -m.

Note: we can perform any Unix command in this pattern by ansible command.

df -h in Unix & Ansible
[ ~]$ df -h
Filesystem                  Size  Used Avail Use% Mounted on
/dev/                       5.8G  1.9G  3.7G  34% /
tmpfs                       7.8G     0  7.8G   0% /dev/shm
/dev/                       485M   11M  449M   3% /data

[ ~]$ ansible local -a "df -h" -u ansible -k
Filesystem                  Size  Used Avail Use% Mounted on
/dev/                       5.8G  1.9G  3.7G  34% /
tmpfs                       7.8G     0  7.8G   0% /dev/shm
/dev/                       485M   11M  449M   3% /data

There is no need to logging into several servers each time and get the details as required, just
control from ansible server with password less ssh configuration.

Book: DevOps by Jeff Geerling

No comments:

Post a Comment

Please provide your input, it will honer for me...