Search code examples
rubypuppeterb

Running curl locally vs inside an erb file yields different results


I have a template file which I'd like to execute some simple code in (I have an endpoint which returns some json revealing other server details which are relevant to the template). I've added in the following code (values omitted where relevant):

<% require 'open3'
url = 'https://a.valid.address.com'
path = '/nodeStatuses'
port = '18091'
username = 'admin'
password = "#{@template_password}"


Open3.popen3("curl -m 10 -X GET --noproxy '*' -vvvv -m 10 --cacert /etc/pki/ca-trust/source/anchors/RootCA.crt -k -u #{username}:#{password} #{url}:#{port}#{path}") do |stdin, stdout, stderr, thread|
  pid = thread.pid
  stdin.close
  @stdout = stdout.read.chomp
  @stderr = stderr.read.chomp
end  %>

stdout: <%= @stdout %>
stderr: <%= @stderr %>

Strangely all my templates are filled with timeouts:

stderr:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
^M  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to a.valid.address.com port 18091 (#0)
*   Trying 10.10.10.10...
^M  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0^M  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0^M  0     0    0     0    0     0      0      0 --:--:--  0:00:03 --:--:--     0^M  0     0    0     0    0     0      0      0 --:--:--  0:00:04 --:--:--     0^M  0     0    0     0    0     0      0      0 --:--:--  0:00:05 --:--:--     0^M  0     0    0     0    0     0      0      0 --:--:--  0:00:06 --:--:--     0^M  0     0    0     0    0     0      0      0 --:--:--  0:00:07 --:--:--     0^M  0     0    0     0    0     0      0      0 --:--:--  0:00:08 --:--:--     0^M  0     0    0     0    0     0      0      0 --:--:--  0:00:09 --:--:--     0* Connection timed out after 10001 milliseconds
^M  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
* Closing connection 0
curl: (28) Connection timed out after 10001 milliseconds

My immediate thoughts were that the url is offline or there's something wrong with the curl, but running the same command via command line yields results:

curl -I -s -m 10 --cacert /etc/pki/ca-trust/source/anchors/RootCA.crt -k -u admin:secret https://a.valid.address.com:18091/nodeStatuses
HTTP/1.1 200 OK
X-XSS-Protection: 1; mode=block
X-Permitted-Cross-Domain-Policies: none
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Server: Couchbase Server
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Date: Fri, 30 Sep 2022 09:00:46 GMT
Content-Type: application/json
Content-Length: 685
Cache-Control: no-cache,no-store,must-revalidate

Then I thought it might be a limitation with erb; nope I get a response from another url (e.g stackoverflow).

Really looking for some clues here. Any help would be very much appreciated.


Solution

  • This template was being rendered on the puppetmaster and not locally on the server (this is expected behaviour). In the end I changed the logic and made the command executed on the puppetmaster return a list of hosts to populate the template file. This isn’t quite what I’d intended to do originally, but the end result is the same/similar.