can var_prompt be included in a role?

2019-08-03 20:02发布

问题:

I have written a simple play book master.yml,the main function is to lookup the roles and enter one as input for second playbook. But it doesn't prompt for the variables eventhough I used var_prompt role.

--- #master.yml

- name: show roles.
  hosts: nodes
  connection: ssh
  gather_facts: no
  tasks:
    - include: showroles.yml

- name: createdb and map roles.
  hosts: nodes
  connection: ssh
  gather_facts: no
  roles:
    - vars_prompt_role
  tasks:
    - include: createdb.yml

My directory structured as following:

/playbooks/createdbandmaprole/
[user@localhost createdbandmaprole]$ ls
createdb.yml  hosts  master.yml  roles  script.sql.j2  showroles.yml
[user@localhost createdbandmaprole]$cd roles
[user@localhost roles]$ ls
var_prompt_role
[user@localhost roles]$ cd var_prompt_role/
[user@localhost var_prompt_role]$ ls
defaults  handlers  library  main.yml  meta  tasks  vars
[user@localhost var_prompt_role]$ vim main.yml
vars_prompt:
 - name: "database"
   prompt: "enter the name of database"
   private: no
 - name: "role"
   prompt: "enter the name of role"
   private: no
 - name: "ad_group"
   prompt: "enter the AD_Group"
   private: no
[user@localhost createdbandmaprole]$ ansible-playbook master.yml -i hosts --check

but it doesn't prompt for vars

回答1:

vars_prompt should be called from the top level of the playbook, not a task:

Here is a most basic example:

---
- hosts: all
  remote_user: root

  vars:
    from: "camelot"

  vars_prompt:
    - name: "name"
      prompt: "what is your name?"
    - name: "quest"
      prompt: "what is your quest?"
    - name: "favcolor"
      prompt: "what is your favorite color?"

So you might actually want to be calling vars_prompt as shown above, according to this page.

If you had a role to call vars_prompt, it would be defined in its own file under a roles/ directory, which should live at the same level as your playbook. In your case, it might look like this:

--- #master.yml

...
- name: createdb and map roles.
  hosts: nodes
  roles:
      - vars_prompt_role
...

Then in roles/vars_prompt_role/tasks/main.yml, you would define the vars_prompt_role role's tasks. For more information on roles, check out this page, or see this page for best practices on content organization.

This is how your roles/ directory should look:

roles/
   var_prompt_role/
     files/
     templates/
     tasks/
       main.yml
     handlers/
     vars/
     defaults/
     meta/

Where main.yml is the main task you want to carry out for this role.



标签: ansible