Tasks

As we are targeting two different operating systems, our tasks/main.yml file needs to look as follows:

---
# tasks file for ansible-role-docker

- name: include the operating system specific variables
include_vars: "{{ ansible_os_family }}.yml"

- name: install the stack on centos
import_tasks: install-redhat.yml
when: ansible_os_family == 'RedHat'

- name: install the stack on ubuntu
import_tasks: install-ubuntu.yml
when: ansible_os_family == 'Debian'

As you can see, this is the same as when we installed our LEMP Stack on the two operating systems in Chapter 6, Targeting Multiple Distributions. The tasks/install-redhat.yml file looks pretty much like the tasks we have used to install Docker in the previous chapters:

---
# tasks file for ansible-role-docker

- name: add the gpg key for the docker repo
rpm_key:
key: "{{ docker.gpg_key }}"
state: "present"

- name: add docker repo from the remote url
get_url:
url: "{{ docker.repo_url }}"
dest: "{{ docker.repo_path }}"
mode: "0644"

- name: install the docker packages
yum:
name: "{{ item }}"
state: "installed"
update_cache: "yes"
enablerepo: "{{ docker.edge }}"
with_items: "{{ docker.packages }}"

- name: install pip
easy_install:
name: pip
state: latest

- name: install the python packages
pip:
name: "{{ item }}"
with_items: "{{ docker.pip }}"

- name: put selinux into permissive mode
selinux:
policy: targeted
state: permissive

- name: start docker and configure to start on boot
service:
name: "docker"
state: "started"
enabled: "yes"

The only difference is that we are enabling the Docker CE Edge repository when installing the packages, and also we are not running a yum update when installing Docker. We are not doing this as it is not our role's decision to update a server when someone else is running the role; our role should only install Docker.

The final task file is tasks/install-ubuntu.yml. This, as you will have already guessed, contains the tasks to install Docker on Ubuntu hosts:

---
# tasks file for ansible-role-docker

- name: install the system packages
apt:
name: "{{ item }}"
state: "present"
update_cache: "yes"
with_items: "{{ docker.system_packages }}"

- name: add the apt keys from a key server
apt_key:
url: "{{ docker.gpg_key }}"
state: present

- name: add the apt repo
apt_repository:
repo: "{{ docker.repo }}"
state: present

- name: install the docker package
apt:
name: "{{ item }}"
state: "present"
update_cache: "yes"
force: "yes"
with_items: "{{ docker.packages }}"

- name: install the python packages
pip:
name: "{{ item }}"
with_items: "{{ docker.pip }}"

- name: start docker and configure to start on boot
service:
name: "docker"
state: "started"
enabled: "yes"

That concludes all of the tasks and variables that we need in order to install Docker on the two different operating systems. In previous chapters, that would have been enough for us to add the role to our playbook and run the tasks. However, as we are going to be publishing this role on Ansible Galaxy, we need to add some more information about the role.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.242.165