Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6e3396cc86 | |||
| 262626a5ca | |||
| 33a6317669 | |||
| 91cdc4fb29 | |||
| 425ea2cba4 | |||
| 4a72838034 |
14
Readme.md
14
Readme.md
@@ -1,14 +0,0 @@
|
||||
## 1er 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 3 commandes adhoc de l'atelier précédent en place en playbook, associées aux machines du groupe **cibles**.
|
||||
|
||||
_Prérequis_:
|
||||
** avoir créé une paire de clé publique/privée RSA ssh à l'aide de la commande ssh-keygen (ou le module ansible openssh_keypair)
|
||||
|
||||
**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.
|
||||
29
setup.yml
29
setup.yml
@@ -3,14 +3,14 @@
|
||||
hosts: cibles
|
||||
tasks:
|
||||
- name: warn people
|
||||
lineinfile:
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/motd
|
||||
create: yes
|
||||
line: "Host is managed by ansible, manual interaction not recommended"
|
||||
state: present
|
||||
|
||||
- name: history
|
||||
lineinfile:
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/history
|
||||
line: "{{ '%Y-%m-%d' | strftime }} - {{ ansible_play_name }}"
|
||||
state: present
|
||||
@@ -24,22 +24,35 @@
|
||||
become: false
|
||||
|
||||
- name: create account
|
||||
user:
|
||||
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
|
||||
copy:
|
||||
content: "ansible ALL=(ALL) NOPASSWD: ALL"
|
||||
dest: /etc/sudoers.d/ansible
|
||||
validate: "/usr/sbin/visudo -cf %s"
|
||||
community.general.sudoers:
|
||||
name: ansible
|
||||
user: ansible
|
||||
commands: ALL
|
||||
nopassword: true
|
||||
|
||||
- name: deploy ssh key
|
||||
authorized_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