Let's say I have the following example, storing all git config
values in an Ansible variable:
- shell: git config --global --list
register: git_config_list
Ansible stores the result of this command in the git_config_list
variable, and one of the items is stdout_lines
, containing the output of the command in an array of entries, e.g.
[
"user.name=Foo Bar",
"user.email=foo@example.com"
]
How can I check whether a certain value is already set, e.g. for verifying that user.name
has a value?
Is there a way to call something like contains
on the array, combined with a regular expression, allowing me to find the value I'm looking for? Or do I have to loop over the stdout_lines
entries to find what I'm looking for?
An example on how to do something like this would be appreciated.
In theory this should be possible by combining the filters
match
andselect
. The latter returns only those list elements which pass another filter. Then you could test for the length of the result.In theory. I just tested it and I can't get it to work. In general the
select
(as well as thereject
) filter returns a string like<generator object _select_or_reject at 0x10531bc80>
even with simple filters like the example from the docs withodd
. Wasn't able to find a solution yet. Maybe you have more success.Though you could simply
join
your list to a string and then search in the string withmatch
. While it's ugly, it works.With select and match (extend answer of udondan):
Simple python
in
would do just fine, NOTE I usestdout
instead ofstdout_lines
:All in all
ansible
is horrible for programming. Try to do as much as you can outside the playbook and write only the orchestration logic inside the playbook. Here are a few examples how you can do it using--get
option ofgit
.