Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `vars_prompt` on Task file

Trying to use vars_prompt on main.yml task inside role but I get error:

tasks/main.yml file for role 'roleName' must contain a list of tasks

  vars_prompt:
    - name: 'variableName'
      prompt: "Prompting User "
      private: no
      default: ''

  - name: taskName
    uri:
      url: "{{ variableName }}"
    register: response
    ignore_errors: yes
  - debug:
      var: response

If I move the prompting to playbook main.yml it works but I need to be able to do it within the task. Thoughts?

like image 808
itms Avatar asked May 15 '19 21:05

itms


People also ask

How do you take input from playbook?

If you want your playbook to prompt the user for certain input, add a 'vars_prompt' section. Prompting the user for variables lets you avoid recording sensitive data like passwords. In addition to security, prompts support flexibility.

How do I use extra vars in Ansible?

To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.

How do you define host vars in Ansible?

Ansible uses a combination of a hosts file and a group_vars directory to pull variables per host group and run Ansible plays/tasks against hosts. group_vars/all is used to set variables that will be used for every host that Ansible is ran against.

What is Pre_tasks in Ansible?

What is Ansible pre_tasks? Ansible pretask is a conditional execution block that runs before running the play. It can be a task with some prerequisites check (or) validation.


1 Answers

vars_prompt can only be defined on a play. A task list is so named because it can only consist of tasks; metadata like vars, vars_prompt, hosts, etc. can only be set at the play level.

Consider avoiding the use of vars_prompt if at all possible. If you need input from the user, have them provide it on the command line using -e variable=value or in a file and using -e @somefile.yml.

like image 54
larsks Avatar answered Oct 02 '22 11:10

larsks