We are going to start by navigating to our ansible/roles directory:
$ cd ansible/roles
We should have in this directory the helloworld and nodejs directories containing the configurations we created previously in Chapter 3, Treating Your Infrastructure As Code. We are now going to create our Jenkins role with the ansible-galaxy command:
$ ansible-galaxy init jenkins
We are now going to edit the task definition for this new role by editing the file jenkins/tasks/main.yml.
Open up the file with your favorite text editor.
The goal of our task is to install and start Jenkins. In order to do so, since we are on a Redhat-based operating system, we are going to install an RPM package through yum. Jenkins maintains a yum repository, so the first step will consist of importing the GNU Privacy Guard (GPG) key of that repository. Ansible has a module to manage these kinds of keys.
Below the initial comment of the tasks file, add the following:
- name: Import Jenkins GPG key rpm_key: state: present key: http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
The next step will be to import the yum repository to our yum repository configuration (basically an entry in /etc/yum.repos.d):
- name: Add Jenkins repository yum_repository: name: jenkins description: jenkins repository baseurl: http://pkg.jenkins.io/redhat enabled: no gpgcheck: yes
By default, we are disabling the repository. This is a common practice put in place to prevent third-party repositories such as this one from upgrading any important system library that we would rather see being managed by the Amazon yum repository.
We have reached the point where we can now use yum to install Jenkins. We will do that with the following call:
- name: Install Jenkins yum: name: jenkins enablerepo: jenkins state: present
Since the jenkins repository is disabled by default, we are enabling it through the enablerepo flag for the execution of this yum command.
At this point, Jenkins will be installed. As a best practice, we will specify which version of Jenkins we want to install (in our case the current version is 2.45). We also want to start the service and have it enabled at the chkconfig level so that if the EC2 instance where jenkins is installed restarts, Jenkins will start automatically. We can do that using the service module. Add the following after the previous call:
- name: Start Jenkins service: name: jenkins-2.45 enabled: yes state: started
For a simple Jenkins role, that's all we need.
We built a role that will allow us to install jenkins. We will want to create a new EC2 instance and install Jenkins on it with the end goal of testing our nodejs code on the instance. In order to be able to do that, the Jenkins host will need to also have Node.js and npm installed.
We have two options. We can either add our nodejs role as a dependency of the Jenkins role like we did for the helloworld role, or list the nodejs role in the list of roles for our playbook. Since ultimately Jenkins doesn't really require Node.js to run, we will opt for the second approach. In the root directory of our ansible repository, create the playbook file. The filename is jenkins.yml and it should look like this:
--- - hosts: "{{ target | default('localhost') }}" become: yes roles: - jenkins - nodejs
Our role is now complete, so we can commit our new role and push it to GitHub.
Following the best practices described previously, we will start by creating a new branch:
$ git checkout -b jenkins
Add our files:
$ git add jenkins.yml roles/jenkins
Commit and finally push the changes:
$ git commit -m "Adding a Jenkins playbook and role" $ git push
From there, submit a pull request inside GitHub and merge the branch back to master, and get back to the master branch:
$ git checkout master
In a real-life situation, to retrieve other developers changes, you will likely also periodically want to run:
$ git pull
We can now create our CloudFormation template in order to call that role.