Ansible troubleshooting - Destination does not exist rc 257
How to troubleshoot the error “Destination does not exist”, return code 257 enabling PasswordAuthentication for SSH using Ansible lineinfile module.
Introduction
Welcome to another episode of Ansible Pilot! I’m Luca Berton, and today we’ll delve into Ansible troubleshooting, focusing on the “Destination does not exist” error with return code 257. This error typically occurs when attempting to edit a file that doesn’t exist on the target file system. We’ll explore the root causes and demonstrate how to resolve this issue using the Ansible lineinfile
module.
Understanding the Error
The fatal error message “Destination does not exist” with return code 257 arises when Ansible attempts to modify a file that is either misspelled or entirely absent on the target system. This error is commonly encountered while configuring SSH settings, particularly when enabling PasswordAuthentication
.
I’m Luca Berton, and let’s dive into today’s Ansible troubleshooting session.
Demo
To illustrate the troubleshooting process, we’ll jump into a live demo. In this scenario, we’ll attempt to edit a configuration file, /etc/ssh/sshd_config2
, which is misspelled on our target system.
Error Code
Let’s examine the Ansible playbook (destinationdoesnotexist_257_error.yml
) triggering the error:
---
- name: PasswordAuthentication enabled
hosts: all
become: true
gather_facts: false
tasks:
- name: ssh PasswordAuthentication
ansible.builtin.lineinfile:
dest: /etc/ssh/sshd_config2
regexp: '^PasswordAuthentication'
line: "PasswordAuthentication yes"
state: present
notify: ssh restart
handlers:
- name: ssh restart
ansible.builtin.service:
name: sshd
state: restarted
Upon execution, the playbook results in a fatal error with return code 257:
$ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/destinationdoesnotexist_257_error.yml
PLAY [PasswordAuthentication enabled] *************************************************************
TASK [ssh PasswordAuthentication] *****************************************************************
fatal: [demo.example.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "msg": "Destination /etc/ssh/sshad_config2 does not exist !", "rc": 257}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Fix Code
Let’s correct the playbook to reference the correct file (/etc/ssh/sshd_config
) in the fixed version (destinationdoesnotexist_257_fix.yml
):
---
- name: PasswordAuthentication enabled
hosts: all
become: true
gather_facts: false
tasks:
- name: ssh PasswordAuthentication
ansible.builtin.lineinfile:
dest: /etc/ssh/sshd_config
regexp: '^PasswordAuthentication'
line: "PasswordAuthentication yes"
state: present
notify: ssh restart
handlers:
- name: ssh restart
ansible.builtin.service:
name: sshd
state: restarted
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Fix Execution
Now, when we execute the corrected playbook, it should run successfully:
$ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/destinationdoesnotexist_257_fix.yml
PLAY [PasswordAuthentication enabled] *************************************************************
TASK [ssh PasswordAuthentication] *****************************************************************
changed: [demo.example.com]
RUNNING HANDLER [ssh restart] *********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Verification
To confirm the changes, log in to the target machine and check the configuration file:
$ ssh [email protected]
[devops@demo ~]$ sudo su
[root@demo devops]# grep 'PasswordAuthentication yes' /etc/ssh/sshd_config
#PasswordAuthentication yes
PasswordAuthentication yes
[root@demo devops]# systemctl status sshd
# Output showing SSH service status
Recap
In this Ansible troubleshooting guide, we explored the “Destination does not exist” error with return code 257. By understanding the root causes and applying the correct playbook adjustments, you can overcome this issue in your Ansible automation.
If you found this tutorial helpful, consider subscribing for more Ansible tips and tricks. Happy automating!
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
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate