Hoeze
Hoeze

Reputation: 716

How to add host to ansible group when the host is running a certain role?

Imagine, you have a role that adds a NFSv4 mount point with Kerberos authentication. This rule directly depends on the host being in the ansible-freeipa/ipaclient group s.t. the host can request Kerberos tickets.

Is there a way to automatically make all hosts that execute this role also member in the ipaclient group?

Or do you know a better solution to this problem?

Upvotes: 1

Views: 212

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68104

Q: "Automatically make all hosts that execute this role a member of the ipaclient group."

A: Put this task into the role

- add_host:
    name: '{{ item }}'
    groups: ipaclient
  loop: "{{ ansible_play_hosts_all }}"

Notes:

  • Module add_host "only runs once for all the hosts in the play"

  • Make the changes permanent in INI-style hosts file

- ini_file:
    allow_no_value: true
    path: "{{ path_to_hosts_file }}"
    section: ipaclient
    option: "{{ item }}"
  loop: "{{ ansible_play_hosts_all }}"
  delegate_to: localhost
  run_once: true

Upvotes: 1

Related Questions