0

I have a simple ansible Playbook called setup.yml which looks basically like this (simplified):

- hosts: my-server-to-setup
  remote_user: [REDACTED]
  become: yes
  #vars_prompt:
  #  - name: "zbxusername"
  #    prompt: "Zabbix Username"
  #    private: no    
  #  - name: "zbxpw"
  #     prompt: "Zabbix Passwort"
  #     private: yes
      roles:
       - basic
  #     - zabbix

The zabbix role requires a username and a password to acccess the zabbix host server. The credentials differ, every user has his own account.

If I want to install the zabbix role, I need to uncomment the vars_prompt section because ansible doesn't allow prompting from within a role.

Is there a better way to do this? The current way can possibly leave important variables undefined which would result in a playbook failure.

1 Answer 1

1

Create a playbook top.yml that imports playbooks basic.yml and zabix.yml. Run the imported playbooks selectively with the tags.

ansible-playbook -t basic top.yml
ansible-playbook -t zabix top.yml

playbook top.yml

- name: Run basic
  import_playbook: basic.yml
  tags: [basic, never]

- name: Run zabix
  import_playbook: zabix.yml
  tags: [zabix, never]

playbook basic.yml

- hosts: my-server-to-setup
  remote_user: [REDACTED]
  become: yes
  roles:
    - basic

playbook zabix.yml

- hosts: my-server-to-setup
  remote_user: [REDACTED]
  become: yes
  vars_prompt:
    - name: "zbxusername"
      prompt: "Zabbix Username"
      private: no    
    - name: "zbxpw"
      prompt: "Zabbix Passwort"
      private: yes
  roles:
    - zabbix

Notes

The tag never might be useful not to accidentally run both roles. Without any tag specified none of the imported playbooks will run. There will be no action as a result of the following command.

ansible-playbook top.yml
3
  • Thanks for your help. I like this approach because I only need to comment out one part now (What you call "Run basic" etc).
    – Panki
    Sep 13, 2019 at 9:20
  • You're welcome. I've added details. (Not sure what you need to comment). Sep 13, 2019 at 9:36
  • Upon implementing I realized that the server is specified in both playbooks. Is it possible to specify this only in top.yml?
    – Panki
    Sep 23, 2019 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .