Wednesday, November 27, 2019

ansible basic ad-hoc comamnds

Here, ad-hoc means passing the commands from command line/shell as parameter.

ansible localhost -m ping

It will ping the current machine where ansible installed.

To run the ansible commands on other machines, we need to create an inventory file where all the nodes/client machines in the network are provided.

The default inventory file is /etc/ansible/hosts file where we will register the client machines

Here, we can provide machine name(if not resolved we can add to system hosts file) or ip address.



---
[gui]
ubuntu
centos

[nogui]
repo ansible_user=administrator

[lab]
172.20.0.[1:6]

more info: docs.ansible.com/ansible/intro_inventory.html

Some ad-hoc commands:

ansible -m ping all

python not required on agents to connect with ansible but some modules expects python.
some not required.

ansible -m raw -a 'usr/bin/uptime' all

ansible -m shell -a 'python -V' all
ansible -m shell -a 'python -V' nogui

ansible all -a 'uptime'
ansible all -a 'whoami'
ansible in ad-hoc mode(direct commands) doesn't elevate/escalate
it will do as regular user.
To escalate use become user option -b
ansible all -b -a 'whoami'
ansible all -m command -a 'reboot'
you will get error, so use -b.
ansible all -b -a 'shutdown -r'
shutdown -c will cancel shutdown

ansible all -b -m apt -a 'name=apache2 state=present'
ansible all -b -m yum -a 'name=httpd state=<present/absent/latest>'

enable a service at boot time:
ansible all -b -m service -a 'name=apache2 enabled=true'
ansible all -b -m service -a 'name=apache2 state=started'

modules:
------
------
specified with -m
command is the default module so if we don't specify -m modulename, it will pick command module.

command:
--
it directly runs the commands not using shell(bash/sh).
downside is can't use pipes/redirects.

shell:
--
can use pipes/redirects.
can get messed up with user settings.

raw:
--
just send commands over ssh, don't need python.

-----------
file module
ansible group1 -b -m file -a 'path=/root/hello.txt state=absent'

ansible gui -b -m copy -a 'src=/etc/hosts dest=/etc/hosts'
-----

No comments:

Post a Comment