Ansible Playbook for Big Project:
In the previous part we wrote a simple playbook to execute various task using yml, now we go on to write a playbook for the big projects. Such as NFS, SAMA & Apache basic as well virtual web hosting with help of playbook on RHEL 8 Server.
In following playbook we use handler, loop and conditions statement so go the small overview.
Handler:
Handlers are just like a regular tasks in an ansible playbook, but are only run if the task contains a notify directive and also indicate that it change something
eg.
–
name: test
hosts: pune
remote_user: root
tasks:
– name: install httpd package
yum:
name: httpd
state: present
notify: restart httpd
handlers:
– name: restart httpd
service:
name: httpd
state: restarted
:wq
For Free, Demo classes Call: 7798058777
Registration Link: Click Here!
Loop:
When we need to repeat and execute any task in multiple time, means we want to run task in looping as per computer programing concept. Here we creating multiple users with the user module, and repeating a polling step until certain result is reached.
–
name: loop
hosts: pune
remote_user: root
tasks:
– name: create users
user:
name: ‘{{ item }}’
state: present
with_items:
– suraj
– ajay
– akash
– sara
Conditions:
Whenever we have different different scenarios, we put conditions according to the scenario.
name: conditiontest
hosts: pune
remote_user: root
tasks:
– name: install apache on debian
command: apt-get -y install apache2
when: ansible_os_family == “Debian”
– name: install apache for redhat
command: yum install httpd -y
when: ansible_os_family == “RedHat”
1.Playbook for NFS Configuration:
–
name: nfspro
hosts: client
remote_user: root
tasks:
– name: Transfter repo file on all clients
copy:
src: /etc/yum.repos.d/server.repo
dest: /etc/yum.repos.d/
– name: Install nfs package
yum:
name: nfs*
state: present
– name: Start service nfs-server
service:
name: nfs-server
state: started
– name: Enable nfs-server
service:
name: nfs-server
enabled: yes
– name: create directory to share
file:
path: /india
state: directory
owner: root
group: root
mode: ‘0777’
– name: create file to share
file:
path: /india/notes.txt
state: touch
– name: Edit configuration file
lineinfile:
path: /etc/exports
line: /india 172.25.0.0/24(rw)
– name: add service into firewall
firewalld:
service: nfs
permanent: yes
state: enabled
– name: Reload firewall service
service:
name: firewalld
state: restarted
– name: Restart nfs service
service:
name: nfs-server
state: restarted
For Free, Demo classes Call: 7798058777
Registration Link: Click Here!
2.Playbook for Samba Configuration:
–
name: sambaconfig
hosts: pune
remote_user: root
vars:
package:
– samba
– samba-common
– samba-client
service:
– smb
– nmb
tasks:
– name: transfer repo
copy:
src: /etc/yum.repos.d/server.repo
dest: /etc/yum.repos.d/
– name: install package
yum:
name: ‘{{ package }}’
state: present
– name: start enable smb service
service:
name: ‘{{ service[0] }}’
state: started
enabled: yes
– name: start enable nmb service
service:
name: ‘{{ service[1] }}’
state: started
enabled: yes
– name: add service into firewall
firewalld:
service: samba
permanent: yes
state: enabled
– name: restart firewalld service
service:
name: firewalld
state: restarted
– name: create directory to share
file:
path: /sambadir
state: directory
owner: root
group: root
mode: ‘0755’
– name: create file to share
file:
path: /sambadir/note.txt
state: touch
– name: set selinux permission
community.general.sefcontext:
target: “/sambadir(/.*)?”
setype: samba_share_t
state: present
– name: restore selinux context
ansible.builtin.command: restorecon -rv /sambadir
– name: restore selinux context
ansible.builtin.command: restorecon -rv /sambadir/note.txt
– name: configure selinux file
blockinfile:
path: /etc/samba/smb.conf
block: |
[share]
comment=samba using ansible
path=/sambadir
browseable=yes
valid users=ajay
read only=yes
hosts allow=172.25.0.
– name: useradd ajay
user:
name: ajay
comment: sambauser
– name: create samba user password
shell: echo -e “123\n123” | smbpasswd -a -s ajay
– name: start enable smb service
service:
name: ‘{{ service[0] }}’
state: restarted
enabled: yes
– name: start enable nmb service
service:
name: ‘{{ service[1] }}’
state: restarted
enabled: yes
3.Playbook for Samba Configuration:
–
name: webhosting
hosts: pune
become: yes
vars:
pkg: httpd
srv: http
tasks:
– name: transfer repo file
copy:
src: /etc/yum.repos.d/server.repo
dest: /etc/yum.repos.d/
– name: install httpd package
yum:
name: ‘{{ pkg }}’
state: present
notify: enable httpd
– name: start httpd service
service:
name: ‘{{ pkg }}’
state: started
– name: create file for webpage
file:
path: /var/www/html/index.html
state: touch
owner: root
group: root
mode: ‘0644’
– name: write webpage code in index file
blockinfile:
path: /var/www/html/index.html
block: |
<html>
<head>
<title>Ansible</title>
</head>
<body bgcolor=skyblue>
<h1>Welcome to ansible automation website</h1>
</body>
</html>
– name: restart httpd service
service:
name: ‘{{ pkg }}’
state: restarted
– name: add service into firewall
firewalld:
service: ‘{{ srv }}’
immediate: true
permanent: true
state: enabled
– name: restart firewall service
service:
name: firewalld
state: restarted
– name: create second webpage
file:
path: /var/www/html/pune
state: directory
owner: root
group: root
mode: ‘0755’
– name: create file for second webpage
file:
path: /var/www/html/pune/index.html
state: touch
owner: root
group: root
mode: ‘0644’
– name: write webpage code in second index file
blockinfile:
path: /var/www/html/pune/index.html
block: |
<html>
<head>
<title>Ansible 2</title>
</head>
<body bgcolor=gray>
<h1>Welcome to ansible automation 2nd website</h1>
</body>
</html>
– name: restart httpd service
service:
name: ‘{{ pkg }}’
state: restarted
– name: create directory for virtual web hosting
file:
path: /usr/mumbai
state: directory
owner: root
group: root
mode: ‘0755’
– name: create file for virtual webpage
file:
path: /usr/mumbai/index.html
state: touch
owner: root
group: root
mode: ‘0644’
– name: write webpage code in second index file
blockinfile:
path: /usr/mumbai/index.html
block: |
<html>
<head>
<title>Virtual</title>
</head>
<body bgcolor=yellow>
<h1>Welcome to ansible virtual website</h1>
</body>
</html>
– name: transfer web configuration file for server
copy:
src: /etc/ansible/server.conf
dest: /etc/httpd/conf.d/
owner: root
group: root
mode: ‘0644’
– name: transfer web configuration file for mumbai
copy:
src: /etc/ansible/mumbai.conf
dest: /etc/httpd/conf.d/
owner: root
group: root
– name: add entry in host file for server
lineinfile:
path: /etc/hosts
line: 192.168.29.125 server.example.com
– name: add entry in host file for mumbai
lineinfile:
path: /etc/hosts
line: 192.168.29.125 mumbai.example.com
– name: create virtual web hosting for nagar website
file:
path: /nagar
state: directory
owner: root
group: root
mode: ‘0755’
– name: create file for virtual webpage
file:
path: /nagar/index.html
state: touch
owner: root
group: root
mode: ‘0644’
– name: write webpage code in second index file
blockinfile:
path: /nagar/index.html
block: |
<html>
<head>
<title>Virtual</title>
</head>
<body bgcolor=pink>
<h1>Welcome to ansible virtual website</h1>
</body>
</html>
– name: transfer web configuration file for nagar
copy:
src: /etc/ansible/nagar.conf
dest: /etc/httpd/conf.d/
owner: root
group: root
– name: add entry in host file for mumbai
lineinfile:
path: /etc/hosts
line: 192.168.29.125 nagar.example.com
– name: set selinux lable
community.general.sefcontext:
target: “/nagar(/.*)?”
setype: httpd_sys_content_t
state: present
– name: restor selinux label for nagar
ansible.builtin.command: restorecon -irv /nagar
– name: restor selinux label for index file
ansible.builtin.command: restorecon -irv /nagar/index.html
– name: restart httpd service
service:
name: ‘{{ pkg }}’
state: restarted
handlers:
– name: enable httpd
service:
name: ‘{{ pkg }}’
enabled: yes
For Free, Demo classes Call: 7798058777
Registration Link: Click Here!
Author:-
Abhijeet Dahatonde
| SevenMentor Pvt Ltd.
© Copyright 2021 | Sevenmentor Pvt Ltd.