Sunday, May 28, 2017

generate random odd/even number with ansible

ansible random module can control start, end and steps when generate random number.

1. make odd and even random number

code here.
% cat random_test.yml
- hosts: all
  become: true
  user: vagrant
  tasks:
  - set_fact:
      var1: "{{ 100 |random(1,2) }}" # odd random number 1-100 
      var2: "{{ 100 |random(2,2) }}" # even random number 2-100
  - debug:
      msg:
        - "odd number is {{ var1 }}"
        - "even number is {{ var2 }}"
generate numbers successfully.
% ansible-playbook -i hosts random_test.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.111.223]
ok: [192.168.111.224]

TASK [set_fact] ****************************************************************
ok: [192.168.111.224]
ok: [192.168.111.223]

TASK [debug] *******************************************************************
ok: [192.168.111.224] => {
    "msg": [
        "odd number is 33",
        "even number is 80"
    ]
}
ok: [192.168.111.223] => {
    "msg": [
        "odd number is 1",
        "even number is 72"
    ]
}

PLAY RECAP *********************************************************************
192.168.111.223            : ok=3    changed=0    unreachable=0    failed=0
192.168.111.224            : ok=3    changed=0    unreachable=0    failed=0