Introduction

Welcome to another episode of Ansible Pilot! I’m Luca Berton, and today we’ll delve into Ansible troubleshooting, focusing on the infamous “Invalid Argument” error. This error commonly occurs when using the Ansible file module to create a symlink in a Linux environment. Let’s explore how to reproduce, troubleshoot, and fix this issue.

The Demo

To better understand Ansible troubleshooting, let’s dive into a live Playbook. We’ll create a playbook (invalidargument_error.yml) that attempts to create a symbolic link using the file module. In this example, we’re trying to symlink /proc/cpuinfo to ~/example.

---
- name: file module demo
  hosts: all
  vars:
    mylink: "~/example"
    mysrc: "/proc/cpuinfo"
  tasks:
    - name: Creating a symlink
      ansible.builtin.file:
        path: "{{ mylink }}"
        dest: "{{ mysrc }}"
        state: link

Executing this playbook (ansible-playbook -i inventory invalidargument_error.yml) results in the following error:

TASK [Creating a symlink] ******************************************************
An exception occurred during task execution.
OSError: [Errno 22] Invalid argument: b'/proc/cpuinfo'

Understanding the Error

The error is clear: “OSError: [Errno 22] Invalid argument.” This occurs because the file module expects a valid source (src) and destination (dest) when creating a symlink. In our original playbook, we only provided the path and dest parameters, leading to the “Invalid argument” error.

Verbose Execution

Executing the playbook with increased verbosity (-vvv) provides a more detailed traceback, helping us pinpoint the issue:

ansible-playbook -i inventory invalidargument_error.yml -vvv

The output reveals the exact error in the file module, emphasizing the need for a valid source parameter.

Join 50+ hours of courses in our exclusive community

Fixing the Code

To resolve the “Invalid Argument” error, we need to correct our playbook. The fixed playbook (invalidargument_fix.yml) includes the missing src parameter:

---
- name: file module demo
  hosts: all
  vars:
    mylink: "~/example"
    mysrc: "/proc/cpuinfo"
  tasks:
    - name: Creating a symlink
      ansible.builtin.file:
        src: "{{ mysrc }}"
        dest: "{{ mylink }}"
        state: link

Successful Execution

Executing the fixed playbook (ansible-playbook -i inventory invalidargument_fix.yml) should now complete without errors:

PLAY [file module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [Creating a symlink] *************************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Conclusion

In this tutorial, we learned how to reproduce, troubleshoot, and fix the “Invalid Argument” error when using the Ansible file module. Remember always to check the module documentation for the required parameters.

I hope this guide helps you overcome similar issues in your Ansible automation journey. If you found this information valuable, consider subscribing for more Ansible insights.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza