==================== Trying some commands used ansible =================
1. check the uptime of the managed nodes
2. Check the OS release of the managed node
3. Install and check a package name 'telnet'
4. create a user name 'Prabhu' with uid=1234 with /bin/bash shell
5. create a file /var/tmp/prabhu with 777
6. start nfs services
7. Create one script like shown & run the same on Managed node
#!/bin/sh
useradd test
touch /tmp/abc
mkdir /tmp/abcd
echo "Hello world"
-----------------------------------------------------------------------
[root@lansibletr-t4 ~]# ansible lansibletr-c4 -a 'uptime'
lansibletr-c4 | SUCCESS | rc=0 >>
11:24:42 up 3:12, 3 users, load average: 0.04, 0.05, 0.04
[root@lansibletr-t4 ~]# ansible lansibletr-c4 -a 'cat /etc/redhat-release'
lansibletr-c4 | SUCCESS | rc=0 >>
CentOS Linux release 7.4.1708 (Core)
[root@lansibletr-t4 ~]# ansible lansibletr-c4 -m package -a "name=wget
state=present"
lansibletr-c4 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached
hostfile\n * base: centos.mirror.fr.planethoster.net\n * extras:
centos.quelquesmots.fr\n * updates: centos.mirror.fr.planethoster.net\nResolving
Dependencies\n--> Running transaction check\n---> Package wget.x86_64 0:1.14-
15.el7_4.1 will be installed\n--> Finished Dependency Resolution\n\nDependencies
Resolved\n\n=======================================================================
=========\n Package Arch Version Repository
Size\n=============================================================================
===\nInstalling:\n wget x86_64 1.14-15.el7_4.1 updates
547 k\n\nTransaction
Summary\n==========================================================================
======\nInstall 1 Package\n\nTotal download size: 547 k\nInstalled size: 2.0
M\nDownloading packages:\nRunning transaction check\nRunning transaction
test\nTransaction test succeeded\nRunning transaction\n Installing : wget-1.14-
15.el7_4.1.x86_64 1/1 \n Verifying : wget-1.14-
15.el7_4.1.x86_64 1/1 \n\nInstalled:\n
wget.x86_64 0:1.14-15.el7_4.1
\n\nComplete!\n"
]
}
[root@lansibletr-t4 ~]#
[root@lansibletr-t4 ~]# ansible lansibletr-c4 -m user -a "name=prabhu uid=1234
shell=/bin/bash"
lansibletr-c4 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 1234,
"home": "/home/prabhu",
"name": "prabhu",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1234
}
[root@lansibletr-t4 ~]# ansible lansibletr-c4 -m file -a "path=/var/tmp/prabhu
mode=777 state=touch"
lansibletr-c4 | SUCCESS => {
"changed": true,
"dest": "/var/tmp/prabhu",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
[root@lansibletr-t4 ~]# ansible lansibletr-c4 -m service -a "name=nfs state=started
enabled=yes"
lansibletr-c4 | SUCCESS => {
"changed": true,
"name": "nfs",
"state": "started",
"status": {
[root@lansibletr-t4 ~]# ansible lansibletr-c4 -m service -a "name=nfs
state=stopped"
lansibletr-c4 | SUCCESS => {
"changed": true,
"name": "nfs",
"state": "stopped",
"status": {
[root@lansibletr-t4 ~]# vi /tmp/script.sh
[root@lansibletr-t4 ~]#
[root@lansibletr-t4 ~]# ansible lansibletr-c4 -m script -a "/tmp/script.sh"
lansibletr-c4 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to lansibletr-c4 closed.\r\n",
"stdout": "Hello Ansible\r\n",
"stdout_lines": [
"Hello Ansible"
]
}
[root@lansibletr-t4 ~]#
=================================
[root@lansibletr-t4 ansible]# cat firstplay.yml
---
- name: This is my first Ansible Playbook
hosts: 10.237.4.125
tasks:
- name: This is a task for useradd
user: name=prabhu uid=949 shell=/bin/bash state=present
- name: This is a task for create dir
file: path=/var/tmp/prabhu_doc mode=0777 state=directory
...
[root@lansibletr-t4 ansible]#
[root@lansibletr-t4 ansible]# ansible-playbook --syntax-check firstplay.yml
---> check syntax error
playbook: firstplay.yml
[root@lansibletr-t4 ansible]#
[root@lansibletr-t4 ansible]# ansible-playbook firstplay.yml --check
PLAY [This is my first Ansible Playbook]
***********************************************************************************
*************************
TASK [Gathering Facts]
***********************************************************************************
*******************************************
ok: [10.237.4.125]
TASK [This is a task for useradd]
***********************************************************************************
********************************
ok: [10.237.4.125]
TASK [This is a task for create dir]
***********************************************************************************
*****************************
ok: [10.237.4.125]
PLAY RECAP
***********************************************************************************
*******************************************************
10.237.4.125 : ok=3 changed=0 unreachable=0 failed=0
[root@lansibletr-t4 ansible]#
[root@lansibletr-t4 ansible]# ansible-playbook firstplay.yml -----> Run the
playbook
===================================================================================
==
==================== NTP setup ===================================
[root@lansibletr-t4 ansible]# cat ntp-setup.yml
---
- name: This is my first Ansible Playbook
hosts: 10.237.4.125
tasks:
- name: This is a task for Install ntp package
package: name=ntp state=present
- name: Configure the NTP configuration file
file: path=/etc/ntp.conf state=file
- name: Start the ntp service
service: name=ntpd state=started enabled=yes
...
[root@lansibletr-t4 ansible]#
==============LAB Taksk
===================================================================
1- A) Create a playbook for user (prabhu) and group (india) creation
B) Add India as a secondary group for yogesh user
2- Create a directory /var/tmp/demo, create file with name demotest inside the
directory created, copy /var/log/messages on managed node
3- Stop NTPD and Install NTP again ,remove the all the contents from ntp.conf file
and only below lines
server0.rhel.pool.org srv0
server1.rhel.pool.org srv1
-----------------
---
- name: This is my first Ansible Playbook
hosts: 10.237.4.125
tasks:
-name: create group and user
group: name=India state=present
user: name=prabhu1 uid=2345 shell=/bin/bash groups=India
---
- name: This is my first Ansible Playbook
hosts: 10.237.4.125
tasks:
- name: create directory
file: path=/var/tmp/demo state=directory
- name: create file
file: name=demotest dest=/var/tmp/demo/ state=touch
- name: copy file from message to demotest file
copy: src=/var/log/messages dest=/var/tmp/demo/demotest
---
- name: This is my first Ansible Playbook
hosts: 10.237.4.125
tasks:
- name: This is a task for stop and remove ntp package
service: name=ntpd state=stopped enabled=no
package: name=ntp state=absent
- name: This is a task for Install ntp package
package: name=ntp state=present
- name: Configure the NTP configuration file
file: path=/etc/ntp.conf state=file
- name : backup the file
copy: dest=/etc/ntp.conf backup=yes
- name: replace the content of the ntp.conf file
copy:
content: |
server0.rhel.pool.org srv0
server1.rhel.pool.org srv1
dest=/etc/ntp.conf1
- name: Start the ntp service
service: name=ntpd state=restarted enabled=yes
==========================================
===========Handler for contional operation ==============
[root@lansibletr-t4 ansible]# cat httpd_handler.yml
---
- name: This is my first Ansible Playbook
hosts: 10.237.4.125
tasks:
- name: This is a task for Install httpd package
package: name=httpd state=present
- name: copy htppd configuration file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf mode=755
notify:
restart httpd
- name: restart the httpd service
service: name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
[root@lansibletr-t4 ansible]#
======================================
==================Debug -- will get log message while run the playbook
===============
[root@lansibletr-t4 ansible]# cat httpd_debug.yml
---
- name: This is my first Ansible Playbook
hosts: 10.237.4.125
tasks:
- name: This is a task for Install httpd package
package: name=httpd state=present
- name: copy htppd configuration file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf mode=755
notify:
restart httpd
- name: restart the httpd service
service: name=httpd state=started
register: outputone
- name: debung the output for httpd
debug: var=outputone
handlers:
- name: restart httpd
service: name=httpd state=restarted
[root@lansibletr-t4 ansible]#
=================================================================================
---
# Creating a demo for variables
- name: variable demo
hosts:
vars:
user1:
========================================
1. Install "telnet' & pass the package as variable
2. create /etc/motd , it should contain "Hello All"
3. use facts in motd and fins Memory, hostname ,IP and OS module, and OS kernel?
---
# Creating a demo for variables
- name: variable demo
hosts: 10.237.4.125
vars:
var_package: telnet
task:
- name: Install telnet package
package: name="{{var_package}}" state=present
-----------------------------------
---
# Creating a demo for variables
- name: variable demo
hosts: 10.237.4.125
vars:
msg: Hello everyone,welcome all
tasks:
- name: Copy the content to the motd text file
copy:
content: |
"{{msg}}"
"{{ansible_memfree_mb}}"
dest: /etc/motd
--------------collect ansible_* from #ansible localhost -m setup > setup.log
---------------------
---
#Creating a demo for variables
- name: variable demo
hosts: 10.237.4.125
vars:
msg: Hello everyone,welcome all
tasks:
- name: Copy the content to the motd text file
copy:
content: |
"{{msg}} {{ansible_memfree_mb}} {{ansible_hostname}}
{{ansible_all_ipv4_addresses}} {{ansible_distribution_file_variety}}
{{ansible_distribution_version}} {{ansible_kernel}}"
dest: /etc/motd
===================================================================================
==
1. Single role
2. Multiple role wirh defined playbook
3. Role with internal dependencies
4. Specific naming
-- create a role -> NTP
-- tasks
-- pkg.yml
--
---------------
var_pakg: ntp
var_serv: ntpd
var_file: /etc/ntpd.conf
=================================