# Extract certbot using regex

**URL:** <https://forum.ansible.com/t/extract-certbot-using-regex/3403>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [January 20, 2024, 12:41pm UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403 "2024-01-20T12:41:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wolkjuh](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wolkjuh/32/2039_2.png) [@Wolkjuh](https://forum.ansible.com/u/Wolkjuh)\
**Post date:** [January 20, 2024, 12:41pm UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/1 "2024-01-20T12:41:25Z")

</div>

Hello,  
I have a question about getting a certbot version using regex.  
I got sent from the letsencrypt forum to this one.  
We think our ansible command is outdated, since I get this error

I replaced the personal data with {}

```auto
Check if the latest version of Certbot is already installed...
  {server} failed | msg: [Errno 2] No such file or directory: b'certbot'
Extract installed Certbot version using regex...
  {server} failed | msg: The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'

The error appears to be in '/home/{me}/ansible/{directory}/{company}.webapp/tasks/7.5-letsencrypt.yml': line 70, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Extract installed Certbot version using regex
  ^ here

```

Now this is the ansible command we use

```auto
- name: Extract installed Certbot version using regex
  set_fact:
    installed_certbot_version_extracted: "{{ installed_certbot_version.stdout | regex_search('(?<=certbot )\\d+\\.\\d+\\.\\d+') }}"
  when: webapp.use_letsencrypt | default(False) | bool

```

A colleague of mine have not updated his system and stuff and everything is working fine at his instance, but when it comes to mine I get this error.  
He only gets the ` {server} failed | msg: [Errno 2] No such file or directory: b'certbot'` but its continuing after that.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 20, 2024, 2:04pm UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/2 "2024-01-20T14:04:43Z")

</div>

Is the `No such file or directory: b'certbot'` error a result of `certbot` not being installed?

If I needed to check the version I’d do something like this:

```yaml
---
- name: Get the certbot version
  block:

    - name: Run certbot --version
      ansible.builtin.command: certbot --version
      check_mode: false
      changed_when: false
      register: certbot_version_command

    - name: Set a fact for the certbot version
      ansible.builtin.set_fact:
        certbot_version: "{{ certbot_version_command.stdout | ansible.builtin.regex_replace('^certbot') | trim }}"
        certbot_semver: "{{ certbot_version_command.stdout | ansible.builtin.regex_replace('^certbot') | trim | community.general.jc('semver') }}"

    - name: Print the certbot version
      ansible.builtin.debug:
        var: certbot_version

    - name: Print the certbot semver
      ansible.builtin.debug:
        var: certbot_semver

  tags:
    - certbot
...

```

This results in:

```auto

TASK [certbot : Run certbot --version] ************************************************************************************************
ok: [localhost]

TASK [certbot : Set a fact for the certbot version] ***********************************************************************************
ok: [localhost]

TASK [certbot : Print the certbot version] ********************************************************************************************
ok: [localhost] => 
    certbot_version: 2.1.0

TASK [certbot : Print the certbot semver] *********************************************************************************************
ok: [localhost] => 
    certbot_semver:
        build: null
        major: 2
        minor: 1
        patch: 0
        prerelease: null

PLAY RECAP ****************************************************************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   

```

---

<div class="post-metadata">

**Author:** ![Wolkjuh](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wolkjuh/32/2039_2.png) [@Wolkjuh](https://forum.ansible.com/u/Wolkjuh)\
**Post date:** [January 22, 2024, 8:26am UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/3 "2024-01-22T08:26:22Z")

</div>

Hi Chris!

Certbot is not installed, but he still should continue right?  
This is our full certbot check code

```auto
- name: Get latest Certbot release information
  uri:
    url: https://api.github.com/repos/certbot/certbot/releases/latest
    return_content: yes
  register: latest_certbot_release_info
  changed_when: False
  when: webapp.use_letsencrypt | default(False) | bool

- name: Set latest_certbot_version variable
  set_fact:
    latest_certbot_version: "{{ (latest_certbot_release_info.content | from_json).tag_name.replace('v', '') }}"
  when: webapp.use_letsencrypt | default(False) | bool

- name: Check if the latest version of Certbot is already installed
  command: certbot --version
  register: installed_certbot_version
  changed_when: False
  ignore_errors: True
  when: webapp.use_letsencrypt | default(False) | bool

- name: Extract installed Certbot version using regex
  set_fact:
    installed_certbot_version_extracted: "{{ installed_certbot_version.stdout | regex_search('(?<=certbot )\\d+\\.\\d+\\.\\d+') }}"
  when: webapp.use_letsencrypt | default(False) | bool

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 22, 2024, 9:18am UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/4 "2024-01-22T09:18:54Z")

</div>

You could add an additional `when` condition to the `set_fact` task to ensure that it is only run when `certbot` is installed, for example by checking the return code of the version check?

```yaml
  when:
    - installed_certbot_version.rc == 0
    - webapp.use_letsencrypt | default(False) | bool

```

---

<div class="post-metadata">

**Author:** ![Wolkjuh](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wolkjuh/32/2039_2.png) [@Wolkjuh](https://forum.ansible.com/u/Wolkjuh)\
**Post date:** [January 22, 2024, 9:35am UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/5 "2024-01-22T09:35:32Z")

</div>

Looking good, thank you very much for your help and fast responses!

---

<div class="post-metadata">

**Author:** ![Wolkjuh](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wolkjuh/32/2039_2.png) [@Wolkjuh](https://forum.ansible.com/u/Wolkjuh)\
**Post date:** [January 26, 2024, 3:46pm UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/6 "2024-01-26T15:46:20Z")

</div>

Hello chris!

I am a step further but I get a weird message now, does this have something to do with the previous step?

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/b/bcb60930c1bccbb5fc977af6d269f75cd9efe807.png)

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 26, 2024, 3:51pm UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/7 "2024-01-26T15:51:56Z")

</div>

Is `certbot` installed on the server in question?

---

<div class="post-metadata">

**Author:** ![Wolkjuh](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wolkjuh/32/2039_2.png) [@Wolkjuh](https://forum.ansible.com/u/Wolkjuh)\
**Post date:** [January 26, 2024, 3:56pm UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/8 "2024-01-26T15:56:07Z")

</div>

No it is not. But my collegue did this on the previous server on his machine without certbot installed and it worked

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 26, 2024, 4:06pm UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/9 "2024-01-26T16:06:10Z")

</div>

Why do you think a task that uses `certbot` should work when `certbot` is not installed?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 25, 2024, 4:06pm UTC](https://forum.ansible.com/t/extract-certbot-using-regex/3403/10 "2024-02-25T16:06:53Z")

</div>

Closed on February 25, 2024, 4:06pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
