I want to install MariaDB after confirming user. I have a role and one simple task:
- name: install MariaDB
yum:
name: MariaDB
state: present
or if I want to use include: MySQL.yml
, if the user wants to, this line executes, if not skip this include.
- name: install MariaDB
yum:
name: MariaDB
state: present
- include: MySQL.yml
more explain
my hosts:
[dbs]
192.168.0.10
192.168.0.11
192.168.0.12
Now, I want if the user enters no for answer prompt, MySQL.yml does not execute for any server.
my code in role (tasks/main.yml
):
---
- pause:
prompt: "Do you want to install mariadb (yes/no)?"
register: my_pause
delegate_to: localhost
- include_tasks: mysql.yml
when: hostvars['localhost'].my_pause.user_input | bool
and my output :
[root@anisble ansible]# ansible-playbook playbooks/test.yml
PLAY [dbs]
********************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [db1]
ok: [db2]
ok: [db3]
TASK [ssh : pause] ******************************************************************************
[ssh : pause]
Do you want to install mariadb (yes/no)?:
no
ok: [db1 -> localhost]
TASK [ssh : include_tasks] ***********************************************************************************
included: /etc/ansible/roles/ssh/tasks/mysql.yml for db1, db2, db3
TASK [ssh : install mariadb] ******************************************************************************
ok: [db3]
ok: [db2]
ok: [db1]
PLAY RECAP *****************************************************************************
db1 : ok=4 changed=0 unreachable=0 failed=0
db2 : ok=3 changed=0 unreachable=0 failed=0
db3 : ok=3 changed=0 unreachable=0 failed=0
You can use either a variable prompt or pause
module with user_input
; and set up a condition for the task:
- hosts: my_host_group
vars_prompt:
- name: "install_mariadb"
prompt: "Do you want to install MariaDB (yes/no)?"
private: no
tasks:
- name: install MariaDB
yum:
name: MariaDB
state: present
when: install_mariadb | bool
- include_tasks: MySQL.yml
when: install_mariadb | bool
or
- hosts: my_host_group
tasks:
- pause:
prompt: "Do you want to install MariaDB (yes/no)?"
register: my_pause
delegate_to: localhost
- name: install MariaDB
yum:
name: MariaDB
state: present
when: hostvars['localhost'].my_pause.user_input | bool
- include_tasks: MySQL.yml
when: hostvars['localhost'].my_pause.user_input | bool
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With