Ansible is a tool which uses no agents, so it's easy to deploy - it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs.
Ansible is able to execute commands or playbooks across multiple instances simultaneously
Requirements:
ansible
package locallypython3
preferred on target host, but python2 avail as a workaround.
Install with:
sudo yum|apt-get|dnf install ansible
- usually
xxx install ansible
whatever your package manager of choice
You can run coomands / modules form the cli wtih no YAML needed.
- Ping -
ansible all -m ping
ping
- Checkall
hosts connectivity- Assumes Inventory file configured (see below)
- Using Modules -
ansible web-1.example.com -m yum -a "name=httpd state=installed"
- Run the
yum
module against the hostweb-1
to ensure thathttpd
is installed yum install -y httpd
- More Modules - https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html
- Run the
The inventory file is a lookup table for Ansible to use when targeting individual hosts or groups or machines. This sits on your local machine that you are pushing out to - you can also store this in a repo alongside your playbook.
- name => ip mapping (if no dns)
- name(s) => group mappings
- Deafult file configured
/etc/ansible/hosts
- Override in config. Example: https://github.com/ansible/ansible/blob/devel/examples/ansible.cfg
$> cat /etc/ansible/hosts
mail.example.com
mail.example.com
[webservers]
web-1.example.com
web-2.example.com
[dbservers]
db-1.example.com
db-2.example.com
db-3.example.com
install-ubuntu-packages.yml
- name: Basic system-wide software installation
hosts: all
become: yes
tasks:
- name: Add official git PPA
apt_repository:
repo: ppa:git-core/ppa
codename: "{{ dist_override | default(omit) }}"
filename: "git"
- name: add asciinema PPA
apt_repository:
repo: ppa:zanchey/asciinema
codename: "{{ dist_override | default(omit) }}"
filename: "asciinema"
- name: Install latest versions of apt packages
apt:
name:
- asciinema # for recording teminal sessions
- build-essential
- flip
- git
- htop
- ipython
Playbooks can import and chain other playbooks, a better way to go is using Roles
- import_playbook: general-system-config.yml
- import_playbook: dotfiles-and-cli-setup.yml
- import_playbook: install-ubuntu-packages.yml
You can have your own local roles in the same dir as your playbooks, or you can leverage the "Galaxy" / pacakge manager for Ansible Roles.
Example https://github.com/geerlingguy/ansible-role-mysql
See more https://www.ansible.com/overview/how-ansible-works
- Install the role
ansible-galaxy install geerlingguy.mysql
- Create Playbook -
mysql-install.yml
-
- hosts: database roles: - role: geerlingguy.mysql become: yes
-
- Create vars file -
default/vars.yml
-
mysql_user_home: /root mysql_user_name: root mysql_user_password: root
-
- More info on this example Role - https://galaxy.ansible.com/geerlingguy/mysql