Linux – use vars_prompt but when i run the playbook it directly gives me prompt and ask for the value, it doesn’t checks in inventory file

ansibleansible-playbooklinux

What I want is set the priority of variables. I am passing the variable from inventory file and if the variable is not defined in inventory file then it should ask for user prompt.
I am trying to use vars_prompt but when i run the playbook it directly gives me prompt and ask for the value, it doesn't checks in inventory file.
So, I want first it should check the inventory file and if variable is not defined there and it should prompt me for the value on console.
If ansible doesn't support this, then can we use shell or command module for this. Any help will be appreciated

Below is my playbook. and inventory file
I am using ansible version 1.6
Inventory file

[test]
X.X.X.X cr="12" 

Playbook


  • name: -> Apply common configuration to {{ target }} nodes
    hosts: "{{ target }}"
    gather_facts: True

    user: root

    pre_tasks:

    • name: -> Is cr defined??

      fail: msg="Please make sure the variables 'cr' is specified in
      inventory host file"

      when: (cr is not defined)

      register: cr_value

    vars_prompt:

    • name: cr

      prompt: "Please mention the cr id"

      private: no

      when: cr_value|failed

    • name: Print the value of cr

      debug: msg="{{ cr }}"

Best Answer

Ansible doesn't support this, from the voice of its creator Michael DeHaan:

vars_prompt always prompts for the variable.

I'd suggest setting a role default instead, or putting a good default value in group_vars/all, and you can override that on the CLI with "-e".

As a workaround you can set a default for your variable prompt and if it's the case override it.

    vars_prompt:
      - name: cr
        prompt: "Please mention the cr id"
        private: no
        default: 12