Ansible playbook lab exercise part-3

  1. Create a playbook where you should install a webserver into 2 OS type(Ubuntu and RHEL) with fact conditioning. and list out a directory which in “/etc” and display such as “This is a directory /file in /etc:-XXXx” To be used Conditiong, looping, regiter and fact variable.
---
- name: Install webserver on 2 os and display directory contents
  hosts: localhost
  
  tasks:
  - name: Install httpd on redhat os
    yum:
      name=httpd
      state=latest
    when: ansible_os_family == "RedHat"
  - name: Install apache2 on ubuntu
    apt:
      name=apache2
      state=latest
    when: ansible_os_family == "Ubuntu"
  - name: List contents in /etc
      shell: ls /etc
      register: contents
  - name: Print /etc contents
      debug:
        msg: "This is a directory /file in /etc : {{ item }}"
      loop: "{{ contents.stdout_lines }}"
2. There are 2 environment DEV and QA. Both has their own group_vars. Using it, in Dev and QA environment(RHEL/CENTOS) install apache and Dev should run with 80 port and QA should run with 8080 port. Whereas config file locaton for apache conf is /etc/httpd.conf/conf/httpd.conf and line for port is “Listen 80”. This should be done using template module and zinja2 lib.

---
- name: Install dev and qa environment
  hosts: web
  vars_files:
    - ext-vars.yaml
	
  tasks:
  - name: Install httpd
    yum:
      name=httpd
      state=latest
    when: ansible_os_family == "RedHat"
  - name: Install apache2
    apt:
      name=apache2
      state=latest
    when: ansible_os_family == "Ubuntu"  

  - name: Template a file to /etc/file.conf
    ansible.builtin.template:
      src: httpd.conf.j2
      dest: /etc/httpd/conf/httpd.conf
     notify:
      - Start service httpd, if not started

  - name: Start service httpd, if not started
    ansible.builtin.service:
      name=httpd
      state=started

  handlers:
  - name: Start service httpd, if not started
    ansible.builtin.service:
      name=httpd
      state=restarted