Bootstrap FreeKB - Ansible - Getting Started with the Amazon Web Services (AWS) modules
Ansible - Getting Started with the Amazon Web Services (AWS) modules

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

Prerequisites

Here is a playbook that can be used to install the AWS CLI. This playbook uses the following modules and parameters.

---
- name: aws setup CLI play
  hosts: all
  tasks:
  - name: use stat to determine if the AWS CLI exists
    stat:
      path: /usr/local/bin/aws
    register: out

  - debug:
      var: out.stat.exists

  - name: enter this block when /usr/local/bin/aws does not exist
    block:

    - name: set_fact aws_cli_url
      set_fact:
        aws_cli_url: https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip

    - debug:
        var: aws_cli_url

    - name: use uri to determine if {{ aws_cli_url }} exists
      uri:
        url: "{{ aws_cli_url }}"
        method: GET
      register: uri

    - name: display uri.status (should be 200 meaning {{ aws_cli_url }} exists)
      debug:
        var: uri.status

    - name: fail if uri.status is NOT 200
      fail:
        msg: it appears that {{ aws_cli_url }} does NOT exist
      when: uri.status != 200

    - name: remove /tmp/awscli-exe-linux-x86_64.zip (if exists)
      file:
        path: /tmp/awscli-exe-linux-x86_64.zip
        state: absent

    - name: use get_url to download {{ aws_cli_url }} to /tmp
      get_url:
        url: "{{ aws_cli_url }}"
        dest: /tmp
      register: get_url

    - name: display get_url.status_code (should be 200 meaning {{ aws_cli_url }} was downloaded to /tmp)
      debug:
        var: get_url.status_code

    - name: fail if get_url.status_code is NOT 200
      fail:
        msg: it appears {{ aws_cli_url }} was NOT downloaded to /tmp
      when: get_url.status_code != 200

    - name: display get_url.dest (should be /tmp/awscli-exe-linux-x86_64.zip)
      debug:
        var: get_url.dest

    - name: use unarchive to extract /tmp/awscli-exe-linux-x86_64.zip to
      unarchive:
        src: "{{ get_url.dest }}"
        dest: /tmp
        remote_src: true
      register: unarchive

   - name: remove /tmp/awscli-exe-linux-x86_64.zip
      file:
        path: "{{ get_url.dest }}"
        state: absent

    - name: issue the /tmp/aws/install command to install the AWS CLI
      shell: /tmp/aws/install
      register: shell

    - name: display the shell.rc (should be 0 meaning the AWS CLI was successfully installed)
      debug:
        var: shell.rc

    - name: display the shell.stdout (should be something like 'You can now run  /usr/local/bin/aws')
      debug:
        var: shell.stdout

    - name: remove the /tmp/aws directory
      file:
        path: /tmp/aws
        state: absent

    when: out.stat.exists == false
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f8783e in the box below so that we can be sure you are a human.