Ansible Series - P1
Photo by Possessed Photography on Unsplash
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/ansible23sudo apt update45sudo 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=khoa3svr02 ansible_host=192.168.1.176 ansible_user=khoa4svr03 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.111345[workers]6worker1 ansible_host=203.0.113.1137worker2 ansible_host=203.0.113.11489[masters:vars]10ansible_user=root1112[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: homeserver2 become: true3 vars:4 container_count: 45 default_container_name: docker6 default_container_image: ubuntu7 default_container_command: sleep 1d8 ansible_sudo_pass: "[root sudo password]" //temporary push the password on the file to test out the command9 tasks:10 - name: Install aptitude11 apt:12 name: aptitude13 state: latest14 update_cache: true1516 - name: Install required system packages17 apt:18 pkg:19 - apt-transport-https20 - ca-certificates21 - curl22 - software-properties-common23 - python3-pip24 - virtualenv25 - python3-setuptools26 state: latest27 update_cache: true28 - name: Add Docker GPG apt Key29 apt_key:30 url: https://download.docker.com/linux/ubuntu/gpg31 state: present3233 - name: Add Docker Repository34 apt_repository:35 repo: deb https://download.docker.com/linux/ubuntu jammy stable36 state: present3738 - name: Update apt and install docker-ce39 apt:40 name: docker-ce41 state: latest42 update_cache: true4344 - name: Install Docker Module for Python45 pip:46 name: docker4748 - name: Pull default Docker image49 community.docker.docker_image:50 name: "{{ default_container_image }}"51 source: pull5253 - name: Create default containers54 community.docker.docker_container:55 name: "{{ default_container_name }}{{ item }}"56 image: "{{ default_container_image }}"57 command: "{{ default_container_command }}"58 state: present59 with_sequence: count={{ container_count }}
Execute the playbook
1ansible-playbook -i inventory playbook.yaml