This scenario shows:
- how to transfer file from control node to worker nodes.
- how to download file from internet and unzip that file.
- You should have a look following lab, nodes that are created in that LAB, are using in ansible commands
- Create html file in the control node.
- Create directory and web page.
mkdir files
nano files/default_site.html
<html>
<title>Ansible File Transfer Test</title>
<body>
<p>Ansible Great!</p>
</body>
</html>
- Update 'site.yml' file (implemented in LAB: Adding Tags)
---
- hosts: all
become: true
pre_tasks:
- name: install updates (CentOS)
tags: always
dnf:
update_only: yes
update_cache: yes
when: ansible_distribution == "CentOS"
- name: install updates (Ubuntu)
tags: always
apt:
upgrade: dist
update_cache: yes
when: ansible_distribution == "Ubuntu"
- hosts: web_servers
become: true
tasks:
- name: install apache and php (CentOS)
tags: centos,apache,httpd
dnf:
name:
- httpd
- php
state: latest
when: ansible_distribution == "CentOS"
- name: install apache and php (Ubuntu)
tags: ubuntu,apache,apache2
apt:
name:
- apache2
- libapache2-mod-php
state: latest
when: ansible_distribution == "Ubuntu"
- name: copy default (index) html file for site
tags: apache,apache2,httpd
copy:
src: default_site.html
dest: /var/www/html/index.html
owner: root
group: root
mode: 0644
- hosts: database_servers
become: true
tasks:
- name: install MariaDB (CentOS)
tags: centos,db,mariadb
dnf:
name: mariadb
state: latest
when: ansible_distribution == "CentOS"
- name: install MariaDB (Ubuntu)
tags: ubuntu,db,mariadb-server
apt:
name: mariadb-server
state: latest
when: ansible_distribution == "Ubuntu"
- Run following:
ansible-playbook --ask-become-pass site.yml
- File is transferred to node1:
- Update 'site.yml' file with adding unzip package (name: install unzip) and unarchive file (name: install terraform).
---
- hosts: all
become: true
pre_tasks:
- name: install updates (CentOS)
tags: always
dnf:
update_only: yes
update_cache: yes
when: ansible_distribution == "CentOS"
- name: install updates (Ubuntu)
tags: always
apt:
upgrade: dist
update_cache: yes
when: ansible_distribution == "Ubuntu"
- hosts: web_servers
become: true
tasks:
- name: install apache and php (CentOS)
tags: centos,apache,httpd
dnf:
name:
- httpd
- php
state: latest
when: ansible_distribution == "CentOS"
- name: install apache and php (Ubuntu)
tags: ubuntu,apache,apache2
apt:
name:
- apache2
- libapache2-mod-php
state: latest
when: ansible_distribution == "Ubuntu"
- name: copy default (index) html file for site
tags: apache,apache2,httpd
copy:
src: default_site.html
dest: /var/www/html/index.html
owner: root
group: root
mode: 0644
- name: install unzip
package:
name: unzip
- name: install terraform
unarchive:
src: https://releases.hashicorp.com/terraform/1.3.4/terraform_1.3.4_linux_amd64.zip
dest: /usr/local/bin
remote_src: yes
owner: root
group: root
mode: 0755
- hosts: database_servers
become: true
tasks:
- name: install MariaDB (CentOS)
tags: centos,db,mariadb
dnf:
name: mariadb
state: latest
when: ansible_distribution == "CentOS"
- name: install MariaDB (Ubuntu)
tags: ubuntu,db,mariadb-server
apt:
name: mariadb-server
state: latest
when: ansible_distribution == "Ubuntu"
- It installs the zip file from remote and unzips this terraform application into '/usr/local/bin'
ansible-playbook --ask-become-pass site.yml
- Installed on node1