0

On an Ansible playbook, I'm trying to execute a shell command only if a service exist on the remote server.

I have 3 tasks :

  • service_facts
  • execution of shell command if tomcat is installed
  • display the output of the shell command if tomcat is installed

Here is my code :

- name: Get Infos
 hosts: all
 gather_facts: yes
 become: false
 remote_user: [MY_USER]
 tasks:
 - name: Get the list of services
 service_facts:
 - name: Get version of Tomcat if installed
 become: true
 shell: 'java -cp /opt/tomcat/lib/catalina.jar org.apache.catalina.util.ServerInfo | grep "Server version"'
 register: tomcat_version
 when: "'tomcat.service' in services"
 - debug: msg="{{ tomcat_version.stdout_lines }}"
 when: "'tomcat.service' in services"

The problem is on certains servers the service name is, for example, tomcat-8.1

How can i use regex in the when condition?

I tried regex(), regex_search(), either I'm doing it wrong or I don't know how to do it.

Have you any idea how to do it?

Thanks in advance!

asked Feb 23, 2022 at 11:46

1 Answer 1

2

Count matching items. For example

 - service_facts:
 - block:
 - shell: smartctl --version | head -1
 register: smart_version
 - debug:
 msg: "{{ smart_version.stdout_lines }}"
 when: _srvcs|length > 0
 vars:
 _regex: '.*smart.*'
 _srvcs: "{{ services|select('match', _regex) }}"

gives

 msg:
 - smartctl 7.1 2019年12月30日 r5022 [x86_64-linux-5.4.0-73-generic] (local build)

The next option is to intersect the list of services, e.g.

 when: _srvcs|length > 0
 vars:
 my_services:
 - smartmontools.service
 - smart-8.1
 - smart-devel.0.0.1
 _srvcs: "{{ my_services|intersect(services) }}"

Debug

Q: "It gives me a failure on the server where my service doesn't exist, cause the playbook still tries to execute the shell. Is it normal?"

A: No. It is not normal. Print debug and find out why the condition evaluates to true, i.e. what service(s) match either the regex or the list. For example

 - debug:
 msg: |
 _srvcs:
 {{ _srvcs|to_nice_yaml|indent(2) }}
 when: debug|d(false)|bool
 vars:
 my_services:
 - smartmontools.service
 - smart-8.1
 - smart-devel.0.0.1
 _srvcs: "{{ my_services|intersect(services) }}"

gives

 msg: |-
 _srvcs:
 - smartmontools.service

To enable the task run the playbook with the option -e debug=true.

answered Feb 23, 2022 at 12:19
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply! With count matching items, it works, but it give me a failed on server where my service doesn't exist, cause the playbook still try to execute the shell. Is it normal?
You are welcome! Debug the task. I updated the answer. Then edit question and add the minimal reproducible example details for others to learn what was going wrong.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.