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 http://www.tutorialbyexample.com 

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




ansible –version
[ansible@tutorialbyexample.com]$ ansible --version
ansible 1.7

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

Type the ansible
[ansible@tutorialbyexample.com ~]$ ansible
Usage: ansible [options]

Options:
-a MODULE_ARGS, --args=MODULE_ARGS
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
(default=N/A)
-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
(default=5)
-h, --help show this help message and exit
-i INVENTORY, --inventory-file=INVENTORY
specify inventory host file
(default=/etc/ansible/hosts)
-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
(default=/opt/ansible/ansible-1.7/library)
-o, --one-line condense output
-P POLL_INTERVAL, --poll=POLL_INTERVAL
set the poll interval if using -B (default=15)
--private-key=PRIVATE_KEY_FILE
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
-u REMOTE_USER, --user=REMOTE_USER
connect as this user (default=ansible)
--vault-password-file=VAULT_PASSWORD_FILE
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:
Unix:
[ansible@tutorialbyexample.com ~]$ free -m
total used free shared buffers cached
Mem: 15951 15459 491 0 1613 11701
-/+ buffers/cache: 2144 13806
Swap: 1023 0 1023
Ansible:
[ansible@tutorialbyexample.com ~]$ ansible local -a "free -m" -u ansible -k
SSH password:
127.0.0.1 | 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
[local]
127.0.0.1
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
Unix:
[ansible@tutorialbyexample.com ~]$ 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:
[ansible@tutorialbyexample.com ~]$ 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.

References
Book: DevOps by Jeff Geerling


No comments:

Post a Comment