Ansible introduction#
Modules#
- Command module: Takes the command and executes it
- Shell module: Executes through a shell like /bin/sh
- Script module: Runs a local script on a remote node after transferring it.
- raw: executes a ssh command. useful for installation python3
Adhoc commands#
Examples:
- ansible all - ping
- ansible web -m command -a “uptime”
- ansible localhost -m setup
Static inventory#
Sample inventory:
[control]
control ansible_host=10.42.0.2
[web]
node-1 ansible_host=10.42.0.6
node-2 ansible_host=10.42.0.7
node-3 ansible_host=10.42.8
[haproxy]
haproxy ansible_host=10.42.0.100
[all:vars]
ansible_user=vagrant
ansible_ssh_private_key_file=~/.vagrant.d/insecure_pvt_keyTest adhoc commands#
- ansible all -i hosts.ini -u vagrant -m ping: to test if systems are up
- ansible all -i hosts.ini -u vagrant -m setup: to test if system is setup
- ansible webservers -i hosts -u vagrant -m yum -a “name=python36 state=present” -b: installing packages python. -a is for args for yum module, -b become root
- Ansible is idempotent
- ansible webservers -i hosts -u vagrant -m yum -a “name=python36 state=absent” -b: to remove python
Variables#
Ansible can work with metadata from various sources and manage their context in the form of variable. Can be facts, filepaths, package versions, etc.
Variable precedence (ansible 2.x):
- Extra vars
- Task vars
- Block vars
- Role and include vars
- Play vars_files
- Play vars_prompt
- Play vars
- Set_facts
- Registered vars
- Host facts
- Playbook host_vars
- Playbook group_vars
- Inventory host_vars
- Inventory group_vars
- Inventory vars
- Role defaults
get_url: download an archive git: clone a source code repo
Tasks#
Example tasks in a play
tasks:
- name: add cache dir
file:
path: /opt/cache
state: directory
- name: install nginx
yum:
name: nginx
state: latest
- name: restart nginx
service:
name: nginx
state: restartedHandler Tasks#
Handlers are special tasks that run at the end of the play if notified by another task (caused by change of state). If a configuration file gets changed notify a service restart it needs to run.
tasks:
- name: add cache dir
file:
path: /opt/cache
state: directory
- name: install nginx
yum:
name: nginx
state: latest
notify: restart nginx
handlers:
- name: restart nginx
service:
name: nginx
state: restartedPlays and Playbooks#
Plays are ordered sets of taks to execute against host selections from your inventory. A playbook is a file containing one or more plays
Playbook example:
---
- name: install and start apache
hosts: web
vars:
http_port:80
max_clients: 200
remote_user: root
tasks:
- name: install httpd
yum: pkg=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
- name: start httpd
service: name=httpd state=startedPlaybook files#
site.yml(or main.yml)
---
- name: install and start apache
hosts: webservers
remote_user: vagrant
become: yes
tasks:
- name: install epel repo
yum: name=epel-release state=present
- name: install python bindings for SELinux
yum: name={{item}} state=present
with_items:
- libselinux-python
- libsemanage-python
- name: test to see if SeLinux is running
command: getenforce
register: sestatus
changed_when: false
- name: install apache
yum: name=httpd state=present
- name: start apache
service: name=httpd state=started enabled=yes- ansible-playbook -i hosts site.yml: to run playbook (execute tasks from top to bottom)
Transition playbooks to roles#
Roles are a packages of closely related ansible content that can be shared more easily than plays alone
Roles directory structure#
- defaults:
- files:
- handlers:
- meta:
- molecule:
- tasks:
- templates:
- vars:
Creating a new role#
- ansible-galaxy init –help
- ansible-galaxy init role_name
- meta: flush_handler: runs the handler right now
Running multiple roles in a playbook#
Example:
---
- name: apply the common configuration to all hosts
hosts: all
remote_user: vagrant
become: yes
roles:
- common
- name: apply the database configuration
hosts: dbservers
remote_user: vagrant
become: yes
roles:
- mariadb
- name: apply the apache and the web roles
hosts: webservers
remote_user: vagrant
become: yes
roles:
- apache
- web
- name: apply the lb configuration
hosts: lbservers
remote_user: vagrant
become: yes
roles:
- haproxy