Docker with Ansible
Task 10 Description:-
🔰 Write an Ansible Playbook that doEs the following operations in the managed nodes:-
- Configure Docker
- Start and enable Docker services
- Pull the httpd server image from the Docker Hub
- Run the httpd container and expose it to the public
- Copy the HTML code in /var/www/HTML directory and start the webserver.
Ansible Playbook that does the following operations in the managed nodes:-
- Configuring yum
- Creating a folder and mounting the cdrom
- creating repo dvd. repo and dvd1.repo
- name : task_1
gather_facts: No
hosts: server1
Task:
-name: create_dvd_folder
file:
path: /dvd
state: directory
mode:"0755"
- name: mount_dvd
mount:
path: /dvd/
src: /dev/cdrom
fstype: iso9660
opts: ro,loop
state: mounted
- name: yum_repo_BaseOS
yum_repository:
name: BaseOS
description: Local_baseos
file: dvd1
baseurl: file:///dvd/BaseOS
gpgcheck: no
- name: yum_repo_AppStream
yum_repository:
name: AppStream
description: Local_appstream
file: dvd2
baseurl: file:///dvd/AppStream
gpgcheck: no
Removing unwanted package pre-created if any:-
- name: Remove docker if installed from CentOS repo
yum:
name:
- docker
- docker-client
- docker-client-latest
- docker-common
- docker-latest
- docker-latest-logrotate
- docker-logrotate
- docker-engine
state: removed
Installing dependencies :-
- name: Install yum utils
yum:
name: yum-utils
state: latest
- name: Install device-mapper-persistent-data
yum:
name: device-mapper-persistent-data
state: latest
- name: Install lvm2
yum:
name: lvm2
state: latest
Configure docker-ce. repo and installing docker using the shell as well as using ansible:-
- name: Add Docker repo
get_url:
url: https://download.docker.com/linux/centos/docker-ce.repo
dest: /etc/yum.repos.d/docker-ce.repo
become: yes
- name: Enable Docker Edge repo
ini_file:
dest: /etc/yum.repos.d/docker-ce.repo
section: 'docker-ce-edge'
option: enabled
value: 0
become: yes
- name: Enable Docker Test repo
ini_file:
dest: /etc/yum.repos.d/docker-ce.repo
section: 'docker-ce-test'
option: enabled
value: 0
become: yes
- name: Install Docker
shell: "dnf install --nobest docker-ce -y"
Or
- name: Install Docker
package:
name: docker-ce
state: latest
become: yes
Starting docker service and installation of required packages
- name: Start Docker service
service:
name: docker
state: started
enabled: yes
become: yes
- pip3:
name: "docker-py"
executable: pip2.7
state: present
Or
- name: docker-py
shell: "pip3 install docker"
Pulling the docker image and creating the container for starting the services with mounting the data and exporting the port :-
- name: pull an image
docker_image:
name: httpd
source: pull
- name: creating a httpd container
docker_container:
name: myhttpd
image: httpd
state: started
published_ports: "6379:80"
volumes: "/var/www/html/:/usr/local/apache2/htdocs/:ro"
Comments
Post a Comment