when condition on a variable starting with a string value

440 views
Skip to first unread message

fanvalt

unread,
Feb 7, 2017, 8:45:44 AM2/7/17
to Ansible Project
Hello,

Actually I wrote my playbook this way to set values id the dual variable is the string value SRV:

   - set_fact:
      distrib: "{{ distribsrv }}"
      Directoryname: "{{ Directorysrv }}"
     when: dual == 'SRV'

I would like to modify the when condition to run it when dual == 'SRV1', "SRV2' ... etc, is there a way to do a when condition : when : dual like 'SRV*' ?

fanvalt

unread,
Feb 7, 2017, 9:53:30 AM2/7/17
to Ansible Project
I wrote this:
  vars:
   services:
     - SRV
     - SRV[1:99]

   - set_fact:
      distrib: "{{ distribsrv }}"
     when: dual in services

but dual is SRV2 and the set_fact module has been ignored

fanvalt

unread,
Feb 7, 2017, 10:32:17 AM2/7/17
to Ansible Project
if I write this, i do not get a variable list:
   - set_fact:
       services: "{{ services + item }}"
     with_sequence: count=99
   - debug: msg="sequence {{ services }}"

ok: [SRV1] => {
    "msg": "sequence SRV123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899"
}

the result expected is SRV1, SRV2, SRV3 ....

Le mardi 7 février 2017 14:45:44 UTC+1, fanvalt a écrit :

fanvalt

unread,
Feb 7, 2017, 10:34:39 AM2/7/17
to Ansible Project
My issue is: I would like to run a task when a variable start with 'SRV' (srv1, SRV2 ...) ad I don't know how to write the condition: when: dual like 'SRV*'


Le mardi 7 février 2017 14:45:44 UTC+1, fanvalt a écrit :

Stankovic, Marko

unread,
Feb 7, 2017, 10:48:38 AM2/7/17
to Ansible Project
I know this is weird, but can't think of something better:

- set_fact:
distrib: "{{ distribsrv }}"
Directoryname: "{{ Directorysrv }}"
when: dual | regex_replace( '^SRV.*', 'True') | bool

Cheers,
Marko
CONFIDENTIALITY NOTICE: This message is the property of International Game Technology PLC and/or its subsidiaries and may contain proprietary, confidential or trade secret information. This message is intended solely for the use of the addressee. If you are not the intended recipient and have received this message in error, please delete this message from your system. Any unauthorized reading, distribution, copying, or other use of this message or its attachments is strictly prohibited.

fanvalt

unread,
Feb 7, 2017, 10:52:33 AM2/7/17
to Ansible Project
thank you I will have a look at your solution, I finally found this way to proceed otherwise:
   - set_fact:
       services: "{{ services }} + SRV{{item }}"
     with_sequence: count=5

   - set_fact:
      distrib: "{{ distribsrv }}"
     when: dual in services

TASK [debug] *******************************************************************
ok: [SRV1] => {
    "msg": "sequence SRV + SRV1 + SRV2 + SRV3 + SRV4 + SRV5"
}


Le mardi 7 février 2017 14:45:44 UTC+1, fanvalt a écrit :

fanvalt

unread,
Feb 7, 2017, 11:07:41 AM2/7/17
to Ansible Project
Thanks a lot Marko, I used your syntax:
  dual|regex_replace('^SRV(.*)', 'True')


Le mardi 7 février 2017 14:45:44 UTC+1, fanvalt a écrit :

Johannes Kastl

unread,
Feb 8, 2017, 3:15:10 AM2/8/17
to ansible...@googlegroups.com
On 07.02.17 17:07 fanvalt wrote:
> Thanks a lot Marko, I used your syntax:
> dual|regex_replace('^SRV(.*)', 'True')

Did you try the following?

> No need to use regex for pattern searching. You can use search like
> this:
>
> when: name_prefix | search("stage-dbs")

from
https://stackoverflow.com/questions/34429842/how-to-evaluate-a-when-condition-for-ansible-task

In your case this should be:

when: dual | search("SRV")

Johannes

signature.asc
Reply all
Reply to author
Forward
0 new messages