This repository give us a place to maintain a control versions of the every object in an Ansible Automation Controller.
CasC (Configuration as Code) means the posibility of define every object of Ansible Automation Controller as code in a git repository. In this lab, we have defined two environments (dev and pro) to do the CasC and interact with a gitops approach between them.
NOTE: Execution Environment called ee-casc in the following playbooks has six needed collections (You can use a EE with this collections inside or install each collection in your workspace. If you will not use a EE, you can skip "podman login" steps.):
- name: ansible.controller
- name: ansible.utils
- name: ansible.posix
- name: community.general
- name: redhat_cop.controller_configuration
- name: automationiberia.casc_setup
Before using CasC as a GitOps approach, it is needed to launch an initialization from CLI which it is called Day-Zero.
-
Clone the repository and create a new day-zero branch
git clone [email protected]:acme/ansible-controller/casc-twitch-demo.git cd casc-twitch-demo/ git checkout -b casc-dev-day0
-
Edit credentials to connect to the controller for day zero.
vi group_vars/dev/configure_connection_controller_credentials.yml vi group_vars/pro/configure_connection_controller_credentials.yml ansible-vault encrypt group_vars/dev/configure_connection_controller_credentials.yml group_vars/pro/configure_connection_controller_credentials.yml
-
Edit credentials for day zero
vi orgs_vars/casc-twitch-demo/env/dev/controller_credentials.d/controller_credentials.yml vi orgs_vars/casc-twitch-demo/env/pro/controller_credentials.d/controller_credentials.yml ansible-vault encrypt orgs_vars/casc-twitch-demo/env/pro/controller_credentials.d/* orgs_vars/casc-twitch-demo/env/dev/controller_credentials.d/*
-
Check the inventory file. Example:
[dev] demo-ctr1-dev.bcnconsulting.com [pro] demo-ctr1-prd.bcnconsulting.com
-
Setting vault credential file
echo "my_vault_pass" > ~/.vault_password ln ~/.vault_password .
-
Launch ansible-navigator from CLI to setup day-zero of CasC. Example:
ansible-navigator run casc_ctrl_config.yml -i inventory -l dev -e '{orgs: casc-twitch-demo, dir_orgs_vars: orgs_vars, env: dev}' -m stdout --eei quay.io/automationiberia/aap/ee-casc --vault-password-file .vault_password ansible-navigator run casc_ctrl_config.yml -i inventory -l pro -e '{orgs: casc-twitch-demo, dir_orgs_vars: orgs_vars, env: pro}' -m stdout --eei quay.io/automationiberia/aap/ee-casc --vault-password-file .vault_password
-
Push the changes
git status -s git add -A git commit -m "CasC day zero" git push origin casc-dev-day0
-
Pomote the
casc-dev-day0
branch to dev (dev
branch) -
Promote the
dev
branch to pro (pro
branch) -
Configure the webhooks for both environments: DEV and PRO.
NOTE: You can use the playbook gitlab_webhook.yml or do it manually if you prefer how it is done:
AUTOMATICALLY WITH A PLAYBOOK (it is needed to change and in the command. Also it can be used gitlab_api_password instead of gitlab_api_token in case it is used a password.):
ansible-navigator run gitlab_webhook.yml -i inventory -l dev -e '{gitlab_action_push: true, gitlab_action_tag: false, gitlab_branch_filter: dev, gitlab_api_user: <user>, gitlab_api_token: <token>}' -m stdout --eei quay.io/automationiberia/aap/ee-casc --vault-password-file .vault_password ansible-navigator run gitlab_webhook.yml -i inventory -l pro -e '{gitlab_action_push: false, gitlab_action_tag: true, gitlab_api_user: <user>, gitlab_api_token: <token>}' -m stdout --eei quay.io/automationiberia/aap/ee-casc --vault-password-file .vault_password
MANUALLY IN DEV (only if you didn't by the playbook):
- Go to Dev Controller and open casc-twitch-demo CasC_AAP_Workflow
- Copy the content of "Webhook URL" and "Webhook Key"
- Go to Gitlab -> Settings -> Webhooks
- Paste the content of "Webhook URL" and "Webhook Key" in the gaps "URL" and "Secret Content"
- Select Push events and fill the gap with dev
MANUALLY IN PRO (only if you didn't by the playbook):
- Go to PRO Controller and open casc-twitch-demo CasC_AAP_Workflow
- Copy the content of "Webhook URL" and "Webhook Key"
- Go to Gitlab -> Settings -> Webhooks
- Paste the content of "Webhook URL" and "Webhook Key" in the gaps "URL" and "Secret Content"
- Select Tag events and fill the gap with dev
-
Clone the given repository:
git clone [email protected]:acme/ansible-controller/casc-twitch-demo.git cd casc-twitch-demo/
-
Create a new branch from
dev
to introduce the new items:git checkout dev git checkout -b add_info_job_template
-
Add a new Playbook and a new Job Template
File:
new_playbook1.yaml
cat > new_playbook1.yaml <<EOF --- - name: "Play to show the hostname" hosts: all tasks: - name: "Show the hostname" debug: msg: - "This server is called (from Ansible inventory): {{ inventory_hostname }}" - "This server is called (from Execution Environment): {{ lookup('pipe', 'cat /etc/hostname') }}" - "Running as user: {{ lookup('pipe', 'id') }}" ... EOF
File:
new_playbook2.yaml
cat > new_playbook2.yaml <<EOF --- - name: "Play to show the hostname" hosts: all connection: local tasks: - name: "Show the hostname" debug: msg: - "This server is called (from Ansible inventory): {{ inventory_hostname }}" - "This server is called (from Execution Environment): {{ lookup('pipe', 'cat /etc/hostname') }}" - "Running as user: {{ lookup('pipe', 'id') }}" ... EOF
File:
orgs_vars/casc-twitch-demo/env/common/controller_job_templates.d/new_job_template.yaml
cat > orgs_vars/casc-twitch-demo/env/common/controller_job_templates.d/new_job_template.yaml <<EOF --- controller_templates: - name: "{{ orgs }} New Job Template" description: "Template to show how to add a new JT" organization: "{{ orgs }}" project: "{{ orgs }} CasC_Data" inventory: "{{ orgs }} Localhost" playbook: "new_playbook1.yaml" job_type: run fact_caching_enabled: false concurrent_jobs_enabled: true ask_scm_branch_on_launch: true extra_vars: ansible_python_interpreter: /usr/bin/python3 ansible_async_dir: /home/runner/.ansible_async/ execution_environment: "ee-casc" ... EOF
-
Commit the changes to the new branch
git add -A . git commit -am "Add new playbook and job template to show server information" git push -u origin add_info_job_template
-
Create a Merge Request to
dev
branch
The following automated process has ben executed at the Ansible Automation Controller:
The following diagram shows the components of the workflow:
Of course, the new Job Template has been created:
-
Pomote the
dev
branch to production (pro
branch)Similarly to the step 5, create a new Merge Request from the
dev
branch to thepro
branch:-
Select the source branch as
dev
andpro
as the destination one] -
Fill in the merge request information
⚠️ Be sure to write a title that have sense for the Merge Request: The default value here isdev
, that is not usefull at all!
When the Merge Request is already merged, the new Job Template is also created in the
pro
environment: -
-
Rollback the PRO environment to previously working tag
To rollback the status of the controller to a previous working version, it's only needed to run the following Job Templates:
-
Fix your playbook
git checkout dev git pull git checkout -b fix_playbook
Modify the Job Template to use the correct playbook:
playbook: "new_playbook2.yaml"
Updated file:
orgs_vars/casc-twitch-demo/env/common/controller_job_templates.d/new_job_template.yaml
--- controller_templates: - name: "{{ orgs }} New Job Template" description: "Template to show how to add a new JT" organization: "{{ orgs }}" project: "{{ orgs }} CasC_Data" inventory: "{{ orgs }} Localhost" playbook: "new_playbook2.yaml" job_type: run fact_caching_enabled: false concurrent_jobs_enabled: true ask_scm_branch_on_launch: true extra_vars: ansible_python_interpreter: /usr/bin/python3 ansible_async_dir: /home/runner/.ansible_async/ execution_environment: "ee-casc" ...
Commit and push your changes:
git commit -am "Fix the connection method" git push -u origin fix_playbook
Create a Merge Request to
dev
branch:Repeat the steps to create a new Merge Request from
dev
topro
, as described at step 6