This site runs best with JavaScript enabled.

Ansible Series - P1


Ansible Series - P1

Ansible Series

When I joined to ORO, there are some Project that were apply the Ansbile to setup the server. After that I tried to use Ansible to improve the automation work like setup the server, deploy the application, etc. So I will write some post to share my experience with Ansible.

Today, when I am working on my HomeLab, The VM normally created and deleted. So I need to setup the VM quickly. So I will use Ansible to setup the VM.

Install Ansible on the Working Machine.

I install the Ansbile on 1 main Debian VM. In this machine I can do everything with Ansible.

1sudo apt-add-repository ppa:ansible/ansible
2
3sudo apt update
4
5sudo apt install ansible

Setup the inventory file

The inventory file is the file that contain the list of the server that we want to manage. In this file we can define the group of the server, the variable for each server, etc.

1[homeserver]
2svr01 ansible_host=192.168.1.253 ansible_user=khoa
3svr02 ansible_host=192.168.1.176 ansible_user=khoa
4svr03 ansible_host=192.168.1.155 ansible_user=khoa

svr01 is alias, ansible_user=khoa is host variable

I have 3 Ubuntu VM that I want to setup the Kubernetes cluster. So I will create the inventory file with the name inventory and put the content above to the file.

Then, Check the inventory is correct.

1ansible-inventory -i inventory --list

The output should be like this:

1{
2 "_meta": {
3 "hostvars": {
4 "svr01": {
5 "ansible_host": "192.168.1.253",
6 "ansible_user": "khoa"
7 },
8 "svr02": {
9 "ansible_host": "192.168.1.176",
10 "ansible_user": "khoa"
11 },
12 "svr03": {
13 "ansible_host": "192.168.1.155",
14 "ansible_user": "khoa"
15 }
16 }
17 },
18 "all": {
19 "children": ["ungrouped", "homeserver"]
20 },
21 "homeserver": {
22 "hosts": ["svr01", "svr02", "svr03"]
23 }
24}

We can have multiple group in the inventory file. For example, I want to create the group for the Kubernetes master and worker. So I will add the content below to the inventory file.

1[masters]
2master1 ansible_host=203.0.113.111
3
4
5[workers]
6worker1 ansible_host=203.0.113.113
7worker2 ansible_host=203.0.113.114
8
9[masters:vars]
10ansible_user=root
11
12[workers:vars]
13ansible_user=worker

Running the command to check the connection to the server.

1ansible all -i inventory -m ping

Add the Playbook

This is the simple playbook that will install the docker on the server and run the simple container

1- hosts: homeserver
2 become: true
3 vars:
4 container_count: 4
5 default_container_name: docker
6 default_container_image: ubuntu
7 default_container_command: sleep 1d
8 ansible_sudo_pass: "[root sudo password]" //temporary push the password on the file to test out the command
9 tasks:
10 - name: Install aptitude
11 apt:
12 name: aptitude
13 state: latest
14 update_cache: true
15
16 - name: Install required system packages
17 apt:
18 pkg:
19 - apt-transport-https
20 - ca-certificates
21 - curl
22 - software-properties-common
23 - python3-pip
24 - virtualenv
25 - python3-setuptools
26 state: latest
27 update_cache: true
28 - name: Add Docker GPG apt Key
29 apt_key:
30 url: https://download.docker.com/linux/ubuntu/gpg
31 state: present
32
33 - name: Add Docker Repository
34 apt_repository:
35 repo: deb https://download.docker.com/linux/ubuntu jammy stable
36 state: present
37
38 - name: Update apt and install docker-ce
39 apt:
40 name: docker-ce
41 state: latest
42 update_cache: true
43
44 - name: Install Docker Module for Python
45 pip:
46 name: docker
47
48 - name: Pull default Docker image
49 community.docker.docker_image:
50 name: "{{ default_container_image }}"
51 source: pull
52
53 - name: Create default containers
54 community.docker.docker_container:
55 name: "{{ default_container_name }}{{ item }}"
56 image: "{{ default_container_image }}"
57 command: "{{ default_container_command }}"
58 state: present
59 with_sequence: count={{ container_count }}

Execute the playbook

1ansible-playbook -i inventory playbook.yaml

Discuss on TwitterEdit post on GitHub

Share article
Kent C. Dodds

Kent C. Dodds is a JavaScript software engineer and teacher. He's taught hundreds of thousands of people how to make the world a better place with quality software development tools and practices. He lives with his wife and four kids in Utah.