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.
``yaml
---
- 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:
`bash
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:
`bash
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.
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:
`yaml
---
- 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:
``bash
PLAY [file module demo]