How to assign a random number to a variable inaccessible?

This is an indispensable script that I expected to print the same random number three times. Instead, he prints three random numbers. How to assign a random number to a variable inaccessible so that it is fixed throughout the play?

--- - name: Test random filter hosts: localhost gather_facts: False vars: random_number: "{{ 100 | random }}" tasks: - name: Print the random number debug: var=random_number - name: Print the random number debug: var=random_number - name: Print the random number debug: var=random_number 
+12
ansible ansible-playbook
source share
2 answers

Just use the set_fact module set_fact as a task:

  - set_fact: r: "{{ 100 | random }}" run_once: yes 

Subsequently, debug: msg=... fixed the value of r .

+19
source share

Set the facts in the task:

 --- - name: Test random filter hosts: localhost gather_facts: False tasks: - name: set fact here set_fact: randome_number: "{{ 100 | random }}" run_once: yes - name: Print the random number debug: var=random_number - name: Print the random number debug: var=random_number - name: Print the random number debug: var=random_number 
0
source share

All Articles