'ansible match exact string from register result
I have below ansible tasks which compares list of packages against register output obtained from yum module.
- hosts: localhost
vars:
pkgs: [ 'git', 'rsync', 'python36' ]
tasks:
- name: stats for installed pkgs
yum:
state: installed
register: installed
- name: Compare against register
set_fact:
unwanted_pkgs: >-
[
{% for value in installed.results %}
{% for pkg in pkgs %}
{% if value.name | regex_search(pkg) %}
" {{ value.name }}",
{% endif %}
{% endfor %}
{% endfor %}
]
- name: print
debug:
msg: "{{ unwanted_pkgs }}"
Upon executing above playbook, debug task print output as :
git
git-core
git-core-doc
python36
python36-devel
While executing the task it prints additional packages (eg: git-core, git-core-doc and python36-devel which I have not defined in pkgs variable). How this can be avoided ?
Solution 1:[1]
Is this what you want?
- debug:
msg: |-
{% for value in installed.results %}
{% for pkg in pkgs %}
{% if value.name == pkg %}
{{ value.name }}
{% endif %}
{% endfor %}
{% endfor %}
gives
msg: |-
git
python36
The same can be achieved by intersecting the lists
unwanted_pkgs: "{{ installed.results|map(attribute='name')|intersect(pkgs) }}"
gives
unwanted_pkgs:
- git
- python36
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vladimir Botka |
