---
# System prepended metadata

title: Ad-hoc commands

---


[toc]



### Ad Hoc Commands - Basic Operations
````go=1
Command Syntax:
ansible <host-group> -m <module-name> -a <arguments>

Example:
ansible nodes -m shell -a 'ls /tmp'


More ad-hoc command practice:

ansible --list-hosts all
ansible --list-hosts <groupname>
ansible nodes -m shell -a 'ls /tmp'
ansible all -m shell -a 'hostname'
ansible all -m shell -a 'uptime'
ansible all -m shell -a 'ip r'
ansible nodes -m shell -a 'fdisk -l' --become
ansible webservers -m user -a 'name=sk12k password=123456' --become
ansible all -m shell -a uptime
ansible all -m shell -a "free -m"
````
### Install/ Uninstall packages (state = present / absent / latest)]
````go=1
ansible nodes -m shell -a 'sudo apt update -y' -b
ansible nodes -m package -a 'name=git state=present' -b

Read the following documentation: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/package_module.html
````

### File Operations
````go=1
Create a file:
ansible nodes -m file -a 'path=/tmp/adhoc1.txt mode=755 state=touch'

Delete a file:
ansible nodes -m file -a 'path=/tmp/adhoc1.txt state=absent'

**validation**
ansible nodes -m shell -a 'ls /tmp'
````

### Copy a file from Controller to all the nodes
````go=1
Create a file which will be the source:
echo "put your content here" >> /tmp/adhoc1

Run following Ansible command to copy to remote nodes
ansible nodes -m copy -a 'src=/tmp/adhoc1 dest=/tmp/adhoc1'
````

### Create a directory on remote nodes using Ansible
````go=1
ansible nodes -m file -a 'path=/tmp/testdirectory_1 mode=755 state=directory'
````

### Working with Ansible Facts (System Properties)

#### Setup Module
````go=1
ansible worker1 -m setup
ansible worker1 -m setup | less
ansible worker1 -m setup -a "filter=*ipv4*"
ansible worker1 -m setup -a "filter=*os_family*"
ansible all -m setup -a "filter=*os_family*"
````
