I do something similar, but then with rsyslog.
I have this set in the role:

defaults/main.yml:
rsyslog_type: client

and then in tasks/main.yml:

- name: Ensure the appropriate tasks file is imported
  include_tasks: "{{ rsyslogd_type }}.yml"

This then includes either tasks/client.yml or tasks/server.yml.

The playbook uses the role like this:

- name: Include rsyslogd role
  include_role:
    name: ansible_role_rsyslogd
  args:
    apply:
      tags: rsyslogd
  tags: always


So by default (i.e. for all hosts in the play) the rsyslog client is
configured.
The one or two rsyslog servers have set this host_var:

rsyslog_type: server

It's more or less the same, except that I use a variable to denote the type
of deployment, rather than tags, as I find tags more cumbersome to work
with.

On Wed, 23 Nov 2022 at 15:52, RG <[email protected]> wrote:

> hi,
>
> I adapted the configuration to your proposal, however it doesn't work
> First all tasks were execetuted on host cmp an then all tasks were again
> exectuted on the the other machine except cmp.
>
> Regarding to your solution would be necessary to tag both tasks
> roles/nfs/tasks/nfs_server.yml
> ---
> - name: tasks to configure nfs servers
>   some_tasks:
> ...
>
> roles/nfs/tasks/nfs_client.yml
> ---
> - name: tasks to configure nfs clients
>   some_tasks:
> ...
>
> maybe as a block, so that there's i reference to those tags in the
> main.yml.
>
> I don't want to call both roles seperately - I'd like to have a playbook
> where both tasks setting up the nfs-server and clients are done once
> automatically.
>
> Is generally the main idea of ansible roles regarding to my problem
> creating two roles, server and client because of different hosts or is my
> approach having one role for both server and client the ideal one?
>
> [email protected] schrieb am Mittwoch, 23. November 2022 um 14:31:52
> UTC+1:
>
>> It sounds like you want one NFS role that can do both NFS server and NFS
>> client tasks. Were I designing such a role I think I would use a variable
>> or tag to identify which "persona" you want the role to configure, and use
>> that in the tasks/main.yml to source two different task files. One task
>> file would do the work for an NFS server persona. One task file would do
>> the work for an NFS client persona.
>>
>>
>> roles/nfs/tasks/main.yml
>> ---
>> - name: configure nfs server
>>   include_tasks: nfs_server.yml
>>   tags: nfs_server
>>
>> - name: configure nfs client
>>   include_tasks: nfs_client.yml
>>   tags: nfs_client
>> ...
>>
>> roles/nfs/tasks/nfs_server.yml
>> ---
>> - name: tasks to configure nfs servers
>>   some_tasks:
>> ...
>>
>>
>> roles/nfs/tasks/nfs_client.yml
>> ---
>> - name: tasks to configure nfs clients
>>   some_tasks:
>> ...
>>
>>
>> This lets you separate the tasks for server and client persona. Executing
>> the role with tag nfs_server will configure the inventory machines as nfs
>> servers. Executing the role with tag nfs_client will configure the
>> inventory machines as nfs clients. Execute the role two times with
>> different inventories and tags as you showed in your original message.
>>
>>
>> my_playbook.yml
>> ---
>> - hosts: cmp
>>   become: true
>>   tags: nfs_server
>>   roles:
>>     - nfs
>>
>> - hosts:
>>     - hardware:!cmp
>>     - vms
>>   become: true
>>   tags: nfs_client
>>   roles:
>>     - nfs
>>
>>
>> I am curious to have others also propose ideas to see how others might
>> address the problem.
>>
>> Walter
>> --
>> Walter Rowe, Division Chief
>> Infrastructure Services, OISM
>> Mobile: 202.355.4123 <(202)%20355-4123>
>>
>> On Nov 23, 2022, at 8:08 AM, Robert Grießner <[email protected]>
>> wrote:
>>
>> Hi,
>> i'd like to migrate my playbooks into a ansible role. I started but a
>> basic question raises.
>> E.g.
>> I sould like to implement nfs in network of linux machines. on one
>> machine i've to configure the nfs-sharing and on the other machines I've to
>> implemt the link to the nfs-sourcing host.
>>
>> systemA - configure a share for other hosts
>> systemB,C,D - use the nfs-sharing from system A
>>
>> I tried to create a role called nfs where i have to configure a few tasks
>> on systemA and the other tasks on systemB,C,D
>> I've splitted the tasks into 2 vaious task files one for systemA and one
>> for systemB and both will be imported into main.yml in tasks.
>> ---
>> # tasks file for installing nfs
>> - import_tasks: install_cmp.yml
>> - import_tasks: install_machines.yml
>>
>> in the run.yml I tried to call the same role twice where I used tags to
>> call the plays for the cmp machine from install_cmp.yml and the tags in
>> install_machines for running the plays for the other machines. however this
>> doesn't work
>>
>> - hosts: cmp
>>   become: true
>>   tags:
>>     - create_nfs_cmp
>>     - config_exports_cmp
>>     - exportfs_cmp
>>     - symlink_cmp
>>     - symlink_hostsini_cmp
>>   roles:
>>     - nfs
>>
>> - hosts:
>>     - hardware:!cmp
>>     - vms
>>   become: true
>>   tags:
>>     - create_nfs_machines
>>     - mnt_nfs_machines
>>   roles:
>>     - nfs
>>
>>
>> is my concept wrong, do I have to configure to roles one for setting up
>> nfs on cmp and one for implementing nfs on the machines or is it possible
>> to create just one role for installing nfs on with vaious tasks on various
>> machines?
>>
>> Thx
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/a9e1aa6f-f5d8-4256-b735-fc693bb133b4n%40googlegroups.com
>> <https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2Fa9e1aa6f-f5d8-4256-b735-fc693bb133b4n%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C63631feb95cf466144e408dacd54229d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638048058487251915%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=FOpwW7nZfhWhVKzhASHgCri5nwlggPPnue52DU9YQYI%3D&reserved=0>
>> .
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/a550d0ac-50f3-4507-b5ab-a26fe214c7d1n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/a550d0ac-50f3-4507-b5ab-a26fe214c7d1n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAF8BbLYqx7gHpF4yr009r4WLy-iLwhYrSBMuwFhqKK7qd612ZA%40mail.gmail.com.

Reply via email to