如何为ansible中的变量分配随机数?

时间:2015-02-12 23:15:18

标签: ansible ansible-playbook

这是一个ansible脚本,我期望打印出相同的随机数三次。相反,它打印出三个随机数。如何为ansible中的变量分配一个随机数,以便在整个剧本中修复它?

---
- 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

2 个答案:

答案 0 :(得分:17)

首先使用set_fact模块作为任务:

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

随后,debug: msg=...的值r已修复。

答案 1 :(得分:0)

在任务下设置事实:

$('.collapse').collapse();