Configurations

Ansile config

ansible.cfg
[defaults]
inventory = hosts
remote_user = vagrant
# bypass ssh key checking
host_key_checking = False
roles_path = ./roles
nocows = 1

Inventory

inventory
[app]
192.168.60.4
192.168.60.5
# Database server
[db]
192.168.60.6
# Group 'multi' with all servers
[multi:children]
app
db
# Variables that will be applied to all servers
[multi:vars]
ansible_ssh_user=vagrant
ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key

Include roles

---
- hosts: kube
become: true
vars_files:
- vars/main.yml
pre_tasks:
- name: Ensure k8s module dependencies are installed.
pip:
name: openshift
state: present
- include_tasks: vagrant-setup.yml
roles:
- name: geerlingguy.security
- name: geerlingguy.swap
- name: geerlingguy.docker
- name: geerlingguy.kubernetes

changed_when and register

---
- hosts: localhost
gather_facts: false
tasks:
- name: Get the current date.
command: date
# register s the returned value (and some other metadata) into a newvariable current_date
register: current_date
# we know running date will never change the state of the host it’s run on, we also add changed_when: false
changed_when: false
- name: Print the current date.
debug:
msg: "{{ current_date.stdout }}"

Build docker image

  • Build docker image
  • Run container
  • Verify if container is running by using uri module
  • Post tasks to push image to container registry
---
- hosts: localhost
gather_facts: false
vars:
ansible_python_interpreter: "{{ ansible_playbook_python }}"
image_name: hello-go
image_tag: latest
tasks:
- name: Build the hello go image
docker_image:
build:
path: .
# not attempt to pull a newer
# version of the base image defined in the Dockerfile’s FROM line. Setting pull: false
# makes Ansible behave just like the Docker CLI
pull: false
name: "{{image_name }}"
tag: "{{image_tag}}"
source: build
- name: Run the Hello Go image.
docker_container:
name: hello-go
image: "{{ image_name }}:{{ image_tag }}"
state: started
published_ports:
- 8180:8180
- name: Verify Hello Go is responding.
uri:
url: http://localhost:8180/test
return_content: true
register: hello_go_response
failed_when: "'/test' not in hello_go_response.content"
post_tasks:
- name: Log into Docker registry.
docker_login:
registry: "{{ registry_url }}"
username: "{{ registry_username }}"
password: "{{ registry_password }}"
when:
- registry_url is defined and registry_url != ''
- registry_username is defined and registry_username != ''
- registry_password is defined and registry_password != ''
- name: Push Hello Go image to Docker registry.
docker_image:
name: "{{ image_name }}"
tag: "{{ image_tag }}"
repository: "{{registry_url}}/{{ image_name }}:{{ image_tag }}"
push: true
# source: local . Setting local tells
# Ansible the image must already exist locally, and Ansible should not attempt to
# pull or build the image.
source: local

Deploy kubernetes object with Ansible

  • Use prestask to check minikube status
---
- hosts: localhost
gather_facts: false
vars:
ansible_python_interpreter: "{{ ansible_playbook_python }}"
image_name: hello-go
replicas: 1
pre_tasks:
- name: Check minikube- status
command: minikube status
register: minikube_status
changed_when: false
ignore_errors: true
- name: Start Minikube if it's not running.
command: minikube start
when: "not minikube_status.stdout or 'Running' not in minikube_st\
atus.stdout"
tasks:
# Build the hello-go Docker image inside Minikube's environment.
- name: Get existing image hash.
# use the vertical pipe ( | ) to indicate to the YAML parser
# it should store the following lines as a “multi-line scalar”. Basically, the following
# content will be the equivalent of a shell script, with each line being its own
# command.
shell: |
eval $(minikube docker-env)
docker images -q {{ image_name }}
register: image_hash
changed_when: false
- name: Build image if it's not already built.
shell: |
eval $(minikube docker-env)
docker build -t {{ image_name }} ../hello-go
when: not image_hash.stdout
- name: Create a Deployment for Hello Go.
k8s:
state: present
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-go
namespace: default
spec:
replicas: "{{ replicas }}"
selector:
matchLabels:
app: hello-go
template:
metadata:
labels:
app: hello-go
spec:
containers:
- name: hello-go
image: "{{ image_name }}"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8180
- name: Create a Service for Hello Go
k8s:
state: present
definition:
apiVersion: v1
kind: Service
metadata:
name: hello-go
namespace: default
spec:
type: LoadBalancer
ports:
- port: 8180
targetPort: 8180
selector:
app: hello-go
post_tasks:
- name: Expose Hello Go on the host via Minikube.
command: minikube service hello-go --url=true
changed_when: false
register: minikube_service
- debug:
msg: "Hello Go URL: {{ minikube_service['stdout_lines'][0] }}"

Build docker image without writing Dockerfile

  • Use add_host module which is useful when you are building infrastructure dynamically
  • Use delegate to delegate task to another host
  • Docker commit to create container
---
- hosts: localhost
gather_facts: false
vars_files:
- vars/main.yml
pre_tasks:
- name: Create and start the build container.
docker_container:
image: debian:buster
name: "{{ container_name }}"
command: sleep infinity
- name: Add the new container to the inventory.
# add_host module is useful when you are building infrastructure dynamically,
add_host:
hostname: "{{ container_name }}"
ansible_connection: docker
ansible_python_interpreter: /usr/bin/python3
- name: Ensure Python is installed.
raw: >
apt-get update &&
apt-get install -y --no-install-recommends python3
delegate_to: "{{ container_name }}"
- name: Gather facts inside the container.
# equivalent of gather_facts: true
setup:
delegate_to: "{{ container_name }}"
- name: Ensure ps command is present for Solr's installer.
apt:
name: procps
state: present
delegate_to: "{{ container_name }}"
roles:
- name: geerlingguy.java
delegate_to: "{{ container_name }}"
- name: geerlingguy.solr
delegate_to: "{{ container_name }}"
post_tasks:
- name: Clean up the container.
# instead of converting newlines to spaces, as with the >
#operator, this time the YAML parser will preserve newlines with |
shell: |
apt-get remove --purge -y python3
rm -rf /var/lib/apt/lists/*
delegate_to: "{{ container_name }}"
args:
warn: no
- name: Commit the container.
command: >
docker commit
-c 'CMD ["/opt/solr/bin/solr", "start", "-f", "-force"]'
-c 'WORKDIR /var/solr'
{{ container_name }} ansible-for-kubernetes/solr:{{ solr_version }}
- name: Remove the container.
docker_container:
name: "{{ container_name }}"
state: absent
Last updated on