Shell module commands runs on Local Controller

Good morning,

I am attempting to use the (shell:) command to obtain the disk space for a remote host. Once I get the command working, I’ll then use AWK to filter the output.

However, the below example playbook insists on running the (shell:) command on the local Ansible control machine, and not the remote host (server1). Thus, I end up with disk space (/var) for the Ansible machine, and NOT the remote host (server1). Does anyone know how what I may be doing wrong? Other commands for example (bigip_command:) run on the remote host fine.

If I enter a directory (chdir:) that only exists on the remote host, Ansible throws an error like “Directory does not exist”, which is true, as it is looking on the local machine.

Thank you!

  • name: Test Playbook
    hosts: server1
    connection: local
    gather_facts: true

  • name: Display Disk Space

shell:

cmd: df /var
chdir: /var
register: disk_space_output

That is becase 'connection: local' forces using the 'local' connection
plugin, which means that instead of executing on the target it 'forks
locally' to execute the module.
Just remove it, 99% of the time you do not need to specify `connection`.

Just remove it, 99% of the time you do not need to specify connection.

… and you still can delegate individual tasks to localhost when needed …

Walter

Hello Brian. Thank you for your reply. Please all me to provide some additional context based on your suggestion (I should have mentioned this on my initial post):

We are working with F5 BIG-IP devices and using REST API (443) (https://clouddocs.f5.com/products/orchestration/ansible/devel/usage/playbook_tutorial.html) for the remote device connection, and not SSH (22). If I remove (connection: local), Ansible try’s to connect using SSH (22) (which is blocked). Since the (shell:) module does not appear to support the F5 (provider:) credentials/connection (please see example below), I’m not sure how to get (shell:) to connect to a remote host. I have tried adding (delegate_to: server1) to the (shell:) command and it simply ignores it.

vars:
provider:
password: password
server: 1.1.1.1
user: admin_name
validate_certs: no
server_port: 443

I can get the basic disk space command (df) to work when using a F5 module (bigip_command:) (which does use provider:). However, the F5 modules do not support (AWK, GREP, SED, etc.) (which is a bummer), like (shell:) and (command:) do. I may end up having to use (bigip_command:) to grab the disk space, and then use a separate Ansible (regex_search) task to filter for what I want, which is what I have done in the past (albeit this is an extra step).

If the F5 is returning structured data (ie. JSON), then set_fact tasks with filters can help you get to what you need.

Walter

Agreed. This is essentially what I am doing now and it does work (pipe var to regex_search, etc.). I was just hoping to get the (shell:) command to work in our environment, but with our setup, it does not appear to be possible. I should also be to take the (bigip_command:) output, write the JSON to file, and then run AWK against the file. I have not tried this yet. I’ll probably just stick with (regex_search) for now. Thanks!

shell and command require python on the remote, try raw instead