Gitlab Integration with AWX - Automating Ansible Playbook Execution
In the below videos, I explain how to install, configure and remove Argo CD with Bash and Ansible.
Tutorial: Installing, Configuring, and Removing Argo CD with Bash and Ansible
In this tutorial, we will cover how to install, configure, and remove Argo CD, a declarative, GitOps continuous delivery tool for Kubernetes, using Bash scripts and Ansible playbooks.
Part 1: Installing and Configuring Argo CD
Install Git and Helm:
Start by installing Git and Helm in your environment. Helm is a package manager for Kubernetes that simplifies deployment of applications and services.
|
|
Enable Helm Autocomplete:
To make your Helm experience more efficient, enable autocomplete for your bash shell:
|
|
Create the Argo CD Namespace:
Namespaces help organize resources within Kubernetes. Create a namespace specifically for Argo CD:
|
|
Add the Argo CD Helm Repository:
Helm repositories store packaged Helm charts. Add the Argo CD repository to Helm:
|
|
Update Your Helm Repositories:
Ensure you have the latest chart information from all your added repositories:
|
|
Install Argo CD with Helm:
Deploy Argo CD into your Kubernetes cluster within the argocd
namespace:
|
|
Create an Ingress Resource for Argo CD:
Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Create an ingress to access Argo CD externally:
First, create a file named argocd-ingress.yml
:
|
|
Then, insert the following YAML content:
|
|
Apply the ingress configuration:
|
|
Retrieve Initial Admin Password:
Argo CD generates an initial admin password that you’ll need for login:
|
|
You should delete the initial secret afterwards as suggested by the Getting Started Guide
Modify Argo CD’s Configuration for logout issue:
If you encounter issues related to logout error, update the Argo CD configuration:
|
|
Part 2: Automating Installation with Bash
To automate the installation process using Bash, follow these steps:
- Create a script named
argocd-install.sh
:
|
|
- Insert the provided Bash script content into the file, which mirrors the manual installation steps we covered earlier.
|
|
- Save the file and make it executable:
|
|
- Execute the script:
|
|
- Retrieve Initial Admin Password:
Argo CD generates an initial admin password that you’ll need for login:
|
|
- You should delete the initial secret afterwards as suggested by the Getting Started Guide
Part 3: Automating Installation with Ansible
For those who prefer Ansible, the tutorial includes steps to automate the Argo CD deployment using an Ansible playbook:
- Create an Ansible playbook named
argocd-install.yml
:
|
|
- Copy the provided Ansible playbook content into the file. The playbook automates the steps from the manual installation process.
|
|
- Save the file and run the playbook:
|
|
You should see similar output:
|
|
- Retrieve Initial Admin Password:
Argo CD generates an initial admin password that you’ll need for login:
|
|
- You should delete the initial secret afterwards as suggested by the Getting Started Guide
The above Ansible playbook is structured to automate the deployment and configuration of Argo CD in a Kubernetes environment. Below is a detailed breakdown of its components and tasks:
-
Overall Structure: The playbook starts with YAML front matter (
---
) and defines a single play named “Install Argo CD”. It is intended to be executed on the local machine (indicated byhosts: localhost
) and requires elevated privileges (become: yes
), which is similar to running commands with sudo. -
Tasks: The playbook comprises several tasks, each designed to accomplish a specific step in the setup process:
-
Create argocd namespace: This task uses the
kubectl create namespace argocd
command to create a new Kubernetes namespace calledargocd
. This namespace is intended for all resources related to Argo CD. Theignore_errors: yes
directive ensures that the playbook continues even if this command encounters an error, which might be useful if the namespace already exists. -
Add Argo CD repository: Executes
helm repo add argo https://argoproj.github.io/argo-helm
to add the Argo CD chart repository to Helm, enabling Helm to install Argo CD from this repository. -
Update repository using Helm: Runs
helm repo update
to update the local cache of charts from all added repositories, ensuring the latest versions are available for installation. -
Install Argo CD using Helm: This task uses Helm to install Argo CD into the previously created
argocd
namespace. It sets theKUBECONFIG
environment variable explicitly to point to the kubeconfig file, ensuring that Helm interacts with the correct Kubernetes cluster. -
Create Ingress for Argo CD: Applies an Ingress resource to expose the Argo CD server externally. The resource is defined inline and applied using
kubectl apply
. The Ingress is configured to use HTTPS and directs traffic to theargocd-server
service. -
Pause for 3 minutes to allow Argo CD to initialize: Utilizes the
pause
module to halt playbook execution for three minutes. This delay gives Argo CD time to fully start and become operational before proceeding with further configurations. -
Get argocd-cm configmap: Retrieves the
argocd-cm
ConfigMap from theargocd
namespace and writes it to a file (/tmp/argocd-cm.yml
). This ConfigMap contains configuration settings for Argo CD. -
Update argocd-cm configmap: Employs the
replace
module to modify the saved ConfigMap file, changing instances ofexample.com
tosysadmin.homes
. This task customizes Argo CD’s domain settings to match the desired environment. -
Apply the modified argocd-cm configmap: Applies the changes to the
argocd-cm
ConfigMap back to the Kubernetes cluster usingkubectl apply
, updating the Argo CD configuration.
Each task includes the ignore_errors: yes
option to continue execution even if errors occur. This can be useful in scripts where errors in some operations can be anticipated or are inconsequential, but it might also obscure important problems, so it’s typically used with caution.
By combining these tasks, the playbook automates the setup and initial configuration of Argo CD, facilitating continuous deployment and management of applications within Kubernetes environments.
You should delete the initial secret afterwards as suggested by the Getting Started Guide
Part 4: Removing Argo CD with Ansible
When you need to remove Argo CD from your cluster, use the provided Ansible playbook designed for clean removal:
- Create a removal playbook named
remove-argocd.yml
:
|
|
- Insert the provided content aimed at systematically deleting Argo CD components.
|
|
- Execute the removal playbook:
|
|
This playbook de-scales, deletes deployments, services, statefulsets, service accounts, role bindings, roles, ingresses, and finally, the entire argocd
namespace, effectively cleaning up all Argo CD components from your cluster.
More details below:
This Ansible playbook is designed to systematically remove Argo CD and its associated resources from a Kubernetes cluster. Each task in the playbook uses the shell
module to execute kubectl
commands directly, interacting with the cluster to delete specific Argo CD components. The playbook operates on the local machine (hosts: localhost
) and requires elevated privileges (become: yes
). Here’s a detailed breakdown:
-
scales all deployments in the argocd namespace to zero replicas: This task scales down all deployments in the
argocd
namespace to zero replicas, effectively stopping all running Argo CD components. This is often done as a preliminary step before deletion to ensure a graceful shutdown of services. -
Remove Argo CD deployments: Deletes specific Argo CD deployments, including the repository server, ApplicationSet controller, notifications controller, Redis server, Dex server, and the main Argo CD server itself, all within the
argocd
namespace. -
Remove Argo CD services: Deletes services associated with the same components listed above. In Kubernetes, services provide network access to set of pods, so removing these services cuts off network access to the corresponding Argo CD components.
-
Remove Argo CD statefulsets: Deletes the
argocd-application-controller
StatefulSet. In Argo CD, the application controller manages the lifecycle of applications and continuously monitors application states. Since it’s deployed as a StatefulSet, it requires a separate command from Deployments. -
Remove Argo CD service accounts: Deletes Kubernetes service accounts used by Argo CD components. Service accounts provide an identity for processes that run in a Pod and allow the Argo CD components to interact with the Kubernetes API.
-
Remove Argo CD role bindings: Deletes RoleBindings in the
argocd
namespace. RoleBindings link Roles to users or groups, granting permissions to the resources described in the roles. This step removes the permissions that Argo CD components had within the namespace. -
Remove Argo CD roles: Deletes Roles within the
argocd
namespace. Roles define a set of permissions, such as what operations are allowed on a set of resources. This step effectively removes those defined permissions. -
Remove Argo CD ingress in namespace argocd: Deletes the Ingress resource for Argo CD, which would have been used to expose the Argo CD server to the outside world via a URL.
-
Remove namespace argocd: Finally, deletes the entire
argocd
namespace, which removes all remaining resources under the namespace, cleaning up the environment. This is the final step to ensure that all components, including those possibly missed by earlier tasks, are removed.
Each task is set with ignore_errors: yes
, meaning the playbook will continue executing even if errors occur in any tasks. This can be useful when you are unsure if all components are present or if you want to ensure the playbook runs through to completion regardless of individual command failures. However, it’s important to be cautious with this setting, as it can also mean real errors are ignored, which could lead to incomplete cleanup.
Congratulations! You have successfully learned how to install, configure, automate, and remove Argo CD in a Kubernetes cluster using both Bash scripts and Ansible playbooks. This tutorial provides the tools necessary for both manual and automated management of Argo CD, catering to a variety of operational preferences.