Using set_fact along with do until group

40 views
Skip to first unread message

Naween Ghimire

unread,
Jul 17, 2015, 7:59:49 AM7/17/15
to ansible...@googlegroups.com
Hi I have a usecase where i have a string "dumb". During the execution of play i create a new string by using a random filter and this string should be any of ['duma','dumc','dumd'] but not "dumb". So i tried to use do until loop as follows:

- hosts: local
  connection: local
  gather_facts: false
  vars:
    old_string: 'dumb'

  tasks:
    - name: Generate random number
      set_fact: "new_string=dum{{ ['a','b','c','d'] |random }}"
      register: e
      until: e != old_string

    - name: Fail if old number and new number are same
      action: fail
      when: new_string == old_string

So as far as i am concerned the check at the until step should avoid allowing the new_string to be "dumb", but its not doing as expected. Any suggestions ?

Brian Coca

unread,
Jul 17, 2015, 11:54:15 AM7/17/15
to ansible...@googlegroups.com
try:
set_fact: "new_string=dum{{
(['a','b','c','d']|difference([old_string[-1]])) |random }}"

it should not need loop as you remove the old string ending from the
available list




--
Brian Coca
Reply all
Reply to author
Forward
0 new messages