Ansible is an open-source software that uses python to provide an automation language. It uses YAML declarative files known as playbooks to describe tasks that are executed by Ansible. Ansible can configure computer systems, deploy software, and automate IT tasks at scale. Ansible does not require software to be installed on managed systems and utilizes protocols such as OpenSSH and WinRM to connect to systems. The official documentation has more detailed information on how to get started.
Installing Ansible
Ansible can be installed on non-Windows operating systems using pip, the Python package manager.
---# Target all hosts from an inventory- hosts:all# Patching requires root privilegesbecome:yes# Check the operating system, this playbook only supports RedHat and Debian based distrospre_tasks:- name:OS Checkdebug:msg:System {{ inventory_hostname }} not supportedverbosity:1when:(ansible_facts['os_family'] != "RedHat") or (ansible_facts['os_family'] != "Debian")# Check for yum-utils on EL 7 hosts- name:Check for yum-utilsyum:name:'yum-utils'state:presentwhen:(ansible_facts['os_family'] == "RedHat" and ansible_facts['distribution'] == "7")tasks:# update all packages with the apt package manager- name:apt - Upgrade everythingapt:update_cache:yesupgrade:distwhen:ansible_facts['os_family'] == "Debian"# update all packages with the yum package manager- name:yum - Upgrade everythingyum:name:"*"state:latestwhen:ansible_facts['os_family'] == "RedHat"# Check if the applied updates require a system reboot- name:check needs-restarting - el7command:'needs-restarting -r'failed_when:falseregister:needs_restartingchanged_when:needs_restarting.rc == 1notify:restartwhen:(ansible_facts['os_family'] != "RedHat" and ansible_facts['distribution'] == "7")- name:check reboot required - Ubuntushell:"[ -f /var/run/reboot-required ]"failed_when:falseregister:reboot_requiredchanged_when:reboot_required.rc == 0notify:restartwhen:(ansible_facts['distribution'] == "Ubuntu")# A handler is called when a task reports 'changed'handlers:- name:restartreboot:
You can create an inventory file to point Ansible at your systems in INI or YAML format.