select/reject filter parameters

107 views
Skip to first unread message

Dimitri Yioulos

unread,
May 3, 2021, 5:15:14 PM5/3/21
to Ansible Project
In for example, the following:

{{ hostvars[host]['java_one']['stderr_lines'] | reject('contains', 'OpenJDK') }}
or
{{ hostvars[host]['java_one']['stderr_lines'] | select('contains', 'Java(TM)') }}

how can i add a second value to select or reject?  I want the action to take place if "OpenJDK" OR "Java(TM)'" is found.

Vladimir Botka

unread,
May 3, 2021, 7:21:28 PM5/3/21
to Dimitri Yioulos, ansible...@googlegroups.com
Instead of "Testing if a list contains a value"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-if-a-list-contains-a-value
you might want to use "Testing strings"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings

For example, use *search* and test the list is not empty to take
action if "OpenJDK" OR "Java(TM)" is found

when: list_of_lines|
select('search', 'OpenJDK|Java\(TM\)')|
list|
length > 0

--
Vladimir Botka

Dimitri Yioulos

unread,
May 4, 2021, 5:28:13 AM5/4/21
to Vladimir Botka, ansible...@googlegroups.com
Ok.  Here's the entire playbook:

---

- hosts: all
  gather_facts: false
  become: yes
  become_user: root
  become_method: su

  tasks:
    - name: list java version
      command: java -version
      register: java_one

    - name: print output
      debug:
        msg: "{{ java_one.stderr }}"
    - local_action:
        module: copy
        content: |
          {% for host in ansible_play_hosts %}
          {{ host }}
          {{ hostvars[host]['java_one']['stderr'] | select('contains', 'Java(TM)') }}

          {% endfor %}
        dest: "/Users/dyioulos/Documents/prodjava.txt"
      run_once: yes

Assuming I remove " | select('contains', 'Java(TM)')", where would I plug in the code you recommend?

Vladimir Botka

unread,
May 4, 2021, 6:07:00 AM5/4/21
to Dimitri Yioulos, ansible...@googlegroups.com
On Tue, 4 May 2021 12:05:36 +0200
Vladimir Botka <vbo...@gmail.com> wrote:

On Tue, 4 May 2021 05:27:53 -0400
Dimitri Yioulos <dimitri...@endurance.com> wrote:

> - local_action:
> module: copy
> content: |
> {% for host in ansible_play_hosts %}
> {{ host }}
> {{ hostvars[host]['java_one']['stderr'] | select('contains',
> 'Java(TM)') }}
>
> {% endfor %}
> dest: "/Users/dyioulos/Documents/prodjava.txt"
> run_once: yes

What content of prodjava.txt do you expect?

--
Vladimir Botka

Dimitri Yioulos

unread,
May 4, 2021, 7:00:26 AM5/4/21
to Ansible Project
This:

['java version "1.7.0_71"', 'Java(TM) SE Runtime Environment (build 1.7.0_71-b14)', 'Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)']

['java version "1.7.0_71"', 'Java(TM) SE Runtime Environment (build 1.7.0_71-b14)', 'Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)']

Vladimir Botka

unread,
May 4, 2021, 7:15:33 AM5/4/21
to Dimitri Yioulos, ansible...@googlegroups.com
On Tue, 4 May 2021 04:00:25 -0700 (PDT)
Dimitri Yioulos <dimitri...@endurance.com> wrote:

> This:
>
> myserver1.mydomain.com
> ['java version "1.7.0_71"', 'Java(TM) SE Runtime Environment (build
> 1.7.0_71-b14)', 'Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed
> mode)']
>
> myserver2.mydomain.com
> ['java version "1.7.0_71"', 'Java(TM) SE Runtime Environment (build
> 1.7.0_71-b14)', 'Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed
> mode)']

Try this

content: |
{% for host in ansible_play_hosts %}
{% set present = hostvars[host].java_one.stderr_lines|
select('search', 'OpenJDK|Java\(TM\)')|
list|length > 0 %}
{{ host }}
{% if present %}{{ hostvars[host].java_one.stderr_lines
}}{% endif %}

{% endfor %}

For details see https://jinja.palletsprojects.com/en/master/

--
Vladimir Botka

Dimitri Yioulos

unread,
May 4, 2021, 7:34:29 AM5/4/21
to Vladimir Botka, ansible...@googlegroups.com
OK, almost got this!  And, I probably didn't explain what I'm after very well.  Based on the code you just provided, this is the output, e.g.:

somehost.mydomain.com
['java version "1.7.0_65"', 'OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)', 'OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)']

anotherhost.mydomain.com

['java version "1.7.0_71"', 'Java(TM) SE Runtime Environment (build 1.7.0_71-b14)', 'Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)']

However, I only want records containing "Java(TM)" returned.  How do I adjust the code to do that?

Dimitri Yioulos

unread,
May 4, 2021, 7:40:17 AM5/4/21
to Vladimir Botka, ansible...@googlegroups.com
Sorry, I'm being messy here.  If i try this, "select('search', '!OpenJDK|Java\(TM\)')|", I get output like this:


anotherhost.mydomain.com
['java version "1.7.0_71"', 'Java(TM) SE Runtime Environment (build 1.7.0_71-b14)', 'Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)']
Reply all
Reply to author
Forward
0 new messages