can we pass a wildcard in a module's parameter's value ?

29 views
Skip to first unread message

Nick-H

unread,
Oct 7, 2020, 5:15:05 PM10/7/20
to Ansible Project
hi,  again a very simple use case in appearance but that is driving me nuts to realize with ansible, anyone ever done : 
tasks:
- name: run a module
  any_module:
    users: {{item}} # I want to loop first here 
    state: absent
    name: "prefixed-*" # basically anything that starts with 'prefixed'

I tried : 
- as above just using the wildcard symbol , doesn't work 
Things that comes to mind :
- use startswith , but can't find the syntax that would fit here
- use another loop, but that tales me to the slope of nested_loops , something I was NEVER able to make it working. 

any simple possibility to just use wildcard in a string ? 

thanks 

 

Matt Martz

unread,
Oct 7, 2020, 5:20:49 PM10/7/20
to ansible...@googlegroups.com
I have no idea what your goal is, and without giving a concrete example with an actual module I cannot say.

You want to loop some list variable, but limit to only values that start with prefixed- from that list?

loop: "{{ something|select('match', 'prefixed-.*')|list }}"

Or you could do:

loop: "{{ something }}'
when: item.startswith('prefixed-')

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2b7ce081-b637-43f0-875b-3fb1202501c2n%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Nick-H

unread,
Oct 7, 2020, 5:50:31 PM10/7/20
to Ansible Project
thanks Matt,
here is the real example , trying to remove multiple IAM policies from a bunch of users

- name: assign policies to a given user
    iam_policy:
      iam_type: user
      iam_name: "{{ item }}"
      state: absent
      policy_name: "{{ starts_with('database_access-(ro|rw)') }}" ## here I need to wildcard to any policy starting with that string . 
     # policy_name:   "database_access-(ro | rw)"   # the other possibility is to find a switch like here so it can either do database_access-ro OR database_access_rw , regex maybe ? 
  
     loop: "{{ my_users }}" 

As I already have one loop for users , I was trying to find a filter that I could directly pass in the 'policy_name' parameter . 

Nick-H

unread,
Oct 7, 2020, 6:53:24 PM10/7/20
to Ansible Project
I am not quite sure what {{something}} was supposed to be  here ? 

Nick-H

unread,
Oct 8, 2020, 1:22:06 AM10/8/20
to Ansible Project
been reading most of ansible docbut can't find anything related to this,
 Can't we just use regular expression for passing a string as a parameter ? 

It looks pretty simple case :

a_module:
 param1:  something_well_defined
 param2:  regex(something_not_so_well_defined.*) 

Dick Visser

unread,
Oct 8, 2020, 2:38:27 AM10/8/20
to ansible...@googlegroups.com
Parameters can be many things: strings, lists, dicts, boolenas etc.
A generic "wild card" or regex therefore doesn't mean much.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/74d709a8-dc83-414e-9352-1a156eae9959n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Stefan Hornburg (Racke)

unread,
Oct 8, 2020, 2:42:32 AM10/8/20
to ansible...@googlegroups.com
On 10/8/20 8:37 AM, Dick Visser wrote:
> Parameters can be many things: strings, lists, dicts, boolenas etc.
> A generic "wild card" or regex therefore doesn't mean much.

The module needs to support the type, so in general a wildcard would only work if the module explicitly supports it.

Regards
Racke
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Reply all
Reply to author
Forward
Message has been deleted
0 new messages