jinja question

Skip to first unread message


Jul 28, 2022, 8:43:13 AMJul 28
to Salt-users
I'm trying to run:

{% set curgrp = salt['cmd.run']('/usr/bin/curl http://myhost.com:8085/$(hostname -f)' %}

but get back an error that curl does not understand '-f'

I tried adding:

{% set curgrp = salt['cmd.run']('/usr/bin/curl http://myhost.com:8085/\$(hostname -f)' %}

but get the same error.  I want to send the FQDN of the minion on which this state is running.  I also tried:

{% set thishost = salt['cmd.run']('hostname -f') %}
{% set curgrp = salt['cmd.run']('/usr/bin/curl http://myhost.com:8085/thishost' %}

{% set curgrp = salt['cmd.run']('/usr/bin/curl http://myhost.com:8085/'thishost %}
{% set curgrp = salt['cmd.run']('/usr/bin/curl http://myhost.com:8085/' ~ {{ thishost }} ~ %}
{% set curgrp = salt['cmd.run']('/usr/bin/curl http://myhost.com:8085/' {{ thishost }} %}

but none of that worked.  Does anyone know how to tack the minion's FQDN on the end of this curl command?


Jul 28, 2022, 10:35:42 AMJul 28
to Salt-users
I solved it by:

{% set thishost = salt['cmd.run']('hostname -f') %}
{% set mycurl = ' /usr/bin/curl http://myhost.com:8085/' ~ thishost %}
{% set curgrp = salt['cmd.run'](mycurl) %}

Phipps, Thomas

Jul 28, 2022, 12:39:35 PMJul 28
to salt-...@googlegroups.com

you really need to stop using cmdmod. it is a bad practice to use it so heavily. esp when there are tools that exist within the python. or variables that exist that already cover what you are trying to do.

 {% set curgrp = salt['cmd.run'](  ' /usr/bin/curl http://myhost.com:8085/' ~ salt["grains.get"]("fqdn")  ) %}

or getting rid of the cmdmod altogether

 {% set curgrp = salt['http.query']('http://myhost.com:8085/' ~ salt["grains.get"]("fqdn"))[body] %}


You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/59108f46-4b26-499d-bf35-e7e829f38880n%40googlegroups.com.


Jul 29, 2022, 4:54:19 AMJul 29
to Salt-users
Because I've only been using salt for a few years - still learning.  I have not learned python yet - need to do that.  :)
I'm all for using salt specific tools, just need to learn about them first.

Anyway, got it working as you suggested with one small change:

{% set curgrp = salt['http.query']('http://myhost.com:8085/' ~ salt["grains.get"]("fqdn")).body %}

Thanks so much!


Aug 7, 2022, 8:34:33 PMAug 7
to Salt-users
Question: is the value returned from salt['http.query']('http://myhost.com:8085/' ~ salt["grains.get"]("fqdn")).body volatile?

If it stays the same for the lifetime of a host, it would be better to add a custom grain, which will be populated once, then cached, for the lifetime of the minion process or until its told to refresh grains. You can define a custom grain in jinja, or you can use a state that sets the grain value explicitly.


Aug 8, 2022, 4:43:23 AMAug 8
to Salt-users
It is volatile.  Our org uses a tool which operates differently based on the "group" the server is assigned.  There is no command line tool on the minion that will show me what group that server is in.  I can go to a management web interface and export the data.  I then manipulate that data a little to get it into a form such that when presented through a web interface and the FQDN of the host is presented to the web interface, it returns the current group that server is a member.  Using that, I can determine if that is the current proper group or not.  I had to add a test if the value returned is defined.  There will be cases where the FQDN presented is not present in the exported table.  But, it is all working now.  Thanks!
Reply all
Reply to author
0 new messages