Aap2 2
Aap2 2
Aap2 2
EX294
Duration: 4Hrs
Instructions:
* All node root password is 'redhat' and Ansible control node user name is student.
* Create a directory 'ansible' under the path /home/student and all the playbook
should be under /home/student/ansible.
* All playbook should be owned/executed by student and Ansible managed node user
name is devops.
Note: In Exam, If they not given the Managed node user use the control node user as
remote user
___________________________________________________________________________________
________________________
# ssh student@workstation
# sudo yum install ansible-navigator ansible tree vim -y (In Exam it will work)
# vim /home/student/.vimrc
set ai ts=2 et cursorcolumn
# source /home/student/.vimrc
# mkdir /home/student/ansible
# cd /home/student/ansible
# vim /home/student/ansible/inventory
[dev]
servera.lab.example.com
[test]
serverb.lab.example.com
[prod]
serverc.lab.example.com
[balancers]
serverd.lab.example.com
[webservers:children]
prod
# vim /home/student/ansible/ansible.cfg
[defaults]
remote_user=devops
inventory=/home/student/ansible/inventory
roles_path=/home/student/ansible/roles
collections_paths=/home/student/ansible/collections
[privilege_escalation]
become=true
i) Name = baseos
Description = Baseos Description
Url = http://content/rhel9.0/x86_64/dvd/BaseOS
GPG is enabled.
Gpgkey = http://content.example.com/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-
release
Repository is enabled.
# vim /home/student/ansible/adhoc.yml
---
- name: Creating yum repository
hosts: all
tasks:
- name: Create BaseOS Repository
ansible.builtin.yum_repository:
name: "baseos"
description: "Baseos Description"
baseurl: http://content/rhel9.0/x86_64/dvd/BaseOS
gpgcheck: yes
gpgkey: http://content.example.com/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-
release
enabled: yes
- name: Create Appstream Repository
ansible.builtin.yum_repository:
name: "appstream"
description: "App Description"
baseurl: http://content/rhel9.0/x86_64/dvd/AppStream
gpgcheck: yes
gpgkey: http://content.example.com/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-
release
enabled: yes
# mkdir /home/student/ansible/collections
# ls collections/ansible_collections (verify)
# mkdir /home/student/ansible/roles
# vim /home/student/ansible/roles/requirements.yml
---
- src: http://content.example.com/Rhce/balancer.tar.gz
name: balancer
- src: http://content.example.com/Rhce/phpinfo.tar.gz
name: phpinfo
i) Install httpd package and the service should be start and enable the httpd
service.
ii) Host the web page using the template.j2
iii) The template.j2 should contain
My host is HOSTNAME on IPADDRESS
Where HOSTNAME is fully qualified domain name.
iv) Create a playbook named apache_role.yml and run the role in dev group.
# vim /home/student/ansible/roles/apache/templates/template.j2
My host is {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}
(or)
My host is {{ ansible_facts['fqdn'] }} on {{ ansible_facts['default_ipv4']
['address'] }}
# vim /home/student/ansible/roles/apache/tasks/main.yml
- name: Install httpd package
ansible.builtin.dnf:
name:
- httpd
- firewalld
state: present
- name: start service httpd
ansible.builtin.service:
name: httpd
state: started
enabled: yes
- name: start service firewalld
ansible.builtin.service:
name: firewalld
state: started
enabled: yes
- name: Add http service in firewall rule
ansible.posix.firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
- name: Copy the template.j2 file to web server directory
ansible.builtin.template:
src: template.j2
dest: /var/www/html/index.html
# vim /home/student/ansible/apache_role.yml
---
- name: apache deploy
hosts: dev
roles:
- apache
# ansible-navigator run apache_role.yml -m stdout
2) The playbook contains the webservers host group for using the role phpinfo
a) browsing, the webserver host group name that provides the output
"Welcome to serverc.lab.example.com, (version 1.0)"
and the output comes with various php contents
b) For example, the webserver hostgroup http://serverc.lab.example.com That
provides the output
"Welcome to serverc.lab.example.com, (version 1.0)"
and the output comes with various php contents
c) Similarly, the webserver hostgroup http://servera.lab.example.com that
provides the output
"My host is servera.lab.example.com on 172.25.250.10
# vim roles.yml
---
- name: Run the phpinfo first
hosts: webservers
roles:
- phpinfo
- name: Run the balancer
hosts: balancers
roles:
- balancer
# cp -rvf
/home/student/ansible/collections/ansible_collections/redhat/rhel_system_roles/
roles/* /home/student/roles/
# vim timesync.yml
---
- name: Using the timesync roles
hosts: all
vars:
timesync_ntp_servers:
- hostname: 172.25.254.254
iburst: yes
roles:
- timesync
# cp -rvf
/home/student/ansible/collections/ansible_collections/redhat/rhel_system_roles/
roles/* /home/student/roles/
# vim selinux.yml
---
- name: Configure selinux as enforcing mode
hosts: all
vars:
- selinux_state: enforcing
roles:
- selinux
# vim packages.yml
---
- name: package installation
hosts: dev,test
tasks:
- name: installing php and mariadb-server
ansible.builtin.dnf:
name:
- vsftpd
- mariadb-server
state: present
- name: group package installation
hosts: prod
tasks:
- name: installing group package 'Development tools'
ansible.builtin.dnf:
name: '@RPM Development Tools' #(in exam @RPM Development Tools)
state: present
- name: update packages
hosts: dev
tasks:
- name: updating all
ansible.builtin.dnf:
name: '*'
state: latest
# ansible dev -m command -a 'yum list installed |grep vsftpd' #(Verify the
output)
# vim /home/student/ansible/webcontent.yml
---
- name: create a link
hosts: dev
tasks:
- name: create a directory
ansible.builtin.file:
path: /devweb
state: directory
group: devops
mode: '02775'
setype: httpd_sys_content_t
- name: create a file
ansible.builtin.file:
path: /devweb/index.html
state: touch
- name: copy the contents to index.html
ansible.builtin.copy:
content: "Development\n"
dest: /devweb/index.html
- name: link the directory
ansible.builtin.file:
src: /devweb
dest: /var/www/html/devweb
state: link
___________________________________________________________________________________
_______________________________________
# vim /home/student/ansible/hwreport.yml
---
- name: hwreport
hosts: all
ignore_errors: yes
tasks:
- name: Download the file
ansible.builtin.get_url:
url: "http://content.example.com/Rhce/hwreport.txt"
dest: /root/hwreport.txt
- name: Collect report 1
ansible.builtin.set_fact:
HOSTNAME: "{{ ansible_hostname }}"
MEMORY: "{{ ansible_memtotal_mb }}"
BIOS: "{{ ansible_bios_version }}"
CPU: "{{ ansible_processor }}"
DISK_SIZE_VDA: "{{ ansible_devices['vda']['size'] }}"
- name: Collect report 2
ansible.builtin.set_fact:
DISK_SIZE_VDB: "{{ ansible_devices['vdb']['size'] }}"
- name: Copy the content to the managed node
ansible.builtin.copy:
content: |
#hwreport
HOSTNAME={{ HOSTNAME | default('NONE') }}
MEMORY={{ MEMORY | default('NONE') }}
BIOS={{ BIOS | default('NONE') }}
CPU={{ CPU | default('NONE') }}
DISK_SIZE_VDA={{ DISK_SIZE_VDA | default('NONE') }}
DISK_SIZE_VDB={{ DISK_SIZE_VDB | default('NONE') }}
dest: /root/hwreport.txt
# ansible-navigator run hwreport.yml -m stdout
# vim /home/student/ansible/issue.yml
---
- name: play for replace module
hosts: all
tasks:
- name: replace the content in dev group
ansible.builtin.copy:
content: Development
dest: /etc/issue
when: inventory_hostname in groups['dev']
- name: replace the content in test group
ansible.builtin.copy:
content: Test
dest: /etc/issue
when: inventory_hostname in groups['test']
- name: replace the content in prod group
ansible.builtin.copy:
content: Production
dest: /etc/issue
when: inventory_hostname in groups['prod']
ii) The file should collect all node information like ipaddress,fqdn,hostname
and it should be the same as in the /etc/hosts file,
if playbook run in all the managed node it must store in /etc/myhosts.
# vim /home/student/ansible/myhosts.j2
last line:
{% for host in groups['all'] %}
{{hostvars[host] ['ansible_facts'] ['default_ipv4'] ['address']}} {{hostvars[host]
['ansible_facts'] ['fqdn']}} {{hostvars[host] ['ansible_facts'] ['hostname']}}
{% endfor %}
# vim hosts.yml
---
- name: Collect the all node information
hosts: all
tasks:
- name: copy the template to the managed node
ansible.builtin.template:
src: myhosts.j2
dest: /etc/myhosts
when: inventory_hostname in groups['dev']
13. Create a variable file vault.yml and that file should contains the variable and
its value.
# vim secret.txt
P@sswOrd
pw_developer: lamdev
pw_manager: lammgr
# wget http://content.example.com/Rhce/user_list.yml
# vim users.yml
---
- name: Create an users and groups
hosts: all
vars_files:
- user_list.yml
- vault.yml
tasks:
- name: Create group 1
ansible.builtin.group:
name: opsdev
state: present
when: inventory_hostname in groups['dev'] or inventory_hostname in
groups['test']
- name: Create group 2
ansible.builtin.group:
name: opsmgr
state: present
when: inventory_hostname in groups['test']
- name: User create 1
ansible.builtin.user:
name: "{{ item.name }}"
uid: "{{ item.uid }}"
password: "{{ pw_developer | password_hash('sha512') }}"
password_expire_max: "{{ item.password_expire_days }}"
groups: opsdev
state: present
loop:
"{{ users }}"
when: item.job == "developer" and (inventory_hostname in groups['dev'] or
inventory_hostname in groups['test'])
- name: user create 2
ansible.builtin.user:
name: "{{ item.name }}"
uid: "{{ item.uid }}"
password: "{{ pw_manager | password_hash('sha512') }}"
password_expire_max: "{{ item.password_expire_days }}"
groups: opsmgr
state: present
loop:
"{{ users }}"
when: item.job == "manager" and inventory_hostname in groups['test']
16. Create a cronjob for user student in all nodes, the playbook name crontab.yml
and the job details are below
i) Every 2 minutes the job will execute logger "EX294 in progress"
# vim crontab.yml
---
- name : Create a cronjob
hosts: all
tasks:
- name: Cronjob for logger
ansible.builtin.cron:
name: Create logger
user: student
minute: "*/2"
job: logger "EX294 in progress"
state: present
17. Create a logical volume named data of 1500M size from the volume group research
and if 1500M size is not created, then atleast it should create 800M size.
i) Verify if vg not exist, then it should debug msg "vg not found" .
ii) 1500M lv size is not created, then it should debug msg "Insufficient size of
vg" .
iii) If Logical volume is created, then assign file system as "ext3" .
iv) Do not perform any mounting for this LV.
iv) The playbook name lvm.yml and run the playbook in all nodes.
___________________________________________________________________________________
______________________
# wget http://content/Rhce/initialscripts.sh
# chmod +x initialscripts.sh
# sh initialscripts.sh
___________________________________________________________________________________
_______________________