Creating a CodeDeploy Ansible role

We are first going to go into the role directory that is present at the root location of our ansible repository:

$ cd roles  

As before, we will rely on ansible-galaxy to put in place the scaffolding needed to create our role:

$ ansible-galaxy init codedeploy  

Our role will be very simple; we are going to edit the file codedeploy/tasks/main.yml and make a call to the new module that the aws_codedeploy library brings as follows:

---
# tasks file for codedeploy
- name: Installs and starts the AWS CodeDeploy Agent
  aws_codedeploy: 
    enabled: yes  

At this point, we can create our new playbook for generic nodejs web servers.

We are going to go back in the root directory of the ansible repository:

$ cd ..  

There, we will create a new file called nodeserver.yml:

$ touch nodeserver.yml  

We will take the same approach we did previously with our other playbooks. The goal of our servers will be to run Node.js applications and run the CodeDeploy daemon.

Edit the nodeserver.yml file and add the following in it:

--- 
- hosts: "{{ target | default('localhost') }}" 
  become: yes 
  roles: 
    - nodejs 
    - codedeploy 
When using CodeDeploy in a configuration management system such as Ansible or CloudFormation, in order to avoid a race condition, it is important always to install all the dependencies for your application before starting the CodeDeploy.

We can now commit our changes to git.

We will first create a new branch and then add new files and directories that we created:

$ git checkout -b code-deploy
$ git add library roles/codedeploy nodeserver.yml ansible.cfg

And finally commit and push the changes:

$ git commit -m "adding aws_codedeploy library, role and a nodeserver playbook"
$ git push

Like before, you can now create a pull request and once the pull request is reviewed and approved, merge it back to the master.

In the end, your ansible repository should look like this: http://bit.ly/2uZ62bF.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset