Cloudwatch Agent Installation to EC2 Instances with Ansible

Cloudwatch Agent Installation to EC2 Instances with Ansible

Prerequisites

In this post, I will be explaining how to install Cloudwatch Agent using an Ansible Controller Host machine which is used for initializing and provisioning other machines.

You will need:

  • Basic knowledge of Ansible
  • A Controller machine to run the playbook remotely
  • A Controller machine with Ansible installed
  • A Remote machine with SSH Daemon and Python interpreter installed
  • A Remote machine with proper Policy attached (Allowance for PutMetricData)
  • Make sure you have a proper configuration .json file, check here

Playbook

Using a Controller EC2 instance makes it easier to manage other instances since in this way, you don’t need to install Ansible each machine you manage because it works with SSH Daemon and an up-to-date Python interpreter.

You can run this playbook on your Controller machine with:

ansible-playbook -s configure-cloudwatch-agent.yml

In fact, you can use variables to configure different environments such as Development, Production etc. For example, we could define “/opt/aws” as our base_dir and call it
{{ base_dir }}.

- name: Create Directory for Downloading Cloudwatch Agent .zip
  file:
    path: {{ base_dir }}/amazon-cloudwatch-zip
    state: directory
    owner: root
    group: root
    mode: 0755
    recurse: no
  when: init_status_result.stderr | search("Unknown job")

We could again inject variables from the CLI parameters. For example,

ansible-playbook -e "env=development" configure-cloudwatch-agent.yml

and inject {{ env }} to the corresponding places in our playbook.

Let’s take a look!

---

- hosts: xxx.xxx.xxx.xxx
  remote_user: root
  gather_facts: true
  tasks:
    - name: Check if Cloudwatch Agent is Installed Already
      command: status amazon-cloudwatch-agent
      register: init_status_result
      ignore_errors: yes

    - debug:
      var: init_status_result.stderr
      verbosity: 2

    - name: Create Directory for Downloading Cloudwatch Agent .zip
      file:
        path: /opt/aws/amazon-cloudwatch-zip
        state: directory
        owner: root
        group: root
        mode: 0755
        recurse: no
      when: init_status_result.stderr | search("Unknown job")

    - name: Download Latest Version of Amazon Cloudwatch Agent
      get_url:
        url: "https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip"
        dest: /opt/aws/amazon-cloudwatch-zip/AmazonCloudWatchAgent.zip
        mode: 0755
      when: init_status_result.stderr | search("Unknown job")

    - name: Unzip Cloudwatch Download File
      unarchive:
        remote_src: yes
        src: /opt/aws/amazon-cloudwatch-zip/AmazonCloudWatchAgent.zip
        dest: /opt/aws/amazon-cloudwatch-zip
      when: init_status_result.stderr | search("Unknown job")

    - name: Execute the Installation Script
      command: /opt/aws/amazon-cloudwatch-zip/install.sh
      args:
        chdir: /opt/aws/amazon-cloudwatch-zip
      when: init_status_result.stderr | search("Unknown job")

    - name: Transfer Cloudwatch Common Configuration(Proxies...) File
      copy:
        src: /files/common-config.toml
        dest: /opt/aws/amazon-cloudwatch-agent/etc
        owner: ec2-user
        group: ec2-user
        mode: 0755
      when: init_status_result.stderr | search("Unknown job")

    - name: Transfer Cloudwatch Configuration File
      copy:
        src: /files/amazon-cloudwatch-agent.json
        dest: /opt/aws/amazon-cloudwatch-agent/etc
        owner: ec2-user
        group: ec2-user
        mode: 0755

    - name: Stop Amazon Cloudwatch Agent
      command: stop amazon-cloudwatch-agent
      ignore_errors: yes

    - name: Start Amazon Cloudwatch Agent
      command: start amazon-cloudwatch-agent

Leave a Reply

Close Menu