Ansible - Deployer des packages

Il existe plusieurs façon de créer un répertoire avec Ansible dans un playbook.
Base de création d’un playbook : https://n0tes.fr/2023/02/12/Ansible-Playbook/

Déployer des packages

Déployer de façon générique

Le module ansible.builtin.package permet de déployer des packages dans un parc hétérogène (Debian/RedHat).

- name: Install ntpdate
ansible.builtin.package:
name: ntpdate
state: present

# This uses a variable as this changes per distribution.
- name: Remove the apache package
ansible.builtin.package:
name: "{{ apache }}"
state: absent

- name: Install the latest version of Apache and MariaDB
ansible.builtin.package:
name:
- httpd
- mariadb-server
state: latest

Déployer des packages avec yum

Gérer des packages YUM dans les distributions Linux basées sur RPM :

- name: Install the latest version of Apache
ansible.builtin.yum:
name: httpd
state: latest

- name: Install Apache >= 2.4
ansible.builtin.yum:
name: httpd>=2.4
state: present

- name: Install a list of packages (suitable replacement for 2.11 loop deprecation warning)
ansible.builtin.yum:
name:
- nginx
- postgresql
- postgresql-server
state: present

- name: Install a list of packages with a list variable
ansible.builtin.yum:
name: "{{ packages }}"
vars:
packages:
- httpd
- httpd-tools

- name: Remove the Apache package
ansible.builtin.yum:
name: httpd
state: absent

- name: Install the latest version of Apache from the testing repo
ansible.builtin.yum:
name: httpd
enablerepo: testing
state: present

- name: Install one specific version of Apache
ansible.builtin.yum:
name: httpd-2.2.29-1.4.amzn1
state: present

- name: Upgrade all packages
ansible.builtin.yum:
name: '*'
state: latest

- name: Upgrade all packages, excluding kernel & foo related packages
ansible.builtin.yum:
name: '*'
state: latest
exclude: kernel*,foo*

- name: Install the nginx rpm from a remote repo
ansible.builtin.yum:
name: http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state: present

- name: Install nginx rpm from a local file
ansible.builtin.yum:
name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state: present

- name: Install the 'Development tools' package group
ansible.builtin.yum:
name: "@Development tools"
state: present

- name: Install the 'Gnome desktop' environment group
ansible.builtin.yum:
name: "@^gnome-desktop-environment"
state: present

- name: List ansible packages and register result to print with debug later
ansible.builtin.yum:
list: ansible
register: result

- name: Install package with multiple repos enabled
ansible.builtin.yum:
name: sos
enablerepo: "epel,ol7_latest"

- name: Install package with multiple repos disabled
ansible.builtin.yum:
name: sos
disablerepo: "epel,ol7_latest"

- name: Download the nginx package but do not install it
ansible.builtin.yum:
name:
- nginx
state: latest
download_only: true

Déployer des packages avec apt

Gérer des packages APT dans les distributions Linux basées sur .DEB :

- name: Install apache httpd  (state=present is optional)
ansible.builtin.apt:
name: apache2
state: present

- name: Update repositories cache and install "foo" package
ansible.builtin.apt:
name: foo
update_cache: yes

- name: Remove "foo" package
ansible.builtin.apt:
name: foo
state: absent

- name: Install the package "foo"
ansible.builtin.apt:
name: foo

- name: Install a list of packages
ansible.builtin.apt:
pkg:
- foo
- foo-tools

- name: Install the version '1.00' of package "foo"
ansible.builtin.apt:
name: foo=1.00

- name: Update the repository cache and update package "nginx" to latest version using default release squeeze-backport
ansible.builtin.apt:
name: nginx
state: latest
default_release: squeeze-backports
update_cache: yes

- name: Install the version '1.18.0' of package "nginx" and allow potential downgrades
ansible.builtin.apt:
name: nginx=1.18.0
state: present
allow_downgrade: yes

- name: Install zfsutils-linux with ensuring conflicted packages (e.g. zfs-fuse) will not be removed.
ansible.builtin.apt:
name: zfsutils-linux
state: latest
fail_on_autoremove: yes

- name: Install latest version of "openjdk-6-jdk" ignoring "install-recommends"
ansible.builtin.apt:
name: openjdk-6-jdk
state: latest
install_recommends: no

- name: Update all packages to their latest version
ansible.builtin.apt:
name: "*"
state: latest

- name: Upgrade the OS (apt-get dist-upgrade)
ansible.builtin.apt:
upgrade: dist

- name: Run the equivalent of "apt-get update" as a separate step
ansible.builtin.apt:
update_cache: yes

- name: Only run "update_cache=yes" if the last one is more than 3600 seconds ago
ansible.builtin.apt:
update_cache: yes
cache_valid_time: 3600

- name: Pass options to dpkg on run
ansible.builtin.apt:
upgrade: dist
update_cache: yes
dpkg_options: 'force-confold,force-confdef'

- name: Install a .deb package
ansible.builtin.apt:
deb: /tmp/mypackage.deb

- name: Install the build dependencies for package "foo"
ansible.builtin.apt:
pkg: foo
state: build-dep

- name: Install a .deb package from the internet
ansible.builtin.apt:
deb: https://example.com/python-ppq_0.1-1_all.deb

- name: Remove useless packages from the cache
ansible.builtin.apt:
autoclean: yes

- name: Remove dependencies that are no longer required
ansible.builtin.apt:
autoremove: yes

- name: Run the equivalent of "apt-get clean" as a separate step
apt:
clean: yes

Déployer sur Windows

Gérer des logiciels pour Windows. Les fichiers pris en charge sont de type .exe, .msi, .msp, .appx, .appxbundle, .msix et .msixbundle :

- name: Install the Visual C thingy
ansible.windows.win_package:
path: http://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x64.exe
product_id: '{CF2BEA3C-26EA-32F8-AA9B-331F7E34BA97}'
arguments: /install /passive /norestart

- name: Install Visual C thingy with list of arguments instead of a string
ansible.windows.win_package:
path: http://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x64.exe
product_id: '{CF2BEA3C-26EA-32F8-AA9B-331F7E34BA97}'
arguments:
- /install
- /passive
- /norestart

- name: Install MSBuild thingy with arguments split to prevent quotes
ansible.windows.win_package:
path: https://download.visualstudio.microsoft.com/download/pr/9665567e-f580-4acd-85f2-bc94a1db745f/vs_BuildTools.exe
product_id: '{D1437F51-786A-4F57-A99C-F8E94FBA1BD8}'
arguments:
- --norestart
- --passive
- --wait
- --add
- Microsoft.Net.Component.4.6.1.TargetingPack
- --add
- Microsoft.Net.Component.4.6.TargetingPack

- name: Install Remote Desktop Connection Manager from msi with a permanent log
ansible.windows.win_package:
path: https://download.microsoft.com/download/A/F/0/AF0071F3-B198-4A35-AA90-C68D103BDCCF/rdcman.msi
product_id: '{0240359E-6A4C-4884-9E94-B397A02D893C}'
state: present
log_path: D:\logs\vcredist_x64-exe-{{lookup('pipe', 'date +%Y%m%dT%H%M%S')}}.log

- name: Uninstall Remote Desktop Connection Manager
ansible.windows.win_package:
product_id: '{0240359E-6A4C-4884-9E94-B397A02D893C}'
state: absent

- name: Install Remote Desktop Connection Manager locally omitting the product_id
ansible.windows.win_package:
path: C:\temp\rdcman.msi
state: present

- name: Uninstall Remote Desktop Connection Manager from local MSI omitting the product_id
ansible.windows.win_package:
path: C:\temp\rdcman.msi
state: absent

# 7-Zip exe doesn't use a guid for the Product ID
- name: Install 7zip from a network share with specific credentials
ansible.windows.win_package:
path: \\domain\programs\7z.exe
product_id: 7-Zip
arguments: /S
state: present
become: yes
become_method: runas
become_flags: logon_type=new_credential logon_flags=netcredentials_only
vars:
ansible_become_user: DOMAIN\User
ansible_become_password: Password

- name: Install 7zip and use a file version for the installation check
ansible.windows.win_package:
path: C:\temp\7z.exe
creates_path: C:\Program Files\7-Zip\7z.exe
creates_version: 16.04
state: present

- name: Uninstall 7zip from the exe
ansible.windows.win_package:
path: C:\Program Files\7-Zip\Uninstall.exe
product_id: 7-Zip
arguments: /S
state: absent

- name: Uninstall 7zip without specifying the path
ansible.windows.win_package:
product_id: 7-Zip
arguments: /S
state: absent

- name: Install application and override expected return codes
ansible.windows.win_package:
path: https://download.microsoft.com/download/1/6/7/167F0D79-9317-48AE-AEDB-17120579F8E2/NDP451-KB2858728-x86-x64-AllOS-ENU.exe
product_id: '{7DEBE4EB-6B40-3766-BB35-5CBBC385DA37}'
arguments: '/q /norestart'
state: present
expected_return_code: [0, 666, 3010]

- name: Install a .msp patch
ansible.windows.win_package:
path: C:\Patches\Product.msp
state: present

- name: Remove a .msp patch
ansible.windows.win_package:
product_id: '{AC76BA86-A440-FFFF-A440-0C13154E5D00}'
state: absent

- name: Enable installation of 3rd party MSIX packages
ansible.windows.win_regedit:
path: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock
name: AllowAllTrustedApps
data: 1
type: dword
state: present

- name: Install an MSIX package for the current user
ansible.windows.win_package:
path: C:\Installers\Calculator.msix # Can be .appx, .msixbundle, or .appxbundle
state: present

- name: Uninstall an MSIX package using the product_id
ansible.windows.win_package:
product_id: InputApp
state: absent

Documentation

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/package_module.html
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_repository_module.html
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html
https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_package_module.html

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_repository_module.html
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_repository_module.html

> Partager <