Automated configuration of git over SSH using ansible

From Notes_Wiki
Revision as of 11:45, 28 July 2022 by Saurabh (talk | contribs)

<yambe:breadcrumb>Git|Git</yambe:breadcrumb>

Automated configuration of git over SSH using ansible

Home > CentOS > CentOS 6.x > System administration tools > ansible > Ansible-playbooks > Automated configuration of git over SSH using ansible

For automated configuration of git over SSH using ansible, where git authentication is through LDAP, use playbook:

---
- name: This file configures git server
  hosts: git
  remote_user: root

  vars:
    repositories:
      - { name: project1, group: project1 }
      - { name: project2, group: project2 }
      - { name: project3, group: project3 }
    ldap_server_fqdn: ldap.rekallsoftware.com
    ldap_server_basedn: dc=rekallsoftware,dc=com

  tasks:
  - name: Install required packages (git, openldap-clients, openldap, nss-pam-ldapd)
    yum: name={{item}} state=present
    with_items:
      - git 
      - openldap-clients 
      - openldap  
      - nss-pam-ldapd

  - name: Remove sssd package
    yum: name=sssd state=absent

  - name: Overwrite sshd_config to disallow all except root ssh
    copy: src=sshd_config  dest=/etc/ssh/sshd_config  owner=root  group=root mode=600
    notify:
      - restart sshd

  - name: Configure authentication via LDAP server
    shell: authconfig --enableldap --enableldapauth --ldapserver="{{ldap_server_fqdn}}" --ldapbasedn="{{ldap_server_basedn}}" --enablelocauthorize --enablepamaccess --disablemkhomedir --updateall

  - name: Create shell script for creating and configuring git repositories
    template: src=configure_git.sh dest=/root/configure_git.sh
  
  - name: Give execute permissions to shell script
    file: path=/root/configure_git.sh owner=root group=root mode=700

  - name: Run git configuration shell script for configuring repositories with correct permissions
    shell: /root/configure_git.sh


  handlers:
  - name: restart sshd
    service: name=sshd state=restarted

The script assumes file named sshd_config present in same folder with contents similar to:

#	$OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile	.ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing, 
# and session processing. If this is enabled, PAM authentication will 
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
#UsePAM no
UsePAM yes

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none

# no default banner path
#Banner none

# override default of no subsystems
Subsystem	sftp	/usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#	X11Forwarding no
#	AllowTcpForwarding no
#	ForceCommand cvs server

PermitTunnel no

Match User *,!root
  ForceCommand perl -e 'exec qw(git-shell -c), $ENV{SSH_ORIGINAL_COMMAND}'
  X11Forwarding no
  AllowTcpForwarding no
  AllowAgentForwarding no
  GatewayPorts no
  Banner "Only git access is allowed"

It might be useful to lock git server more by disabling even root ssh or password based ssh.


Finally the playbook assumes file name 'configure_git.sh' present in the same folder with contents:

#!/bin/bash

mkdir -p /git

{% for repository1 in repositories %}

  mkdir -p /git/{{repository1.name}}
  cd /git/{{repository1.name}}
  git --bare init --shared
  cd ..
  chgrp -R {{repository1.group}} {{repository1.name}}
  chmod -R 070 {{repository1.name}}

{% endfor %}


<yambe:breadcrumb>Git|Git</yambe:breadcrumb>


Home > CentOS > CentOS 6.x > System administration tools > ansible > Ansible-playbooks > Automated configuration of git over SSH using ansible