Use aws cli to start AWS workspaces via AWX

Good day!

My scenario is as follows: I have a dynamic inventory which gives me the latest data from my AWS workspaces with every run. I would like to start these workspaces regularly via AWX, run updates and then stop them again.

I have written the following playbook for the start:

- name: Start AWS Workspaces (Ubuntu)
  hosts: ubuntu2204workspacesstopped
  gather_facts: False
  tasks:
    - name: Start Workspace
      shell: >
        aws workspaces start-workspaces --start-workspace-requests WorkspaceId={{ hostvars[inventory_hostname]['workspace_id'] }}
      delegate_to: localhost

- name: Start AWS Workspaces (Amazon Linux)
  hosts: amazonlinux2workspacesstopped
  gather_facts: False
  tasks:
    - name: Start Workspace
      shell: >
        aws workspaces start-workspaces --start-workspace-requests WorkspaceId={{ hostvars[inventory_hostname]['workspace_id'] }}
      delegate_to: localhost

if I use this locally, it works and all workspaces are started.

if i run it as a template via AWX, it tries to run the shell command on each of the hosts instead of running it from the AWX host itself:

PLAY [Start AWS Workspaces (Ubuntu)] *******************************************
TASK [Start Workspace] *********************************************************
fatal: [IP_1 -> localhost]: FAILED! => {"changed": true, "cmd": "aws workspaces start-workspaces --start-workspace-requests WorkspaceId=WorkspaceId_1\n", "delta": "0:00:00.004320", "end": "2024-07-03 15:17:20.518879", "msg": "non-zero return code", "rc": 127, "start": "2024-07-03 15:17:20.514559", "stderr": "/bin/sh: line 1: aws: command not found", "stderr_lines": ["/bin/sh: line 1: aws: command not found"], "stdout": "", "stdout_lines": []}
fatal: [IP_2 -> localhost]: FAILED! => {"changed": true, "cmd": "aws workspaces start-workspaces --start-workspace-requests WorkspaceId=WorkspaceId_2\n", "delta": "0:00:00.003953", "end": "2024-07-03 15:17:20.520235", "msg": "non-zero return code", "rc": 127, "start": "2024-07-03 15:17:20.516282", "stderr": "/bin/sh: line 1: aws: command not found", "stderr_lines": ["/bin/sh: line 1: aws: command not found"], "stdout": "", "stdout_lines": []}
...
...
...
PLAY RECAP *********************************************************************
IP_1              : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
IP_2              : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
...
...
...

what could be the reason for this? have I overlooked something elementary?

You’re getting a 127 return code which is command not found. I’d venture a guess that the AWX linux user/environment does not have aws in it’s $PATH to just find it as you’ve written your tasks. Create a task that just does aws --version and run it in AWX. Do you still get an error? If you do, then it is the path to aws from your AWX user account when executing your playbook.

ah okay, i get this:

TASK [Check AWS CLI version] ***************************************************10:01:23

4

fatal: [localhost]: FAILED! => {"changed": false, "cmd": "aws --version", "msg": "[Errno 2] No such file or directory: b'aws'", "rc": 2, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

5

...ignoring

thats weird, because on the Execution Environment itself it works:

[root@test-ee bin]#  aws --version

aws-cli/2.17.5 Python/3.11.8 Linux/4.18.0-552.3.1.el8.x86_64 exe/x86_64.centos.9

if i use “localhost” as deligate or hosts, does it try to execute this on the EE i selected or on the AWX host himself? :thinking:

Small Update/Fix: aws cli was installed on the wrong EE (i did not used) :man_facepalming:

but thanks for reply!