Install on Ubuntu
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible
Check install Ansible successful
After create ansible.cfg file, you can run
inventory.ini define hosts, groups you want to manage
my inventory.ini file
[ubuntu1]
192.168.1.127
[ubuntu2]
192.168.1.128
[dvp:vars]
ansible_user=dvp
ansible_ssh_pass=1
[dp:vars]
ansible_user=dp
ansible_ssh_pass=1
Then, you change ansible.cfg file
[defaults]
host_key_checking = False
inventory = /home/Documents/test-ansible/inventory.ini
remote_user = dvp
ping all machine you defined on your own inventory.ini file
---
- name: install docker
hosts: ubuntu1
gather_facts: false
tasks:
- name: ping
ping:
register: result
- name: install-docker
become: yes
apt:
name: docker.io
state: present
- name: install docker-compose
become: yes
apt:
name: docker-compose
state: present
- name: ensure docker service is running
become: yes
service:
name: docker
state: started
- name: deploy wordpress
hosts: ubuntu1
gather_facts: false
tasks:
- name: pull docker compose yaml
become: yes
get_url:
url: https://raw.githubusercontent.com/bitnami/bitnami-docker-wordpress/master/docker-compose.yml
dest: /home/dvp/docker-compose.yml
- name: run docker compose
become: yes
command: docker-compose up -d
- name: ensure docker-compose container is running
become: yes
docker_container_info:
name: my_container
register: result
and Let's see!
Practice 2: Using Ansible to set up docker on VMs and deploy Wordpress on Ubuntu1, MariaDB on Ubuntu2
ansible.cfg (same practice 1)
[defaults]
host_key_checking = False
remote_user = dvp
inventory.ini (define database host)
[ubuntu1]
192.168.1.127
[ubuntu2]
192.168.1.130
[ubuntu1:vars]
ansible_user=dvp
ansible_ssh_pass=1
[ubuntu2:vars]
ansible_user=phuc
ansible_ssh_pass=1
Create file install-docker-playbook.yaml
- name: set up docker
hosts: ubuntu1
gather_facts: false
tasks:
- name: Ping
ping:
register: result
- name: Install docker.io
become: yes
apt:
name: docker.io
state: present
- name: Ensure docker service is running
become: yes
service:
name: docker
state: started
Now, run command:
$ ansible-playbook -i inventory.ini install-docker-playbook.yaml
- name: deploy mariadb
hosts: ubuntu1
gather_facts: false
tasks:
- name: create volume for db
become: yes
command: docker volume create --name mariadb_data
- name: deploy mariadb
become: yes
command: docker run -d --name mariadb --env ALLOW_EMPTY_PASSWORD=yes --env MARIADB_USER=bn_wordpress --env MARIADB_PASSWORD=bitnami --env MARIADB_DATABASE=bitnami_wordpress --network host --volume mariadb_data:/bitnami/mariadb bitnami/mariadb:latest
register: result2
- name: setup data
debug:
var: result2
- name: ensure docker-compose container is running
become: yes
docker_container_info:
name: my_container
register: result
- name: checking result
debug:
var: result
$ ansible-playbook -i inventory.ini deploy-mairiadb-playbook.yaml
Create file deploy-wordpress-playbook.yaml
hosts: phuc
gather_facts: false
tasks:
- name: Ensure docker service is running
become: yes
service:
name: docker
state: started
- name: Create a volume for wordpress
become: yes
command: docker volume create --name wordpress_data
- name: Create the Wordpress container
become: yes
command: docker run -d --name wordpress -p 8080:8080 -p 8443:8443 --env ALLOW_EMPTY_PASSWORD=yes --env WORDPRESS_DATABASE_USER=bn_wordpress --env WORDPRESS_DATABASE_PASSWORD=bitnami --env WORDPRESS_DATABASE_NAME=bitnami_wordpress --network host --add-host mariadb:192.168.1.127 --volume wordpress_data:/bitnami/wordpress bitnami/wordpress:latest