ansible random moduleについて | www.yoshiislandblog.net

ansible random moduleについて

ansibleのrandomモジュールの計算についてあまり情報が無く、奮闘したのでメモ。

 

■3か4をランダムで出したい

# OK
"{{ 5 |random(3,1) }}"

# NG
"{{ 4 |random(3,1) }}"

 

■randomで出た結果を計算したい
ran1には5-10のランダムな数
ran2にはran1+30の数字を格納したい

 

(1)
これだと完璧

# OK
(ansible_work) ~/ansible_work/centos $ cat provision_vagrant.yml
- hosts: vagrant
become: true
user: vagrant
tasks:
- set_fact:
var1: "{{ 11 |random(5,1) }}"
var2: "{{ 11 |random(5,1) + 30 }}"
- debug:
msg:
- "{{ var1 }}"
- "{{ var2 }}"
(ansible_work) ~/ansible_work/centos $

# OK1 results
(ansible_work) ~/ansible_work/centos $ ansible-playbook -i hosts provision_vagrant.yml

PLAY [vagrant] *****************************************************************

TASK [setup] *******************************************************************
ok: [192.168.111.222]

TASK [set_fact] ****************************************************************
ok: [192.168.111.222]

TASK [debug] *******************************************************************
ok: [192.168.111.222] => {
"msg": [
"8",
"38"
]
}

PLAY RECAP *********************************************************************
192.168.111.222            : ok=4    changed=0    unreachable=0    failed=0

 

(2)
これだとvar1もvar2もランダムな数字に成ってしまう

# NG1
(ansible_work) ~/ansible_work/centos $ cat provision_vagrant.yml
- hosts: vagrant
become: true
user: vagrant
tasks:
- set_fact:
var1: "{{ 11 |random(5,1) }}"
- set_fact:
var2: "{{ 11 |random(5,1) + 30 }}"
- debug:
msg:
- "{{ var1 }}"
- "{{ var2 }}"
(ansible_work) ~/ansible_work/centos $

# NG1 results
(ansible_work) ~/ansible_work/centos $ ansible-playbook -i hosts provision_vagrant.yml

PLAY [vagrant] *****************************************************************

TASK [setup] *******************************************************************
ok: [192.168.111.222]

TASK [set_fact] ****************************************************************
ok: [192.168.111.222]

TASK [set_fact] ****************************************************************
ok: [192.168.111.222]

TASK [debug] *******************************************************************
ok: [192.168.111.222] => {
"msg": [
"9",
"40"
]
}

PLAY RECAP *********************************************************************
192.168.111.222            : ok=5    changed=0    unreachable=0    failed=0

(ansible_work) ~/ansible_work/centos $

 

(3) この後は完全NG集

# NG2
(ansible_work) ~/ansible_work/centos $ cat provision_vagrant.yml
- hosts: vagrant
become: true
user: vagrant
tasks:
- name: var1
set_fact:
var1: "{{ 11 |random(5,1) }}"
- name: var2
set_fact:
var2: "{{ var1 + 30 }}"
- debug:
msg:
- "{{ var1 }}"
- "{{ var2 }}"
(ansible_work) ~/ansible_work/centos $

# NG2 results
(ansible_work) ~/ansible_work/centos $ ansible-playbook -i hosts provision_vagrant.yml

PLAY [vagrant] *****************************************************************

TASK [setup] *******************************************************************
ok: [192.168.111.222]

TASK [var1] ********************************************************************
ok: [192.168.111.222]

TASK [var2] ********************************************************************
fatal: [192.168.111.222]: FAILED! => {"failed": true, "msg": "Unexpected templating type error occurred on ({{ var1 + 30 }}): coercing to Unicode: need string or buffer, int found"}

NO MORE HOSTS LEFT *************************************************************
[WARNING]: Could not create retry file 'provision_vagrant.retry'.
[Errno 2] No such file or directory: ''

PLAY RECAP *********************************************************************
192.168.111.222            : ok=3    changed=0    unreachable=0    failed=1

(ansible_work) ~/ansible_work/centos $

 

# NG3
(ansible_work) ~/ansible_work/centos $ cat provision_vagrant.yml
- hosts: vagrant
become: true
user: vagrant
tasks:
- set_fact:
var1: "{{ 11 |random(5,1) }}"
var2: "{{ var1 + 30 }}"
- debug:
msg:
- "{{ var1 }}"
- "{{ var2 }}"
(ansible_work) ~/ansible_work/centos $

# NG3 results
(ansible_work) ~/ansible_work/centos $ ansible-playbook -i hosts provision_vagrant.yml

PLAY [vagrant] *****************************************************************

TASK [setup] *******************************************************************
ok: [192.168.111.222]

TASK [set_fact] ****************************************************************
fatal: [192.168.111.222]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'var1' is undefined\n\nThe error appears to have been in 'xxx/provision_vagrant.yml': line xx, column x, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:xxx"}

NO MORE HOSTS LEFT *************************************************************
[WARNING]: Could not create retry file 'provision_vagrant.retry'.
[Errno 2] No such file or directory: ''

PLAY RECAP *********************************************************************
192.168.111.222            : ok=2    changed=0    unreachable=0    failed=1

(ansible_work) ~/ansible_work/centos $

 

# NG4
(ansible_work) ~/ansible_work/centos $ cat provision_vagrant.yml
- hosts: vagrant
become: true
user: vagrant
tasks:
- set_fact:
var1: "{{ 11 |random(5,1) }}"
- debug:
msg:
- "{{ var1 }}"
- "{{ var2 + 30 }}"
(ansible_work) ~/ansible_work/centos $

# NG4 results
(ansible_work) ~/ansible_work/centos $ ansible-playbook -i hosts provision_vagrant.yml

PLAY [vagrant] *****************************************************************

TASK [setup] *******************************************************************
ok: [192.168.111.222]

TASK [set_fact] ****************************************************************
ok: [192.168.111.222]

TASK [debug] *******************************************************************
fatal: [192.168.111.222]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'var2' is undefined\n\nThe error appears to have been in 'xxx/provision_vagrant.yml': line xx, column x, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:xxx"}

NO MORE HOSTS LEFT *************************************************************
[WARNING]: Could not create retry file 'provision_vagrant.retry'.
[Errno 2] No such file or directory: ''

PLAY RECAP *********************************************************************
192.168.111.222            : ok=3    changed=0    unreachable=0    failed=1

(ansible_work) ~/ansible_work/centos $
%d bloggers like this: