An automation controller Project is a logical collection of Ansible Playbooks. You manage your playbooks by placing them into a source code management (SCM) system supported by automation controller, including Git, Subversion, and others.
And yes, you should definitely keep your Playbooks under version control.
In this lab we’ll use existing Playbooks that are provided in this Github repository:
https://github.com/ansible-labs-crew/playbooks_summit2021
A Playbook to install the Apache webserver has already been committed to the directory rhel/apache, apache_install.yml
, here for reference:
---
- name: Apache server installed
hosts: all
tasks:
- name: latest Apache version installed
yum:
name: httpd
state: latest
- name: latest firewalld version installed
yum:
name: firewalld
state: latest
- name: firewalld enabled and running
service:
name: firewalld
enabled: true
state: started
- name: firewalld permits http service
firewalld:
service: http
permanent: true
state: enabled
immediate: yes
- name: Apache enabled and running
service:
name: httpd
enabled: true
state: started
Note the difference to other Playbooks you might have written! Most importantly there is no become
and hosts
is set to all
.
To configure and use this repository as a Source Code Management (SCM) system in automation controller you have to create a Project that uses the repository
Go to Resources ⇒ Projects in the side menu view click the button. Fill in the form:
Name: Ansible Workshop Examples
Organization: Default
Source Control Credential Type: Git
Now you need the URL to access the repo. You could get the URL in Github as Clone URL. Enter the URL into the Project configuration:
Source Control URL: https://github.com/ansible-labs-crew/playbooks_summit2021.git
Options: Tick the boxes Clean, Delete, Update Revision on Launch to always get a fresh copy of the repository and to update the repository when launching a job.
Click Save
The new Project will be synced automatically after creation. But you can also do this manually: Sync the Project again with the Git repository by going to the Projects view and clicking the circular arrow Sync Project icon to the right of the Project.
After starting the sync job, go to the Jobs view: there are new jobs for the update of the Git repository.
A job template is a definition and set of parameters for running an Ansible job. Job templates are useful to execute the same job many times. So before running an Ansible Job from automation controller you must create a Job Template that pulls together:
Inventory: On what hosts should the job run?
Credentials What credentials are needed to log into the hosts?
Project: Where is the Playbook?
What Playbook to use?
Okay, let’s just do that: Go to the Templates view, click the button and choose Add job template.
Remember that you can often click on magnifying glasses to get an overview of options to pick to fill in fields.
Name: Install Apache
Job Type: Run
Inventory: Workshop Inventory
Project: Ansible Workshop Examples
Execution Environment: Default execution environment
Playbook: rhel/apache/apache_install.yml
Credentials: Workshop Credentials
We need to run the tasks as root so check Privilege Escalation under Options
Click Save
You can start the job directly from the Details view by clicking the Launch button, or by clicking on the rocket in the Templates overview. After launching the Job Template, you are automatically brought to the job overview where you can follow the playbook execution in real time:
Since this might take some time, have a closer look at all the details provided:
Now switch to the Details view:
All details of the job template like inventory, project, credentials and playbook are shown.
Additionally, the actual revision of the playbook is recorded here - this makes it easier to analyse job runs later on.
Also the time of execution with start and end time is recorded, giving you an idea of how long a job execution actually was.
After the Job has finished go to the main Jobs view: All jobs are listed here, you should see directly before the Playbook run a job of type Source Control Update was started. This is the Git update we configured for the Project on Template launch!
Time for a little challenge:
You have already been through all the steps needed, so try this for yourself.
What about systemctl status httpd
?
Go to Inventories ⇒ Workshop Inventory
On the Hosts tab view select all hosts and click Run Command
Module: command
Arguments: systemctl status httpd
Next
Execution Environment: Don’t specify, the Default execution environment will be used.
Next
Machine Credential: Workshop Credentials
Click Launch
It should show Apache running on all nodes!
Here is a list of tasks:
Please make sure to finish these steps as the next chapter depends on it!
Create a new inventory called Webserver
and make only node1.<GUID>.internal
member of it.
Copy the Install Apache
template using the copy icon in the Templates view.
Edit the Template and change the name to Install Apache Ask
.
Change the Inventory setting of the Project so it will prompt for the inventory on launch (tick the appropriate box).
Save
Launch the Install Apache Ask
template.
It will now ask for the inventory to use, choose the Webserver
inventory and click Next and Launch.
Wait until the Job has finished and make sure it ran only on node1.<GUID>.internal
.
The Job didn’t change anything because Apache was already installed in the latest version.