Ansible’s Feature:
- Accessed mostly through SSH ( it also has a paraminko and local modes)
- Based on an agent less architecture
- Have more than 200 built-in modules
- No custom infrastructure required
- Configuration (module, playbook) written in the easy to use YML format
- Ansible interacts with its clients either through playbooks or a command line tool ( ad-hoc command)
PARAMINKO – high-quality Python implementation of OpenSSH
Ansible components
- Inventory
- Playbooks
- Play
- Tasks
- Roles
- Handlers
- Templates
- Variables
Example Ad-Hoc commands
To transfer a file directly to many servers:
$ ansible hadoop -m copy -a “src=/etc/hosts dest=/tmp/hosts”
To ping the servers
[ovi@ ~]$ ansible last_bpm -m ping –ask-pass
SSH password:
192.168.18.207 | success >> {
“changed”: false,
“ping”: “pong”
}
192.168.18.208 | success >> {
“changed”: false,
“ping”: “pong”
}
192.168.18.206 | success >> {
“changed”: false,
“ping”: “pong”
}
Run ansible ad-hoc command to check OS
[root@ ansible]# ansible hadoop_dev -m command -a “uname -a” –ask-pass
SSH password:
192.168.68.119 | success | rc=0 >>
Linux dphdmst04 2.6.32-504.23.4.el6.x86_64 #1 SMP Fri May 29 10:16:43 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
192.168.68.118 | success | rc=0 >>
Linux dphdmst03 2.6.32-504.23.4.el6.x86_64 #1 SMP Fri May 29 10:16:43 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
192.168.68.117 | success | rc=0 >>
Linux dphdmst02 2.6.32-504.23.4.el6.x86_64 #1 SMP Fri May 29 10:16:43 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
192.168.68.116 | success | rc=0 >>
Linux dphdmst01 2.6.32-504.23.4.el6.x86_64 #1 SMP Fri May 29 10:16:43 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
Restart ntpd service on one server
# ansible 192.168.129.61 -m service -a “name=ntpd state=restarted” -k
SSH password:
192.168.129.61 | success >> {
“changed”: true,
“name”: “ntpd”,
“state”: “started”
}
[root@]# ansible hadoop -m shell -a “ps -e -o pcpu,pid,user,args|sort -k1 -nr|head -1” -k
SSH password:
192.236.1.52 | success | rc=0 >>
99.9 29068 root python /usr/bin/goferd
192.236.1.56 | success | rc=0 >>
12.9 7321 root /opt/microsoft/configmgr/bin/ccmexec.binsort: write failed:
192.236.1.53 | success | rc=0 >>
99.9 543544 root python /usr/bin/goferd
192.236.1.54 | success | rc=0 >>
99.2 567756 root python /usr/bin/goferd
192.236.1.55 | success | rc=0 >>
8.0 65506 root sshd: root@notty
192.236.1.57 | success | rc=0 >>
15.6 7260 root /opt/microsoft/configmgr/bin/ccmexec.binsort: write failed: standard output: Broken pipe
192.236.1.58 | success | rc=0 >>
100 491426 root python /usr/bin/goferdsort: fflush failed: standard output: Broken
192.236.1.59 | success | rc=0 >>
100 485478 root python /usr/bin/goferd
192.236.1.61 | success | rc=0 >>
100 463591 root python /usr/bin/goferdsort: fflush failed: standard output: Broken pipe
192.236.1.60 | success | rc=0 >>
20.8 7263 root /opt/microsoft/configmgr/bin/ccmexec.binsort: fflush failed:
manage services
# ansible hadoop_prod -m service -a “name=goferd state=restarted” -k
SSH password:
192.236.1.56 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
192.236.1.52 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
192.236.1.53 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
192.236.1.54 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
192.236.1.57 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
192.236.1.58 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
192.236.1.60 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
192.236.1.61 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
192.236.1.59 | success >> {
“changed”: true,
“name”: “goferd”,
“state”: “started”
}
gathering facts
[ovi ~]$ ansible last_bpm -a “free -m” –ask-pass
SSH password:
192.168.18.207 | success | rc=0 >>
total used free shared buffers cached
Mem: 15951 3823 12128 0 237 1199
-/+ buffers/cache: 2385 13565
Swap: 16383 0 16383
192.168.18.208 | success | rc=0 >>
total used free shared buffers cached
Mem: 15951 2540 13410 0 214 261
-/+ buffers/cache: 2063 13887
Swap: 16383 0 16383
192.168.18.206 | success | rc=0 >>
total used free shared buffers cached
Mem: 15951 1245 14706 0 168 216
-/+ buffers/cache: 860 15090
Swap: 16383 0 16383
[ovi ~]$ ansible hadoop_prod -m file -a “dest=/tmp/testansible mode=644 owner=ovi group=130 state=directory” –ask-pass
192.236.1.60 | success >> {
“changed”: true,
“gid”: 130,
“group”: “sysadmin”,
“mode”: “0644”,
“owner”: “ovi”,
“path”: “/tmp/testansible”,
“size”: 4096,
“state”: “directory”,
“uid”: 275
}
192.236.1.61 | success >> {
“changed”: true,
“gid”: 130,
“group”: “sysadmin”,
“mode”: “0644”,
“owner”: “ovi”,
“path”: “/tmp/testansible”,
“size”: 4096,
“state”: “directory”,
“uid”: 275
}
$ ansible all -m setup
[ovi@C ~]$ ansible endur_dev -a “sudo yum list openssh” –ask-pass
SSH password:
dora01.uat.my.com | success | rc=0 >>
Loaded plugins: package_upload, product-id, security, subscription-manager
Available Packages
openssh.x86_64 5.3p1-112.el6_7 rhel-6-server-rpms
dora02.dev.my.com | success | rc=0 >>
Loaded plugins: package_upload, product-id, security, subscription-manager
Available Packages
openssh.x86_64 5.3p1-112.el6_7 rhel-6-server-rpms
copy a file to server
[ovi@ ~]$ ansible endur_dev -m copy -a “src=/etc/hosts dest=/tmp/hosts” –ask-pass
SSH password:
dora01.dev.my.com | success >> {
“changed”: true,
“dest”: “/tmp/hosts”,
“gid”: 130,
“group”: “sysadmin”,
“md5sum”: “bf17964d25f8802d53ee22d97edb8d4e”,
“mode”: “0644”,
“owner”: “oasimin”,
“size”: 132,
“src”: “/tmp/ansible-1456345034.31-198250330572361/source”,
“state”: “file”,
“uid”: 275
}
dora02.uat.my.com | success >> {
“changed”: true,
“dest”: “/tmp/hosts”,
“gid”: 130,
“group”: “sysadmin”,
“md5sum”: “bf17964d25f8802d53ee22d97edb8d4e”,
“mode”: “0644”,
“owner”: “oasimin”,
“size”: 132,
“src”: “/tmp/ansible-1456345034.3-35469159385891/source”,
“state”: “file”,
“uid”: 275
}
Command to run ansible playbook
|
1
|
ansible-playbook ovi.yml |
where ‘ovi.yml’ is playbook name
Dry Run mode
|
1
|
ansible-playbook ovi.yml --check |