Ansible troubleshooting - urlopen error
How to reproduce the urlopen error in Ansible, troubleshooting, and fix to be able to successfully open an URL in your playbook.


Today we’re going to talk about Ansible troubleshooting, specifically about urlopen error
.
I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- 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 Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- 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
demo
The best way of talking about Ansible troubleshooting is to jump in a live demo to show you practically the urlopen error
and how to solve it!
error code
- urlopen_error.yml
---
- name: uri module demo
hosts: all
become: false
vars:
server: "https://reqres.it"
endpoint: "/api/users?page=2"
tasks:
- name: list users
ansible.builtin.uri:
url: "{{ server }}{{ endpoint }}"
method: GET
status_code: 200
timeout: 30
register: result
- name: debug
ansible.builtin.debug:
var: result.json.data
error execution
$ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/urlopen_error.yml
PLAY [uri module demo] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [demo.example.com]
TASK [list users] **************************************************************
fatal: [demo.example.com]: FAILED! => {"changed": false, "elapsed": 15, "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno -2] Name or service not known>", "redirected": false, "status": -1, "url": "https://reqres.it/api/users?page=2"}
PLAY RECAP *********************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
fix code
- urlopen_fix.yml
---
- name: uri module demo
hosts: all
become: false
vars:
server: "https://reqres.in"
endpoint: "/api/users?page=2"
tasks:
- name: list users
ansible.builtin.uri:
url: "{{ server }}{{ endpoint }}"
method: GET
status_code: 200
timeout: 30
register: result
- name: debug
ansible.builtin.debug:
var: result.json.data
fix execution
$ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/urlopen_fix.yml
PLAY [uri module demo] ****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [list users] *********************************************************************************
ok: [demo.example.com]
TASK [debug] **************************************************************************************
ok: [demo.example.com] => {
"result.json.data": [
{
"avatar": "https://reqres.in/img/faces/7-image.jpg",
"email": "[email protected]",
"first_name": "Michael",
"id": 7,
"last_name": "Lawson"
},
{
"avatar": "https://reqres.in/img/faces/8-image.jpg",
"email": "[email protected]",
"first_name": "Lindsay",
"id": 8,
"last_name": "Ferguson"
},
{
"avatar": "https://reqres.in/img/faces/9-image.jpg",
"email": "[email protected]",
"first_name": "Tobias",
"id": 9,
"last_name": "Funke"
},
{
"avatar": "https://reqres.in/img/faces/10-image.jpg",
"email": "[email protected]",
"first_name": "Byron",
"id": 10,
"last_name": "Fields"
},
{
"avatar": "https://reqres.in/img/faces/11-image.jpg",
"email": "[email protected]",
"first_name": "George",
"id": 11,
"last_name": "Edwards"
},
{
"avatar": "https://reqres.in/img/faces/12-image.jpg",
"email": "[email protected]",
"first_name": "Rachel",
"id": 12,
"last_name": "Howell"
}
]
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Recap
Now you know better how to troubleshoot the Ansible urlopen error
and solve it!
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack 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