1:
Hello
Is it possible to search in when condition two strings from single line by matching the registered output.
For example:
the registered output consists of services and its status
1: httpd disabled
2: tftp enabled
I want to search the line and use it in when condition.
What is your use case?
Could you tell us more what you are trying to achieve?
vbotka
(Vladimir Botka)
June 8, 2020, 9:12pm
3
For example
- debug:
var: item
loop: "{{ output.stdout_lines }}"
when:
- "'httpd' in item"
- "'disabled' in item"
should give
ok: [localhost] => (item=1: httpd disabled) => {
"ansible_loop_var": "item",
"item": "1: httpd disabled"
}
skipping: [localhost] => (item=2: tftp enabled)
Hello @all
Let me elaborate the question
I have 20 services to check and I would like to execute shell module one time and register its output and match the service name and its status from output lines.
for ex: I dont want to use grep in shell module for service name I want to get the status from registered output by matching the lines
shell: systemctl list-unit-files --all --state=disabled,enabled,unknown --no-legend --no-pager
register: _service_status
I want to verify the service name and status with when condition from the _service_status.stdout lines
ex:
comment: “tftp service is in enabled”
when: _service_name _expected_state in service_status
comment: “httpd service is in disabled”
when: _service_name _current_state in service_status
variables listed is mentioned in defaults/vars:
service_name: tftp
expected_state: enable
service_name: httpd
current: disable
OK… and what would you DO with this information…?
vbotka
(Vladimir Botka)
June 9, 2020, 5:51am
6
Nothing. Why are you asking?
I meant what would the topic starter do with this list of services.
vbotka
(Vladimir Botka)
June 9, 2020, 8:07am
8
For example
- hosts: localhost
vars:
test_units:
- unit: cups.service
state: enabled
- unit: nfs-server.service
state: enabled
- unit: zfs-mount.service
state: enabled
tasks:
- command: systemctl list-unit-files --all
--state=disabled,enabled,unknown
--no-legend --no-pager
register: status
- set_fact:
my_units: "{{ my_units|default({})|
combine({my_unit: {'state': my_state,
'preset': my_preset}}) }}"
loop: "{{ status.stdout_lines }}"
vars:
my_list: "{{ item.split() }}"
my_unit: "{{ my_list.0 }}"
my_state: "{{ my_list.1 }}"
my_preset: "{{ my_list.2 }}"
- debug:
msg: "{{ item.unit }} is
{{ (my_units[item.unit].state == item.state)|
ternary('', 'NOT') }}
{{ item.state }}"
loop: "{{ test_units }}"
gives
"msg": "cups.service is enabled"
"msg": "nfs-server.service is NOT enabled"
"msg": "zfs-mount.service is enabled"
HTH,
-vlado