Stas Fedoseev
Stas Fedoseev

Reputation: 11

How to generate a random number from the range 1-500 excluding numbers from the file?

Im try this one but always have problem with var my_list - i cant use path to file.

  vars:
    my_list: "{{ lookup('file', 'id.txt') }}"
  tasks:
- name: run shell to get random number
  shell: exit `shuf -i 1-500 -n 1` 
  register: shell_command
  failed_when: shell_command.rc > 500
  until: shell_command.rc not in my_list
  retries: 1000
  delay: 1
  delegate_to: localhost


- name: print results
  debug:
    var: shell_command.rc

how to do this in ansible? the contents of the id.txt file are below

349
104
182
111
180
196

Upvotes: 1

Views: 188

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68144

For example,

    - debug:
        msg: "{{ _range|difference(my_list)|random }}"
      vars:
        my_list: "{{ lookup('file', 'id.txt').splitlines()|map('int')|list }}"
        _range: "{{ range(1, 501) }}"

Upvotes: 1

Related Questions