Article original publié le : 06 mai 2021 Mise a jour le : |
Un mémo sur le module LineInFile que j’avais utilisé pour ajouter des tokens afin de faire de l’authentification sur des serveurs Apache
Pour tester le bon fonctionnement en local
1 |
$ cat hosts |
1 2 |
[local] admin ansible_connection=local |
1 |
$ cat playbook_localtest.yml |
1 2 3 4 5 6 7 8 9 |
--- - name: Playbook lineinfile hosts: local tasks: - name: Ajout du certificat lineinfile: path: require.conf line: "Require expr \"'%{SSL_CLIENT_I_DN}:%{SSL_CLIENT_S_DN_CN}:%{SSL_CLIENT_M_SERIAL}' == 'CN=xxx,OU=xxx,O=xxx,C=xx:{{ prenom }} {{ nom }}:{{ certificat }}'\"" |
Pour jouer le playbook il faut renseigner les 3 variables {{ prenom }} {{ nom }}:{{ certificat }}
Elles ne sont pas définies en dur, car elles sont attribuées a la création du token
Ce qui donne en test
1 |
$ ansible-playbook -i hosts playbook_localtest.yml -e "prenom=John3 nom=Doe3 certificat=11111DDD" |
1 2 3 4 5 6 7 8 9 10 |
PLAY [Playbook lineinfile] ******************************************************************************************************************************************************* TASK [Gathering Facts] ********************************************************************************************************************************************************************** ok: [admin] TASK [Ajout du certificat] **************************************************************************************************************************************************** changed: [admin] PLAY RECAP ********************************************************************************************************************************************************************************** admin : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Le fichier est bien incrémenté d’une nouvelle ligne avec les renseignements qu’on lui a donnés.
1 |
$ cat require.conf |
1 2 3 |
Require expr "'%{SSL_CLIENT_I_DN}:%{SSL_CLIENT_S_DN_CN}:%{SSL_CLIENT_M_SERIAL}' == 'CN=xxx,OU=xxx,O=xxx,C=xx:John Doe:11111EEE'" Require expr "'%{SSL_CLIENT_I_DN}:%{SSL_CLIENT_S_DN_CN}:%{SSL_CLIENT_M_SERIAL}' == 'CN=xxx,OU=xxx,O=xxx,C=xx:John2 Doe2:11111CCC'" Require expr "'%{SSL_CLIENT_I_DN}:%{SSL_CLIENT_S_DN_CN}:%{SSL_CLIENT_M_SERIAL}' == 'CN=xxx,OU=xxx,O=xxx,C=xx:John3 Doe3:11111DDD'" |
Pour les serveurs en ligne, quelques modifications du playbook
1 |
$ cat hosts |
1 2 3 4 5 6 |
[local] admin ansible_connection=local [apache] serveur1.domaine.tld ansible_port=2222 serveur2.domaine.tld ansible_port=2222 |
1 |
$ cat playbook_lineinfile.yml |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
--- - name: Playbook lineinfile hosts: apache become: yes tasks: - name: Ajout du certificat lineinfile: path: /etc/httpd/conf.auth.d/extranet.xxxv.xx/{{ destination }}.conf line: "Require expr \"'%{SSL_CLIENT_I_DN}:%{SSL_CLIENT_S_DN_CN}:%{SSL_CLIENT_M_SERIAL}' == 'CN=xxx,OU=xxx,O=xxx,C=xx:{{ prenom }} {{ nom }}:{{ certificat }}'\"" owner: root group: root - name: Status du service command: /usr/sbin/apachectl configtest changed_when: false - name: recharger apache command: /usr/sbin/apachectl graceful |
Les 3 variables sont toujours demandé, mais en plus le path fait appel a une nouvelle variable, un fichier de destination en fonction du workspace ou le token est autorisé à se connecter, il est à renseigner également
Pour l’exécution
1 |
$ ansible-playbook -i hosts playbook_lineinfile.yml-e "prenom=John4 nom=Doe4 certificat=11111BBB" -e "destination=workspace1" |
Pour un prenom/nom composé, le mettre en simple cote
1 |
$ ansible-playbook -i hosts playbook_lineinfile.yml-e "prenom='John5 JR' nom=Doe4 certificat=11111AAA" -e "destination=workspace1" |