« ^ »

Playbook実行時に変数の入力をさせるvars_prompt

所要時間: 約 1分

AnsibleのPlaybookを作成して実行していると実行前になんらかの変数を入力させたいことがある。 vars_prompt を使うと実現できる。

- name: Test
  hosts: localhost
  vars_prompt:
    - name: "input_var"
      prompt: "INPUT: "
      confirm: no
      private: no
      default: "fish"
  tasks:
    - debug: msg="{{ input_var }}"

試しにbeefを指定する。

.venv/bin/ansible-playbook playbook.yml
 [WARNING]: Host file not found: /etc/ansible/hosts

 [WARNING]: provided hosts list is empty, only localhost is available

INPUT:  [fish]: beef
 _____________
< PLAY [Test] >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 ______________
< TASK [setup] >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [localhost]
 ______________
< TASK [debug] >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [localhost] => {
    "msg": "beef"
}
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

localhost                  : ok=2    changed=0    unreachable=0    failed=0

指定せずにENTERを押すとデフォルト値が使われる。

```

.venv/bin/ansible-playbook playbook.yml
 [WARNING]: Host file not found: /etc/ansible/hosts

 [WARNING]: provided hosts list is empty, only localhost is available

INPUT:  [fish]:
 _____________
< PLAY [Test] >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 ______________
< TASK [setup] >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [localhost]
 ______________
< TASK [debug] >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [localhost] => {
    "msg": "fish"
}
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

localhost                  : ok=2    changed=0    unreachable=0    failed=0