Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6e3396cc86 | |||
| 262626a5ca | |||
| 33a6317669 | |||
| 91cdc4fb29 | |||
| 425ea2cba4 | |||
| 4a72838034 |
29
Readme.md
29
Readme.md
@@ -1,29 +0,0 @@
|
||||
## Compilations de tâches AdHoc en playbook
|
||||
|
||||
**tâche**: rassembler des commandes adhoc dans un playbook
|
||||
|
||||
**condition**: quand on veut pouvoir les réutiliser facilement
|
||||
|
||||
**norme**: éditeur de texte, référence des modules ansible
|
||||
|
||||
**Pratique:** Convertir les 4 commandes adhoc de l'atelier précédent en place en playbook, associées aux machines du groupe **cibles** :
|
||||
|
||||
```bash
|
||||
ansible localhost -m community.crypto.openssh_keypair -a 'path=/home/formation/.ssh/id_rsa owner=formation group=formation' -u formation
|
||||
ansible cibles -m ansible.builtin.user -a 'user=ansible password={{ "123Soleil!"| password_hash( "sha512", 65534 | random(seed=inventory_hostname) | string) }} create_home=yes home=/home/ansible'
|
||||
ansible cibles -m community.general.sudoers -a 'name="ansible" user=ansible commands=ALL nopassword=true'
|
||||
ansible cibles -m ansible.posix.authorized_key -a 'key={{ lookup( "file", "~/.ssh/id_rsa.pub") }} user=ansible'
|
||||
```
|
||||
|
||||
Rajouter 2 tâches:
|
||||
|
||||
- une qui insère dans le fichier /etc/motd la ligne "Attention cette machine est gérée par ansible"
|
||||
- une qui insère dans le fichier /etc/history la date et le nom du playbook exécuté.
|
||||
|
||||
La date peut être obtenu grâce à l'instruction "{{ '%Y-%m-%d' | strftime }}"
|
||||
|
||||
Le nom du playbook en cours d'exécution est stocké dans la variable "{{ ansible_play_name }}"
|
||||
|
||||
**Validation**: le playbook s'exécute correctement sur nos cibles. À l'issue de son exécution, on peut se connecter en ssh sans mot de passe sur les cibles en utilisant le compte ansible. On peut faire un sudo -l sans avoir à fournir de mot de passe.
|
||||
|
||||
solution: voir branche "solution"
|
||||
58
setup.yml
Normal file
58
setup.yml
Normal file
@@ -0,0 +1,58 @@
|
||||
---
|
||||
- name: setup target to be managed by ansible
|
||||
hosts: cibles
|
||||
tasks:
|
||||
- name: warn people
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/motd
|
||||
create: yes
|
||||
line: "Host is managed by ansible, manual interaction not recommended"
|
||||
state: present
|
||||
|
||||
- name: history
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/history
|
||||
line: "{{ '%Y-%m-%d' | strftime }} - {{ ansible_play_name }}"
|
||||
state: present
|
||||
create: true
|
||||
|
||||
- name: generate ssh keys
|
||||
openssh_keypair:
|
||||
path: "~/.ssh/id_rsa"
|
||||
size: 2048
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
|
||||
- name: create account
|
||||
ansible.builtin.user:
|
||||
name: ansible
|
||||
password: "{{ '123Soleil%' | password_hash('sha512',65534|random(seed=inventory_hostname) | string) }}"
|
||||
create_home: yes
|
||||
home: /home/ansible
|
||||
|
||||
- name: configure sudo
|
||||
community.general.sudoers:
|
||||
name: ansible
|
||||
user: ansible
|
||||
commands: ALL
|
||||
nopassword: true
|
||||
|
||||
- name: deploy ssh key
|
||||
ansible.posix.authorized_key:
|
||||
user: ansible
|
||||
key: "{{ item }}"
|
||||
loop:
|
||||
- "{{ lookup( 'file', '~/.ssh/id_rsa.pub' ) }}"
|
||||
- "https://infra.opendoor.fr/id_rsa.pub"
|
||||
|
||||
- name: reconfigure ansible
|
||||
community.general.ini_file:
|
||||
option: "{{ item.option }}"
|
||||
value: "{{ item.value }}"
|
||||
section: "{{ item.section }}"
|
||||
path: /etc/ansible/ansible.cfg
|
||||
loop:
|
||||
- { option: "remote_user", value: "ansible", section: "defaults" }
|
||||
- { option: "become_ask_pass", value: "false", section: "privilege_escalation" }
|
||||
- { option: "ask_pass", value: "false", section: "defaults" }
|
||||
delegate_to: localhost
|
||||
62
setup2.yml
Normal file
62
setup2.yml
Normal file
@@ -0,0 +1,62 @@
|
||||
---
|
||||
- name: ssh key pair on localhost
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: generate ssh keys
|
||||
openssh_keypair:
|
||||
path: "~/.ssh/id_rsa"
|
||||
size: 2048
|
||||
become: false
|
||||
|
||||
- name: setup target to be managed by ansible
|
||||
hosts: cibles
|
||||
tasks:
|
||||
- name: warn people
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/motd
|
||||
create: yes
|
||||
line: "Host is managed by ansible, manual interaction not recommended"
|
||||
state: present
|
||||
|
||||
- name: history
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/history
|
||||
line: "{{ '%Y-%m-%d' | strftime }} - {{ ansible_play_name }}"
|
||||
state: present
|
||||
create: true
|
||||
|
||||
- name: create account
|
||||
ansible.builtin.user:
|
||||
name: ansible
|
||||
password: "{{ '123Soleil%' | password_hash('sha512',65534|random(seed=inventory_hostname) | string) }}"
|
||||
create_home: yes
|
||||
home: /home/ansible
|
||||
|
||||
- name: configure sudo
|
||||
community.general.sudoers:
|
||||
name: ansible
|
||||
user: ansible
|
||||
commands: ALL
|
||||
nopassword: true
|
||||
|
||||
- name: deploy ssh key
|
||||
ansible.posix.authorized_key:
|
||||
user: ansible
|
||||
key: "{{ item }}"
|
||||
loop:
|
||||
- "{{ lookup( 'file', '~/.ssh/id_rsa.pub' ) }}"
|
||||
- "https://infra.opendoor.fr/id_rsa.pub"
|
||||
|
||||
- name: ansible config on localhost
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: reconfigure ansible
|
||||
community.general.ini_file:
|
||||
option: "{{ item.option }}"
|
||||
value: "{{ item.value }}"
|
||||
section: "{{ item.section }}"
|
||||
path: /etc/ansible/ansible.cfg
|
||||
loop:
|
||||
- { option: "remote_user", value: "ansible", section: "defaults" }
|
||||
- { option: "become_ask_pass", value: "false", section: "privilege_escalation" }
|
||||
- { option: "ask_pass", value: "false", section: "defaults" }
|
||||
Reference in New Issue
Block a user