0

I have collection of roles and hosts that these roles are run against. But I also have "custom" hosts that only need some of those roles, and it varies which roles which host needs. How could I create a playbook which would run against specific group of hosts and ask before every role whether the role should be run?

So far I created playbook custom.yml (which doesn't work...):

- name: Select roles
  hosts: custom
  become: yes
  roles:
  - role: custom
  - { role: common, when: run_common == "Y" or run_common == "y" }
  - { role: system, when: run_system == "Y" or run_system == "y" }
  - { role: date, when: run_date == "Y" or run_date == "y" }
  - { role: swap, when: run_swap == "Y" or run_swap == "y" }
  - { role: users, when: run_users == "Y" or run_users == "y" }

I created a role to prompt whether to run a specific role or not - custom/tasks/main.yml:

- include_tasks: select_roles.yml
  with_items:
  - common
  - system
  - date
  - swap
  - users

custom/tasks/select_roles.yml:

- include_vars:
  vars_prompt:
    - name: "run_{{ item }}"
      prompt: "Run {{ item }}?"
      default: "N"

  tasks:
    - set_fact:
        run_{{ item }}: "run_{{ item }}"
      when: "run_{{ item }}" == "Y"

At the moment I have it running all inside a single playbook but it's lot of copy-paste and editing, and looks ugly:

- name: Run select roles on all custom nodes
  hosts: custom
  become: yes
  vars_prompt:
     - name: "run_common"
     prompt: "Run common?"
     default: "N"
     private: no
     - name: "run_system"
     prompt: "Run system?"
     default: "N"
     private: no
     - name: "run_date"
     prompt: "Run date?"
     default: "N"
     private: no
     - name: "run_swap"
     prompt: "Run swap?"
     default: "N"
     private: no
     - name: "run_users"
     prompt: "Run users?"
     default: "N"
     private: no
  roles:
  - { role: common, when: run_common == "Y" or run_common == "y" }
  - { role: system, when: run_system == "Y" or run_system == "y" }
  - { role: date, when: run_date == "Y" or run_date == "y" }
  - { role: swap, when: run_swap == "Y" or run_swap == "y" }
  - { role: users, when: run_users == "Y" or run_users == "y" }
3
  • Possible duplicate of can var_prompt be included in a role? Jan 9, 2019 at 13:39
  • @imjoseangel Thanks for pointing to it. It's somewhat similar but not quite the same as I'm trying to use variables ("run_{{ item }}") and loops (with_items) to create dynamic vars_prompt.
    – goldie
    Jan 9, 2019 at 16:11
  • Another question. So every time you want to run a role against an inventory, you want to ask to the user if he/she wants to run a specific role, isn't it? Jan 9, 2019 at 16:43

1 Answer 1

1

Look this solution. I think you will like it. You can have your vars in another file and load them first.

---
- name: Run Specific Role
  hosts: localhost
  connection: local
  gather_facts: false

  vars:
    mymenu:
      - common
      - system
      - date
      - swap
      - users
    allowed_items:
      - "1"
      - "2"
      - "3"
      - "4"
      - "5"

  vars_prompt:
    - name: runmenu
      prompt: "Run\n1 - {{ mymenu[0] }}\n2 - {{ mymenu[1] }}\n3 - {{ mymenu[2] }}\n4 - {{ mymenu[3] }}\n5 - {{ mymenu[4] }}\n[Enter Values with Comma as Separator]"
      private: false

  pre_tasks:

    - name: Set Fact
      set_fact:
        menu{{ item  }}: "menu{{ item  }}"
      with_items: "{{ runmenu.split(',') }}"
      when: item in allowed_items

  roles:
  - role: common
    when: menu1 is defined
3
  • It works nicely, quite similar to my solution. Seems approx the same amount of copy-paste and editing as mine, so not that dynamic... I guess vars_prompt just can't be used within a role so it has to be done on the playbook level, and no loops and items to make it more elegant...
    – goldie
    Jan 10, 2019 at 16:10
  • Yep, it’s a pity it has not that flexibility. Jan 10, 2019 at 17:52
  • According to stackoverflow.com/questions/25466675/… what I requested can't be done, and is so by design. So I set this as an answer. Both this and my example work.
    – goldie
    Jan 15, 2019 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.