run_once not working to register value of curl

182 views
Skip to first unread message

Nicola Contu

unread,
Apr 10, 2020, 9:48:06 AM4/10/20
to Ansible Project
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
      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

Dick Visser

unread,
Apr 10, 2020, 10:32:08 AM4/10/20
to ansible...@googlegroups.com
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. 

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5b4d3382-6545-4213-a915-ea8f6e3d85b5%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Kai Stian Olstad

unread,
Apr 10, 2020, 11:42:52 AM4/10/20
to ansible...@googlegroups.com
On Fri, Apr 10, 2020 at 06:48:05AM -0700, Nicola Contu 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.

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.

--
Kai Stian Olstad

Nicola Contu

unread,
Apr 10, 2020, 12:01:23 PM4/10/20
to ansible...@googlegroups.com
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.

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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwPDaWvADiUa8r%2BN2kqYwa4md2QusnVv7kPv_bWSU_b5Ew%40mail.gmail.com.

Dan Linder

unread,
Apr 10, 2020, 1:38:16 PM4/10/20
to Ansible Project
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
      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
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
--
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 ansible...@googlegroups.com.

Nicola Contu

unread,
Apr 10, 2020, 2:06:08 PM4/10/20
to ansible...@googlegroups.com
[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

To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7e4959e3-498f-4929-a1d5-2e1c4f3dfa40%40googlegroups.com.

Dan Linder

unread,
Apr 10, 2020, 5:40:43 PM4/10/20
to Ansible Project
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

```
To unsubscribe from this group and all its topics, send an email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7e4959e3-498f-4929-a1d5-2e1c4f3dfa40%40googlegroups.com.

Nicola Contu

unread,
Apr 15, 2020, 3:43:51 AM4/15/20
to ansible...@googlegroups.com
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

To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a01c8f0b-6ff9-45ad-9422-a3b819997d17%40googlegroups.com.

Dan Linder

unread,
Apr 15, 2020, 8:14:07 PM4/15/20
to Ansible Project
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.
To unsubscribe from this group and all its topics, send an email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a01c8f0b-6ff9-45ad-9422-a3b819997d17%40googlegroups.com.

Nicola Contu

unread,
Apr 16, 2020, 3:04:37 AM4/16/20
to ansible...@googlegroups.com
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.

To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/dcf9c04d-86c0-49f2-b6b6-275298223c59%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages