Skip to content

Commit

Permalink
Ansible role for joining rhel instances to AD (#367)
Browse files Browse the repository at this point in the history
* Role for joining linux instances to AD
  • Loading branch information
pavmoj authored Oct 19, 2023
1 parent 133c5d7 commit 22ae6d1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions ansible/roles/join-ad-linux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This role is for joining linux instances to a domain.
9 changes: 9 additions & 0 deletions ansible/roles/join-ad-linux/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
service_account_ssm_passwords:
service_account:
parameter: "/join_domain_linux_service_account/passwords"
users:
- username: auto
- password: auto

ad_domain: AZURE.NOMS.ROOT
6 changes: 6 additions & 0 deletions ansible/roles/join-ad-linux/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Restart SSSD service
service:
name: sssd
state: restarted
enabled: yes
23 changes: 23 additions & 0 deletions ansible/roles/join-ad-linux/tasks/get_facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: Get linux service account details
import_role:
name: ssm-passwords
vars:
ssm_passwords: "{{ service_account_ssm_passwords }}"

- name: Set linux service account variables
set_fact:
join_domain_linux_service_account_username: "{{ ssm_passwords_dict['service_account'].passwords['username'] }}"
join_domain_linux_service_account_password: "{{ ssm_passwords_dict['service_account'].passwords['password'] }}"

- name: Check parameters
set_fact:
all_variables_set: true
when:
- join_domain_linux_service_account_username|length > 0
- join_domain_linux_service_account_password|length > 0

- name: Fail if missing parameters
fail:
msg: Ensure all required parameters are set
when: not all_variables_set|default(false)
30 changes: 30 additions & 0 deletions ansible/roles/join-ad-linux/tasks/join_domain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
- name: Configure sshd_config
lineinfile:
path: /etc/ssh/sshd_config
regexp: "^PasswordAuthentication.*no"
line: "PasswordAuthentication=yes"
notify: Restart SSSD service

- name: Install pexpect for injecting secrets
pip:
name: pexpect

- name: Install required packages for joining to the domain
package:
name:
- realmd
- sssd
- samba-common-tools
- realmd
- oddjob
- oddjob-mkhomedir
- adcli
- krb5-workstation
state: present

- name: Join instance to the domain
expect:
command: /bin/bash -c "/usr/sbin/realm join --user={{ join_domain_linux_service_account_username }}@{{ ad_domain|upper }} {{ ad_domain|lower }} -v"
responses:
Password for *: "{{ join_domain_linux_service_account_password }}"
15 changes: 15 additions & 0 deletions ansible/roles/join-ad-linux/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Set instance hostname
import_role:
name: set-ec2-hostname

- name: Get linux service account details
import_tasks: get_facts.yml
tags:
- ec2provision
when: ansible_distribution in ['RedHat']

- import_tasks: join_domain.yml
tags:
- ec2provision
when: ansible_distribution in ['RedHat']

0 comments on commit 22ae6d1

Please sign in to comment.