vars_prompt variables carry over into next tasks

Good morning.

In the following playbook, i have prompts to capture certain information. The created variables work fine in the first set of tasks in the playbook. However, they don’t carry over into the second set of tasks in the playbook, based on what I’ve tried (as seen in the playbook, which is probably not the most efficient). Is there a way to do this?

I think the vars from vars_prompt are tied to localhost in the first play, so if you need them in any next plays, you would need to reference them as localhost’s hostvars:

{{ hostvars[‘localhost’].pemno }}

(not tested)

Dick, I’m not getting that to work. For now, I’m prompting again for the pemno withing the second set of tasks. That works, but isn’t ideal. I’d like to see if I can “re-use” the pemno gathered from the first tasks prompt in the second set of tasks:

- hosts: localhost
connection: local
gather_facts: false

vars_prompt:
- name: “pemno”
prompt: “Enter the number of the created pems”
private: no

tasks:

- hosts: myhost
become: yes

tasks:
<----- use pemno here from above

This works. The set_fact makes a global variable for the entire playbook.

  • hosts: localhost

connection: local

gather_facts: false

become: false

vars_prompt:

  • name: “pemno”

prompt: “Enter the number of the created pems”

private: no

tasks:

  • set_fact:

pemno: “{{ pemno }}”

  • hosts: all

gather_facts: false

become: false

tasks:

  • debug: var=pemno

Walter

Walter, I tried this, which is like what I think you posted: