1

How can I access to answer one prompt on another server? my hosts:

[dbs]
db1
db2
db3

I have one role and my tasks/main.yml is:

- pause:
   prompt: "Do you want to install mysql (yes/no)?"
  register: my_pause

- include_tasks: mysql.yml
  when: my_pause.user_input | bool

when I execute this role, only first server skip MySQL.yml and to another server execute MySQL.yml.

I want if the user enters no for answer prompt,‍‍MySQL.yml does not execute for any server. but when I enter no It will be installed again!!

 [root@anisble ansible]# ansible-playbook playbooks/test.yml 

PLAY [dbs] 
**************************************************************
    TASK [Gathering Facts] 
*******************
ok: [db1]
ok: [db3]
ok: [db2]

TASK [ssh : pause] ********************************************************************************
[ssh : pause]
Do you want to install mysql (yes/no)?:
no
  ok: [db1]

TASK [ssh : include_tasks] ********************************************************************************
skipping: [db1]
included: /etc/ansible/roles/ssh/tasks/mysql.yml for db2, db3

TASK [ssh : install mysql] ********************************************************************************
ok: [db3]
ok: [db2]

PLAY RECAP ********************************************************************************
db1                        : ok=2    changed=0    unreachable=0    failed=0   
db2                        : ok=3    changed=0    unreachable=0    failed=0   
db3  

Edited-1

I use the first your solution but when I enter no It will be installed again!!

task/main.yml:

    ---
    - pause:
       prompt: "Do you want to install mysql (yes/no)?"
      register: install_mysql
      delegate_to: localhost

- include_tasks: mysql.yml
  when: hostvars['localhost']['install_mysql']['user_input'] == 'yes'

Output is :

[root@anisble ansible]# ansible-playbook playbooks/test.yml 
PLAY [dbs] ********************************************************************************
TASK [Gathering Facts] ********************************************************************************
ok: [db2]
ok: [db1]
ok: [db3]

TASK [ssh : pause] ********************************************************************************
[ssh : pause]
Do you want to install mysql (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 mysql] ********************************************************************************
  changed: [db1]
changed: [db3]
changed: [db2]

  PLAY RECAP ********************************************************************************
    db1                        : ok=4    changed=1    unreachable=0    failed=0   
    db2                        : ok=3    changed=1    unreachable=0    failed=0   
    db3                        : ok=3    changed=1    unreachable=0    failed=0   

Edited-2

This method works.

- name: confirm
  pause:
    prompt: "Do you want to install mysql (yes/no)?"
  register: install_mysql

- include_tasks: mysql.yml
  when: hostvars[groups['dbs'][0]]['install_mysql']['user_input'] == "yes"

closed as off-topic by techraf, DarkHeart, Archemar, Thomas Nyman, roaima Mar 5 '18 at 9:46

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

2

Run pause on localhost

You can run the pause task on a specific host, eg localhost and get the answer from the hostvars array hostvars['localhost'](stolen from here).

- name: Pause prompt on localhost
  hosts: localhost
  tasks:
    - name: Prompt
      pause:
        prompt: "Do you want to install mysql (yes/no)?"
      register: install_mysql

- name: Get the answer from the hostvars array
  hosts: all  
  tasks:
    - name: Include role when you answered 'yes'
      include_tasks: mysql.yml
      when: hostvars['localhost']['install_mysql']['user_input'] == 'yes'

You have to use a dedicated play on that host to be able to access the answer in the hostvars array. If you run it once on a hostgroup (like in your original request), you have to guess which host will be processed first and use hostvars['firstprocessedhost'] (sometimes hostvars['db1']).

When you use delegate_to: localhost the task is still run for db1 and the answer is stored in hostvars['db1'].

You should not depend on the order of your hosts and the pause module is intended to pause a playbook not to prompt for user data.

vars_prompt

If it's possible, you should use the vars_prompt section of your play:

- name: A play with a vars_prompt section
  hosts: all  
  vars_prompt:
    - name: install_mysql
      prompt: "Do yo want to install mysql (yes/no)?"

  tasks:
    - name: Include role when you answered 'yes'
      include_tasks: mysql.yml
      when: install_mysql == 'yes'
  • but is this a single choice that applies to all hosts? I think the question is about how to say yes or no for each individual hosts – thrig Feb 14 '18 at 16:48
  • In this case you cannot use delegate_to. – ctx Feb 15 '18 at 10:48
  • special thanks, @ctx. problem solved. I edited finally result ;) – pyramid13 Feb 17 '18 at 6:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.