Thursday, 13 February 2020

Ansible AD HOC commands examples

1
=======
Use the ping module to ping all the nodes in your inventory:-

[sai@san-master-anisble ~]$cat /etc/ansible/hosts
192.161.1.10

[sai@san-master-anisble ~]$ ansible all -m ping -u root -k
SSH password: 
192.161.1.10| SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}


2.
===
[root@san-master-anisble ~]# ansible webservers -m shell -a "/sbin/reboot" -u root -k
SSH password: 
192.161.1.11 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Shared connection to 10.219.39.151 closed.", 
    "unreachable": true
}


3.
=============from Control server to Traget Server=============
[root@san-master-anisble opt]# ansible webservers -m copy -a "src=/opt/test2.txt dest=/tmp/test2.txt" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "cdcdb882c45c507f08092f6247ca0c25b13e4ee6", 
    "dest": "/tmp/test2.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "fe42c7a375851a6e6ab6efd67b8895f8", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 14, 
    "src": "/root/.ansible/tmp/ansible-tmp-1580943557.82-148407876984882/source", 
    "state": "file", 
    "uid": 0
}
[root@san-master-anisble opt]# 


4.
================================Ctrl:-managed server to managed server================
[root@san-master-anisble ~]# ansible webservers -m copy -a "src=/opt/test1.txt dest=/tmp/test1.txt remote_src=TRUE" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "dfd5f9682ccd17adb12394fd1e01ed677e6bf5f2", 
    "dest": "/tmp/test1.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "de556121cc4cbc5b33dde4ec60ee507f", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
    "size": 25, 
    "src": "/opt/test1.txt", 
    "state": "file", 
    "uid": 0
}


5.
==============
[root@san-master-anisble opt]# ansible webservers -m file -a "dest=/opt/test1.txt mode=644 remote_src=TRUE" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/opt/test1.txt", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 25, 
    "state": "file", 
    "uid": 0
}
[root@san-master-anisble opt]# 


6.
==================================

[root@san-master-anisble opt]# ansible local -m file -a "dest=/opt/test2.txt mode=755" -u root -k
SSH password: 
192.161.1.10 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}


7.
-----
[root@san-master-anisble opt]# ansible local -m file -a "dest=/opt/test2.txt mode=755" -u root -k
SSH password: 
192.161.1.10 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/opt/test2.txt", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 14, 
    "state": "file", 
    "uid": 0
}
[root@san-master-anisble opt]# ls -ltrh


8.
=========how to delete files on control server ==
[root@san-master-anisble opt]# ansible local  -m file -a "dest=/opt/test2.txt state=absent" -u root -k
SSH password: 
192.161.1.10 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/opt/test2.txt", 
    "state": "absent"
}
[root@san-master-anisble opt]# 



9.
============how to delete files on managed server 
[root@san-master-anisble opt]# ansible webservers -m file -a "dest=/opt/test1.txt state=absent remote_src=TRUE" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/opt/test1.txt", 
    "state": "absent"
}


10.
[root@san-master-anisble opt]# ansible webservers -m file -a "path=/opt/a state=absent remote_src=TRUE" -u root -k
SSH password: 
192.161.1.11| CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/opt/a", 
    "state": "absent"
}
[root@san-master-anisble opt]# 


11.
========managed servers file permission/ownership change==========
[root@san-master-anisble tmp]# ansible webservers -m file -a "dest=/opt/yum.log mode=755 owner=noc group=noc  remote_src=TRUE" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1000, 
    "group": "noc", 
    "mode": "0755", 
    "owner": "noc", 
    "path": "/opt/yum.log", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 1000
}
[root@san-master-anisble tmp]# 


12.
=========================Dir creation on managed server=========
[root@san-master-anisble tmp]# ansible webservers -m file -a "dest=/opt/sankar mode=755 owner=noc group=noc state=directory  remote_src=TRUE" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1000, 
    "group": "noc", 
    "mode": "0755", 
    "owner": "noc", 
    "path": "/opt/sankar", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 1000
}
[root@san-master-anisble tmp]# 


13.
=========managing packages in managed servers===
[root@san-master-anisble tmp]# ansible webservers -m yum -a "name=http* state=present" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "http*"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: centos.excellmedia.net\n * 
....
....
....

 
14.  
======== latest version update ====
[root@san-master-anisble tmp]# ansible webservers -m yum -a "name=http* state=latest" -u root -k
SSH password: 
192.161.1.11 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "changes": {
        "installed": [], 
        "updated": []
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "All packages providing http* are up to date", 
        ""
    ]
}
[root@san-master-anisble tmp]# 


14.
=========service madule on managed server(start http service start)
[root@san-master-anisble tmp]# ansible  webservers -m service -a "name=httpd state=started" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "httpd", 
    "state": "started", 
.....
.....


16.
---- stopped service on managed server ===

[root@san-master-anisble tmp]# ansible  webservers -m service -a "name=httpd state=stopped" -u root -k
SSH password: 
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "httpd", 
    "state": "stopped", 
.....
.....


17.
============== Gathering facts(discoverd variables about a system =========
[root@san-master-anisble tmp]# ansible all -m setup -u root -k
SSH password: 
192.161.1.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.219.39.151"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::dbdb:a08a:cf20:ddb2"
        ], 
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "12/01/2006", 
        "ansible_bios_version": "VirtualBox", 
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-1062.9.1.el7.x86_64", 
            "LANG": "en_US.UTF-8", 
            "crashkernel": "auto", 
            "quiet": true, 
            "rd.lvm.lv": "cl/swap", 
            "rhgb": true, 
            "ro": true, 
            "root": "/dev/mapper/cl-root"
        }, 
        "ansible_date_time": {
....
....
....



18.
===== user creation ======
[root@san-master-anisble ~]# ansible all -m user -a "name=sai1 password=sai@123" -u root -k
SSH password: 
[WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this module to work properly.
192.161.1.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1001, 
    "home": "/home/sai1", 
    "name": "sai1", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001
}


19.
------------------------------------------------------------------------
[root@san-master-anisble ~]# ansible localhost -m ping -e 'ansible_python_interpreter="/usr/bin/env python"'
localhost | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@san-master-anisble ~]# 


20.
--
[root@san-master-anisble ~]# ansible --version
ansible 2.9.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[root@san-master-anisble ~]# 


21.
---
[root@san-master-anisble ~]# ansible-config --version
ansible-config 2.9.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-config
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[root@san-master-anisble ~]# 

Tuesday, 4 February 2020

First Ansible:-Use the ping module to ping all the nodes in your inventory

Use the ping module to ping all the nodes in your inventory:

[sai@san-master-anisble ~]$cat /etc/ansible/hosts
192.161.1.10

[sai@san-master-anisble ~]$ ansible all -m ping -u root -k
SSH password:
192.161.1.10| SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}


[sai@san-master-anisble ~]$ ansible all -a "/bin/echo hello" -u root -k
SSH password:
192.161.1.11 | CHANGED | rc=0 >>
hello
[sai@san-master-anisble ~]$


If you need privilege escalation (sudo) to run a command, pass the become flags:
Case1:- noc // from noc user switched to root

[sai@san-master-anisble ~]$ ansible all -m ping -u noc --become
192.161.1.11 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

Case 2: from noc user to switch to noc1
[sai@san-master-anisble ~]$ ansible all -m ping -u noc --become --become-user noc1
192.161.1.11 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[sai@san-master-anisble ~]$