Ansible adhoc commands

by admin March 10, 2020 at 11:43 am

Ansible adhoc commands are useful to perform the tasks in a single time. Mostly adhoc commands are using to check the log information, service status and gather information of cpu, memory etc.,

We will see some frequent using modules with examples.

1. user
2. file
3. copy
4. service

1. How to create a user


[ansible@rameshmsr11c ~]$ ansible all -b -m user -a "name=ramesh"
rameshmsr13c | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "append": false, 
    "changed": false, 
    "comment": "", 
    "group": 1008, 
    "home": "/home/ramesh", 
    "move_home": false, 
    "name": "ramesh", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 1007
}
rameshmsr12c | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "append": false, 
    "changed": false, 
    "comment": "", 
    "group": 1008, 
    "home": "/home/ramesh", 
    "move_home": false, 
    "name": "ramesh", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 1007
}
[ansible@rameshmsr11c ~]$ 

2. How to create a directory

[ansible@rameshmsr11c ~]$ ansible all -b -m file -a "path=/home/ramesh/test state=directory owner=ramesh group=ramesh"
rameshmsr13c | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1008, 
    "group": "ramesh", 
    "mode": "0755", 
    "owner": "ramesh", 
    "path": "/home/ramesh/test", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 1007
}
rameshmsr12c | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1008, 
    "group": "ramesh", 
    "mode": "0755", 
    "owner": "ramesh", 
    "path": "/home/ramesh/test", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 1007
}

3. How to perform the remote copy of a file


[ansible@rameshmsr11c ~]$ ansible all -b -m copy -a "src=/home/ansible/test.txt dest=/home/ramesh/test/ owner=ramesh group=ramesh mode=755"
rameshmsr13c | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "9119d1d849d23913e140395d4970185bf839297d", 
    "dest": "/home/ramesh/test/test.txt", 
    "gid": 1008, 
    "group": "ramesh", 
    "md5sum": "06cf090a7cc22bacaf973d9e29c06ed1", 
    "mode": "0755", 
    "owner": "ramesh", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 16, 
    "src": "/home/ansible/.ansible/tmp/ansible-tmp-1583840916.11-198029487586843/source", 
    "state": "file", 
    "uid": 1007
}
rameshmsr12c | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "9119d1d849d23913e140395d4970185bf839297d", 
    "dest": "/home/ramesh/test/test.txt", 
    "gid": 1008, 
    "group": "ramesh", 
    "md5sum": "06cf090a7cc22bacaf973d9e29c06ed1", 
    "mode": "0755", 
    "owner": "ramesh", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 16, 
    "src": "/home/ansible/.ansible/tmp/ansible-tmp-1583840916.1-204290785592887/source", 
    "state": "file", 
    "uid": 1007
}

4. How to start and enable the service


[ansible@rameshmsr11c ~]$ ansible all -b -m service -a "name=auditd state=started enabled=yes"
rameshmsr13c | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "enabled": true, 
    "name": "auditd", 
    "state": "started", 
    "status": {

Add Comment