Ruth Edges
Ruth Edges

Reputation: 147

Can you use a when: with a vars_prompt?

I am trying to skip the next three prompts if the user inputs y to the first prompt.

---

- name: User MOTD Script
  hosts: localhost
  vars_prompt:

      - name: "defaultMOTD"
        prompt: "Do you want to set the MOTDs to the default? (y/n)"
        private: no

      - name: "MOTD"
        prompt: "Please input your MOTD for /etc/motd: "
        private: no
        when: defaultMOTD != "y"

      - name: "MOTDIssue"
        prompt: "Please input your MOTD for /etc/issue: "
        private: no
        when: defaultMOTD != "y"

      - name: "MOTDIssueNet"
        prompt: "Please input your MOTD for /etc/issue.net: "
        private: no
        when: defaultMOTD != "y"

Output when y is inputted on the first prompt.

Do you want to set the MOTDs to the default? (y/n): y
Please input your MOTD for /etc/motd: :
Please input your MOTD for /etc/issue: :
Please input your MOTD for /etc/issue.net: :

The 2nd,3rd and 4th prompts should be skipped.

Upvotes: 1

Views: 58

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Use pause, e.g.

---

- name: User MOTD Script
  hosts: localhost
  tasks:

    - name: "defaultMOTD"
      pause:
        prompt: "Do you want to set the MOTDs to the default? (y/n)"
      register: result
    - set_fact:
        defaultMOTD: "{{ result.user_input }}"

    - block:
        - name: "MOTD"
          pause:
            prompt: "Please input your MOTD for /etc/motd"
          register: result
        - set_fact:
            MOTD: "{{ result.user_input }}"
        - name: "MOTDIssue"
          pause:
            prompt: "Please input your MOTD for /etc/issue"
          register: result
        - set_fact:
            MOTDIssue: "{{ result.user_input }}"
        - name: "MOTDIssueNet"
          pause:
            prompt: "Please input your MOTD for /etc/issue.net"
          register: result
        - set_fact:
            MOTDIssueNet: "{{ result.user_input }}"
      when: defaultMOTD != "y"

Upvotes: 2

Related Questions