0

I want to achieve a common ansible role. And conditions are : without using gather_facts (which basically allows us to check os_family for linux).

The way I have been doing it is with inventory group [windows] & when condition with playbooks (without roles).

Now, as I am trying very hard to convert all the playbooks to ansible roles, I am just wondering is there any better way to achieve it. Or is it better to have separate roles like windows-rolename & linux-rolename".

Here is how I had it in playbooks, which I can apply to roles as well but I really want a better way if there is any..

My approach:

myplaybook.yml

---

- hosts: target
  tasks:
    - name: This will run on windows
      win_shell: whoami
      when: inventory_hostname in groups ['windows']

    - name: On linux, if ljnux
      shell: whoami
      when: inventory_hostname not in groups ['windows']

myinventory

[target]
xx.xx.xx.xx

[windows:children]
target

[linux:children]
..
...

This is just a basic example/glimpse of what I have

Just imagine it with 100s of playbooks..

Hope I am not overthinking it, my usecase is weird I know... but any help and clarification would be appreciated!! :)

Abid Khan
  • 145
  • 2
  • 9

1 Answers1

0

I am answering it too, as this is the only manageable solution I feel I have, I would really appreciate a better idea than this.

  1. Create separate roles one for windows & one for linux
roles/
    windows-rolename
    linux-rolename

roles/windows-rolename/tasks/main.yml

- name: This will run on windows
  win_shell: whoami

roles/linux-rolename/tasks/main.yml

- name: On Linux, if linux
  shell: whoami
  1. Create a main playbook with when condition to check if the target host is in windows group or linux, based on the condition it will include the corresponding role.

./myplaybook.yml

- hosts: target
  roles:
    - include_role: *windows-rolename*
      when: inventory_hostname in groups ['windows']

    - include_role: *linuxx-rolename*
      when: inventory_hostname not in groups ['windows']

I don't know the better way of achieving it yet.. like with just one role name

Something like this..

roles/
  <rolename>
    ## windows tasks ##
    ## linux tasks ##
Abid Khan
  • 145
  • 2
  • 9
  • This does not look like an answer but as additional info that should get inside [an edit of your question](https://stackoverflow.com/posts/65269931/edit). Moreover, you question is very broad and certainly opinion based. There are different ways to acheive the same goal that all have their pros and cons. Therefore, it might be considered [off-topic](/help/on-topic) – Zeitounator Dec 13 '20 at 11:02
  • It is unclear to me how this is an answer. Please include an explanation what you changed, and why (and how) that fixes the problem. – Mark Rotteveel Dec 13 '20 at 11:11
  • @Zeitounator I have updated the answer, yes I agree it should be in the question itself. But I feel that's the only solution I see and more manageable one, unless someone says otherwise. – Abid Khan Dec 13 '20 at 11:39
  • @MarkRotteveel Updated it, if it is still unclear then I would like your suggestion. I am looking for a better solution which doesn't go against any standards. Cause I have seen handling flat playbooks was really a pain when you have tons of functionalities to be automated. – Abid Khan Dec 13 '20 at 11:41
  • I haven't marked it as answer yet, I am putting my thoughts and want somebody to come up with a better idea. :) – Abid Khan Dec 13 '20 at 11:42
  • Not terribly sure that this is a good answer or not, but in the tasks folder of the role, you could have your main task do a check for each of it's tasks and then call the windows or linux equiv of that task in say windows_download.yml/linux_download.yml. I'm approaching a smilar issue with build servers running windows or linux and need to run the same tasks on each, but windows tasks in ansible are different than linux tasks... – Jeff Patton Dec 29 '20 at 16:43
  • Your solution above creates a role per OS which seems like a lot, my solution moves that logic into one role...which seems like a lot...lol. For those of us supporting windows/linux i think it's just a rough row to hoe :) Interested if you find anything more elegant. – Jeff Patton Dec 29 '20 at 16:45
  • 1
    @JeffPatton I agree with you, even if we move everything to one role it will be a chaos anyway .. there is NO easy path to the heaven, isn't it!! I am more confused than I ever was, should I go with this approach and stop making a big deal out of it or I should wait for the future me to tell me just get it done already!!! :/ – Abid Khan Dec 30 '20 at 21:12