Category Archives: Ansible

Ansible script examples

EXAMPLE: Restart NTP daemon in Linux

First create the playbook pbntprestart.yml as below and run using this command. The cdhservers is where you define all your hostnames.

$ ansible-playbook pbntprestart.yml -u root -k

The content of pbntprestart.yml  is below:

$ cat pbntprestart.yml

– hosts: cdhservers
tasks:

– name: NTPRESTART
shell: service ntpd restart
register: out1
– debug: var=out1.stdout_lines

– name: NTPSTATUS
shell: ntpstat
register: out2
– debug: var=out2.stdout_lines

– name: NTPDSTATUS
shell: service ntpd status
register: out3
– debug: var=out3.stdout_lines

 

EXAMPLE: Run step by step each ansible task

ansible-playbook pbtest.yml -u userid -k –step

 

EXAMPLE: Check linux system basic status

$ cat pbchkuptime.yml

– hosts: cdhservers
tasks:
– name: CHECKUPTIME
shell: uname -a;uptime;free -h;df -h
register: out1
– debug: var=out1.stdout_lines

 

Advertisements

Install Ansible on Windows 10 WSL-Ubuntu

Steps to install Ansible on Windows 10.

First we need to enable Windows Subsystem for Linux (Beta) which is an Ubuntu linux on Windows. Complete the steps in this blog:

https://plenium.wordpress.com/2017/11/21/install-linux-on-windows/

 

<<<NEXT STEPS>>>

Install Ansible Latest Releases Via Apt (Ubuntu)

To configure the PPA on your machine and install ansible run these commands:

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

After the Ansible install check basic things:

# ansible –version

ansible 2.4.1.0

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/dist-packages/ansible

executable location = /usr/bin/ansible

python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

The windows drives are mounted in the Subsystem inside the /mnt directory

Open a bash prompt, and cd into your Windows user’s Documents directory:

$cd /mnt/c/Users/youruserid/Documents/

Create a new test playbook:

$ vim test.yml

Add the following contents (note the — should start at column 1):

  – hosts: localhost

    tasks:

      – debug: msg=”Ansible is working!”

 

Run the playbook with the command

$ ansible-playbook test.yml –connection=local

[WARNING]: Could not match supplied host pattern, ignoring: all

[WARNING]: provided hosts list is empty, only localhost is available

PLAY [localhost] *************************************************************************************

TASK [Gathering Facts] *************************************************************************************************ok: [localhost]

 

TASK [debug] ***********************************************************************************************************ok: [localhost] => {

“msg”: “Ansible is working!”

}

 

PLAY RECAP *************************************************************************************************************localhost                  : ok=2    changed=0    unreachable=0    failed=0

 

This indicates that Ansible was successfully installed and you can start using it.

 

EXAMPLES:

Add the following servers in the /etc/ansible/hosts file so ansible can find the servers to run the commands on:

[cdhservers]

10.x.x.1

10.x.x.2

10.x.x.3

10.x.x.4

 

Create a playbook file pb1.yml with below lines:


– hosts: cdhservers
tasks:
– name: DISKSPACE
shell: df -h
register: out1
– debug: var=out1.stdout_lines

– name: CPU/RAM
shell: vmstat 3 3
register: out2
– debug: var=out2.stdout_lines

– name: FREEMEM
shell: free
register: out3
– debug: var=out3.stdout_lines

 

Run this using the command which will show the output below:

$ ansible-playbook pb1.yml -u root -k

SSH password:

PLAY [cdhservers] *************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************
ok: []
TASK [DISKSPACE] *************************************************************************************
changed: []

TASK [debug] *************************************************************************************
ok: [] => {
“out1.stdout_lines”: [
“Filesystem Size Used Avail Use% Mounted on”,
“/dev/mapper/vg_trhel6-lv_root”,
” 250G 93G 155G 38% /”,
“tmpfs 7.8G 0 7.8G 0% /dev/shm”,
“/dev/sda1 477M 105M 348M 24% /boot”,
“cm_processes 7.8G 43M 7.8G 1% /var/run/cloudera-scm-agent/process”
]
}

TASK [CPU/RAM] *************************************************************************************
changed: []

TASK [debug] *************************************************************************************
ok: [] => {
“out2.stdout_lines”: [
“procs ———–memory———- —swap– —–io—- –system– —–cpu—–“,
” r b swpd free buff cache si so bi bo in cs us sy id wa st”,
” 7 0 445172 554660 209108 4151844 0 0 29 92 3 1 7 1 91 0 0\t”,
” 0 0 445172 553932 209108 4151972 0 0 0 693 3774 12904 3 1 96 0 0\t”,
” 0 0 445172 553088 209108 4152268 0 0 0 4 5641 16558 8 2 89 0 0\t”
]
}

TASK [FREEMEM] *************************************************************************************
changed: []

TASK [debug] ***********************************************************************************************************************************************************************************************************************************
ok: [] => {
“out3.stdout_lines”: [
” total used free shared buffers cached”,
“Mem: 16333920 15779352 554568 83820 209108 4152180”,
“-/+ buffers/cache: 11418064 4915856”,
“Swap: 2097148 445172 1651976”
]
}

 

EXAMPLE:Change a users password

$ansible all -m shell -a “echo passwordxxx | passwd –stdin useridxxx” -u root -k

SSH password:

| SUCCESS | rc=0 >>

Changing password for user useridxxx.

passwd: all authentication tokens updated successfully.

 

 

Reference:

https://www.jeffgeerling.com/blog/2017/using-ansible-through-windows-10s-subsystem-linux