Accueil  > Dev  > Ansible  >  ConfigServeur

Description

Installation et configuration de tous les packages utiles pour mes serveurs Linux via un script Bash exécutant un playbook Ansible.

 

Démarrage

Chargement des différents Playbooks

- hosts: all
  become: true
  vars_files:
    - vars/main.yml

  roles:
    - base
    - fw

 

Installation des Packages de base

Liste des packages a installer

# Installation des Packages de base
- name: Installation des paquets de base
  apt:
    name: '{{item.name}}'
    state: present
  ignore_errors: yes
  with_items:
    - { name: 'zsh' }
    - { name: 'git' }
    - { name: 'ansible' }
    - { name: 'apt-transport-https' }
    - { name: 'software-properties-common' }
    - { name: 'htop' }
    - { name: 'iftop' }
    - { name: 'bpytop' }
    - { name: 'bc' }
    - { name: 'build-essential' }
    - { name: 'ca-certificates' }
    - { name: 'curl' }
    - { name: 'dirmngr' }
    - { name: 'exfat-fuse' }
    - { name: 'exfat-utils' }
    - { name: 'python3-pip' }
    - { name: 'python3-dev' }
    - { name: 'python3-setuptools'}
    - { name: 'python3-wheel' }
    - { name: 'python3-psutil'}
    - { name: 'iotop' }
    - { name: 'atop' }
    - { name: 'net-tools' }

# Création du Compte {{ utilisateur }}
- name: Creation du Compte Utilisateur
  user:
    name: "{{ utilisateur }}"
    password: "{{ password }}"
    groups: # Empty by default, here we give it some groups
      - audio
      - sudo
      - dialout
    state: present
    shell: /bin/bash       # Defaults to /bin/bash
    system: no             # Defaults to no
    createhome: yes        # Defaults to yes
    home: /home/{{ utilisateur }}  # Defaults to /home/<username>

- name: Autorisation de  '{{ utilisateur }}'  pour Sudo sans mot de passe
  lineinfile:
    dest: /etc/sudoers
    state: present
    regexp: '^{{ utilisateur }}'
    line: '{{ utilisateur }} ALL=(ALL) NOPASSWD: ALL'
    validate: 'visudo -cf %s'

# Copie des Fichiers configuration Git et Bash
- name: Fichier de configuration git
  copy:
    src: "gitconfig"
    dest: "/home/{{ utilisateur }}/.gitconfig"
    owner: "{{ utilisateur }}"
    group: "{{ utilisateur }}"

- name: Fichier zshrc
  copy:
    src: "zshrc"
    dest: "/home/{{ utilisateur }}/.zshrc"
    owner: "{{ utilisateur }}"
    group: "{{ utilisateur }}"

- name: Fichier bashrc
  copy:
    src: "bashrc"
    dest: "/home/{{ utilisateur }}/.bashrc"
    owner: "{{ utilisateur }}"
    group: "{{ utilisateur }}"

- name: Fichier zshrc Root
  copy:
    src: "zshrc"
    dest: "/root/.zshrc"
    owner: "root"
    group: "root"

- name: Fichier bashrc Root
  copy:
    src: "bashrc"
    dest: "/root/.bashrc"
    owner: "root"
    group: "root"

# Copie et création de la tache Cron pour MAJ auto
- name: script de lancement de ansible
  template:
    src: Mirim.sh.j2
    dest: /usr/bin/Mirim.sh
    owner: root
    group: root
    mode: 0755

- cron:
    name: lancement de ansible-pull
    weekday: "*"
    minute: "0"
    hour: "20"
    user: root
    job: "/bin/bash /usr/bin/Mirim.sh"

- name: Rotation de log sur ansible.log
  copy:
    src: logrotate_ansible
    dest: /etc/logrotate.d/ansible

- name: Git Clone PowerLine
  git:
    repo: https://github.com/b-ryan/powerline-shell.git
    dest: /root/powerline
    # single_branch: yes
    version: master

- name: Installation Powerline
  ansible.builtin.script: /root/powerline/setup.py install
  args:
    executable: python3
    chdir: /root/powerline

 

Variables

utilisateur: "user"
password: "password"
mysql_user: "user"
mysql_password: "password"
root_password: "password"
debian_version: "bookworm"

 

Script Bash

Le script Bash

#!/bin/bash
### VARIABLES ###
ANSIBLE_PATH="/etc/ansible";
TMP_PATH="/tmp/MirimAnsible";
### SCRIPT ###
/usr/bin/apt install -y ansible git sudo
if [ ! -d $ANSIBLE_PATH ];then
echo "Création du $ANSIBLE_PATH !";
/usr/bin/mkdir $ANSIBLE_PATH;
fi
if [ ! -d $TMP_PATH ];then
echo "Création du $TMP_PATH !";
/usr/bin/mkdir $TMP_PATH;
fi
/usr/bin/echo '['${HOSTNAME:0:10}']' > $TMP_PATH/hosts
/usr/bin/echo $HOSTNAME >> $TMP_PATH/hosts
/usr/bin/sudo /usr/bin/cp $TMP_PATH/hosts /etc/ansible/
/usr/bin/chmod 777 $ANSIBLE_PATH/hosts
# Lancement de la commande de lancement d'Ansible.
ansible-pull --sleep 10 --checkout=main --directory=$TMP_PATH/ansible --url=https://user:password@git.serveur.fr/xxx/ConfigServeur.git main.yml
/usr/bin/rm -Rf $TMP_PATH $TMP_PATH/ansible

 

Publié le
Modifié le