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
<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
<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
<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.
Il giorno ven 10 apr 2020 alle ore 16:32 Dick Visser <[email protected]>
ha scritto:
> 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.
>
> On Fri, 10 Apr 2020 at 15:48, Nicola Contu <[email protected]> wrote:
>
>> 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:
>> -
>> 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: "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"
>> run_once: true
>> register: HTTPD_SITE_VERSION
>> delegate_to: 127.0.0.1
>>
>> -
>> 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 -
>> run_once: true
>> register: GIT_SITE_VERSION
>> delegate_to: 127.0.0.1
>>
>> -
>> 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 PHP"
>> register: CURRENT_HTTPD_VERSION
>> shell: "httpd -v | grep 2.4 | tail -n 1 | tail -c 14 | head -c 6"
>> -
>> name: "Get current version PHP"
>> 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]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]
>>
>> - name: Cat produced file
>> command: /bin/cat /home/ncontu/test.txt
>> register: details
>> delegate_to: 127.0.0.1
>> run_once: true
>> when: inventory_hostname == ansible_play_hosts_all[-1]
>>
>> - name: Base64 produced file
>> command: base64 /home/ncontu/test.txt
>> register: basefile
>> delegate_to: 127.0.0.1
>> run_once: true
>> when: inventory_hostname == ansible_play_hosts_all[-1]
>>
>>
>> Any help would be much appreciated.
>>
>> Thanks
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/5b4d3382-6545-4213-a915-ea8f6e3d85b5%40googlegroups.com
>> <https://groups.google.com/d/msgid/ansible-project/5b4d3382-6545-4213-a915-ea8f6e3d85b5%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> Sent from a mobile device - please excuse the brevity, spelling and
> punctuation.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/YAiOo8cTtis/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAL8fbwPDaWvADiUa8r%2BN2kqYwa4md2QusnVv7kPv_bWSU_b5Ew%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAL8fbwPDaWvADiUa8r%2BN2kqYwa4md2QusnVv7kPv_bWSU_b5Ew%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAMTZZh2AwPjVZ%2ByjknvW6fU9ZJyCrpFYmWeNXePichdiNou9Eg%40mail.gmail.com.