run_once not working to register value of curl

Hello,
I am trying to get the latest version of few softwares and comparing it with a list of hosts with the current installed.

Calling the github API is creating issues with the number of call due to the the list of hosts that is quite big.
So I would like to register the latest versions in a variable and then use it to compare for each host.

I tried a lot of things, like run_once, changed_when but it seems like it is not working.

Here is the playbook.

  • hosts: “{{ variable_host }}”
    serial: 1
    tasks:

Add a new play to the top of the list that targets localhost.

The use the registered vars in a subsequent play that targets all your hosts.

Hello,
I am trying to get the latest version of few softwares and comparing it
with a list of hosts with the current installed.

Calling the github API is creating issues with the number of call due to
the the list of hosts that is quite big.
So I would like to register the latest versions in a variable and then use
it to compare for each host.

I tried a lot of things, like run_once, changed_when but it seems like it
is not working.

Lot of text, but you did not actually write what the problem is, not working can be so many things.

Here is the playbook.

- hosts: "{{ variable_host }}"
  serial: 1

run_once run one time for each batch, your batch size is 1, so it will run tasks with run_once for every host.

Hey Dick,
I tried that but the subsequent play is not able to recognise those variables.

{“msg”: "The task includes an option with an undefined variable. The error was: ‘PHP_SITE_RESPONSE’ is undefined

I changed the playbook like this :

- hosts: 127.0.0.1
tasks:
- name: “Get PHP version from site”
register: PHP_SITE_RESPONSE
uri:
body_format: json
method: GET
return_content: true
url: “https://www.php.net/releases/?json&version=7.2

- name: “Get APACHE version from site”
shell: “curl -s ‘https://api.github.com/repos/apache/httpd/tags?per_page=100’ | jq -r ‘.[] | .name’ | sort -V | grep ^2.4 | tail -n 1”
register: HTTPD_SITE_VERSION

- name: “Get GIT version from site”
shell: “curl -s ‘https://api.github.com/repos/git/git/tags?per_page=100’ | jq -r ‘.[] | .name’ | sort -V | grep ^v2 | tail -n 1 | cut -f1 -d’-’ | tail -c 7”
register: GIT_SITE_VERSION

- hosts: dev preprod
serial: 1
tasks:

- name: “Get current version PHP”
register: CURRENT_PHP_VERSION
shell: “/usr/local/php/bin/php --version | head -c 10 | tail -c 6”

- name: “Get current version HTTPD”
register: CURRENT_HTTPD_VERSION
shell: “httpd -v | grep 2.4 | tail -n 1 | tail -c 14 | head -c 6”

- name: “Get current version GIT”
register: CURRENT_GIT_VERSION
shell: “git --version | tail -c 7”

- name: Write header
run_once: true
lineinfile: dest=/home/ncontu/test.txt create=yes line=“LATEST VERSION PHP {{PHP_SITE_RESPONSE.json.version}} HTTPD {{HTTPD_SITE_VERSION.stdout_lines[0]}} GIT {{GIT_SITE_VERSION.stdout_lines[0]}}”
delegate_to: 127.0.0.1

- name: Write to local disk
lineinfile: dest=/home/ncontu/test.txt insertafter=EOF create=yes line=“{{ ansible_fqdn }} {{CURRENT_PHP_VERSION.stdout_lines[0]}} {{CURRENT_HTTPD_VERSION.stdout_lines[0]}} {{CURRENT_GIT_VERSION.stdout_lines[0]}}”
delegate_to: 127.0.0.1
when: CURRENT_PHP_VERSION.stdout_lines[0] != PHP_SITE_RESPONSE.json.version or CURRENT_HTTPD_VERSION.stdout_lines[0] != HTTPD_SITE_VERSION.stdout_lines[0] or CURRENT_GIT_VERSION.stdout_lines[0] != GIT_SITE_VERSION.stdout_lines[0]

.
.
.
.
.

Is there a way to tell those variables are from another play?
Or am I doing something wrong?

Thanks a lot for your help.

Hi Nicola,

I’d take a bit step back and reduce your playbook to just a couple of tasks:


- hosts: "{{ variable_host }}"
serial: 1
tasks:
-
name: "Get PHP version from site"
register: PHP_SITE_RESPONSE
uri:
body_format: json
method: GET
return_content: true
url: "[https://www.php.net/releases/?json&version=7.2](https://www.php.net/releases/?json&version=7.2)"
run_once: true
delegate_to: 127.0.0.1

-
name: "Show PHP version from site"
debug:
var: PHP_SITE_RESPONSE

What output does that produce?

DanL

[ncontu@cmd-config cmd]$ ansible-playbook playbooks/test.yml -e “variable_host=127.0.0.1”
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, but still be user configurable on
deprecation. This feature will be removed in version 2.10. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [127.0.0.1] ***************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Get PHP version from site] ***********************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Show PHP version from site] **********************************************************************************************************************************************
ok: [127.0.0.1] => {
“PHP_SITE_RESPONSE”: {
“cache_control”: “max-age=0”,
“changed”: false,
“connection”: “close”,
“content”: “{"announcement":true,"tags":["security"],"date":"19 Mar 2020","source":[{"filename":"php-7.2.29.tar.bz2","name":"PHP 7.2.29 (tar.bz2)","sha256":"eaa1f5503f2bf0c8569ec4ae80ffd8ca8cbc260f01c2503dd0e83dfc9cf0b923","date":"19 Mar 2020"},{"filename":"php-7.2.29.tar.gz","name":"PHP 7.2.29 (tar.gz)","sha256":"ea5c96309394a03a38828cc182058be0c09dde1f00f35809622c2d05c50ee890","date":"19 Mar 2020"},{"filename":"php-7.2.29.tar.xz","name":"PHP 7.2.29 (tar.xz)","sha256":"b117de74136bf4b439d663be9cf0c8e06a260c1f340f6b75ccadb609153a7fe8","date":"19 Mar 2020"}],"version":"7.2.29"}”,
“content_language”: “en”,
“content_type”: “application/json”,
“cookies”: {
“COUNTRY”: “NA%2C208.77.65.47”
},
“cookies_string”: “COUNTRY=NA%2C208.77.65.47”,
“date”: “Fri, 10 Apr 2020 18:05:04 GMT”,
“elapsed”: 0,
“etag”: “"myra-2a0383c2"”,
“expires”: “Fri, 10 Apr 2020 18:05:04 GMT”,
“failed”: false,
“json”: {
“announcement”: true,
“date”: “19 Mar 2020”,
“source”: [
{
“date”: “19 Mar 2020”,
“filename”: “php-7.2.29.tar.bz2”,
“name”: “PHP 7.2.29 (tar.bz2)”,
“sha256”: “eaa1f5503f2bf0c8569ec4ae80ffd8ca8cbc260f01c2503dd0e83dfc9cf0b923”
},
{
“date”: “19 Mar 2020”,
“filename”: “php-7.2.29.tar.gz”,
“name”: “PHP 7.2.29 (tar.gz)”,
“sha256”: “ea5c96309394a03a38828cc182058be0c09dde1f00f35809622c2d05c50ee890”
},
{
“date”: “19 Mar 2020”,
“filename”: “php-7.2.29.tar.xz”,
“name”: “PHP 7.2.29 (tar.xz)”,
“sha256”: “b117de74136bf4b439d663be9cf0c8e06a260c1f340f6b75ccadb609153a7fe8”
}
],
“tags”: [
“security”
],
“version”: “7.2.29”
},
“msg”: “OK (unknown bytes)”,
“redirected”: false,
“server”: “myracloud”,
“set_cookie”: “COUNTRY=NA%2C208.77.65.47; expires=Fri, 17-Apr-2020 18:05:04 GMT; Max-Age=604800; path=/; domain=.php.net”,
“status”: 200,
“transfer_encoding”: “chunked”,
“url”: “https://www.php.net/releases/?json&version=7.2”,
“x_frame_options”: “SAMEORIGIN”
}
}

PLAY RECAP *********************************************************************************************************************************************************************
127.0.0.1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

So, from that output we know that the ‘PHP_SITE_RESPONSE’ variable is being set and we have the variable structure to look at.

Your error from a few messages back just showed:
{“msg”: "The task includes an option with an undefined variable. The error was: ‘PHP_SITE_RESPONSE’ is undefined

I assume that was from one of these tasks:

- name: Write header
run_once: true
lineinfile: dest=/home/ncontu/test.txt create=yes line="LATEST VERSION PHP {{PHP_SITE_RESPONSE.json.version}} HTTPD {{HTTPD_SITE_VERSION.stdout_lines[0]VERSION.stdout_lines[0]}}"
delegate_to: 127.0.0.1

- name: Write to local disk
lineinfile: dest=/home/ncontu/test.txt insertafter=EOF create=yes line="{{ ansible_fqdn }} {{CURRENT_PHP_VERSION.stdout_lines[0]}} {{CURRENT_HTTPD_VERSI}} {{CURRENT_GIT_VERSION.stdout_lines[0]}}"
delegate_to: 127.0.0.1
when: CURRENT_PHP_VERSION.stdout_lines[0] != PHP_SITE_RESPONSE.json.version or CURRENT_HTTPD_VERSION.stdout_lines[0] != HTTPD_SITE_VERSION.stdout_lines[VERSION.stdout_lines[0] != GIT_SITE_VERSION.stdout_lines[0]

As a next step I’d add each of those tasks one at a time into the small playbook you just wrote/tested but change the tasks to further “debug” calls:

- hosts: "{{ variable_host }}"
serial: 1
tasks:
-
name: "Get PHP version from site"
register: PHP_SITE_RESPONSE
uri:
body_format: json
method: GET
return_content: true
url: "https://www.php.net/releases/?json&version=7.2"
run_once: true
delegate_to: 127.0.0.1

-
name: "Show PHP version from site"
debug:
var: PHP_SITE_RESPONSE

- name: DEBUG Write header
run_once: true
debug: |
lineinfile: dest=/home/ncontu/test.txt create=yes line="LATEST VERSION PHP {{PHP_SITE_RESPONSE.json.version}} HTTPD {{HTTPD_SITE_VERSION.stdout_lines[0]VERSION.stdout_lines[0]}}"
delegate_to: 127.0.0.1

I am sorry Dan,
I think I did not understand what you want to debug.
This playbook does not work and I can’t make it working.

Sorry about that.

Btw, the issue is that the variable is not available between plays in the same playbook. I just want to know how to pass those variable to the next play because it does not seem to be automatic.

Thanks

When you say “variable is not available between plays in the same playbook”, that is a different question than the one originally asked in this thread.

I’d suggest you post a very minimal playbook showing the error so we can reproduce it and assist you further with that problem.

You wrote “I think I did not understand what you want to debug.”

The playbook example was trying to “baby step” adding a couple more tasks in the play, and introduce a few additional variables. I might have confused you by using the “debug:” module.

The discussion has been moved to another problem after Dick’s suggestion, sorry for the confusion.
By the way I solved it using this : hostvars[‘localhost’][‘PHP_SITE_RESPONSE’].json.version

That’s the way to pass a variable to another play.