int-srv

mkdir /etc/ansible
mkdir /opt/template

vi /etc/ansible/hosts

### vi ###
[web]
web01 ansible_host=10.1.20.31
web02 ansible_host=10.1.20.32
### vi ###

ansible all -m ping

### output ###
web02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
web01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
### output ###

vi /etc/ansible/ansible.cfg

### vi ###
[defaults]
timeout = 60
### vi ###

scp 10.1.20.31:/etc/apache2/sites-available/000-default.conf /opt/template/000-default.conf.j2
scp 10.1.20.31:/var/www/html/index.html /opt/template/index.html.j2

vi /opt/deploy.yml

### vi ###
---
- name: Deploy web service
  hosts: "{{ target }}"
  become: yes
  gather_facts: no
  
  tasks:
	  - name: Install Apache2
		  apt:
			  name: apache2
			  state: present
		
		- name: Deploy Apache virtual host config
			template: 
				src: /opt/template/000-default.conf.j2
				dest: /etc/apache2/sites-available/000-default.conf
			notify: Restart Apache
			
		- name: Deploy web content
			template:
				src: /opt/template/index.html.j2
				dest: /var/www/html/index.html
				owner: www-data
				group: www-data
				mode: '0644'
				
		- name: Create whoami file
			copy:
				dest: /var/www/html/whoami
				content: "{{ inventory_hostname }}\\n"
				owner: www-data
				group: www-data
				mode: '0644'
	
	handlers:
		- name: Restart Apache
			services:
				name: apache2
				state: restarted
### vi ###

vi /opt/rollback.yml

### vi ###
---
- name: Rollback web service
	hosts: "{{ target }}"
	become: yes
	
	tasks:
		- name: Stop and disable Apache2
			service:
				name: apache2
				state: stopped
				enabled: no
		
		- name: Remove Apache2 package
			apt:
				name: apache2
				state: absent
				purge: yes
		
		- name: Remove web directory
			file:
				path: /var/www
				state: absent
### vi ###