Ansible 101
Trishna Guha (@trishnag)
Associate Software Engineer
Ansible by Red Hat
Ansible is so simple but still powerful
-
Automation
-
Configuration Management
-
Application Deployment
Key Points
- Written in Python
- Uses SSH
- Agentless
Key Words
- Playbook
- Tasks
- Inventory
- Roles
Playbook
- Entrypoint of Automation
- Yaml File
---
- name: main playbook
hosts: localhost
become: yes
become_method: sudo
tasks:
- name: install httpd
dnf:
name: httpd
state: latest
Tasks
- Operations to be perfomed
---
- name: main playbook
hosts: localhost
become: yes
become_method: sudo
tasks:
- name: install httpd
dnf:
name: httpd
state: latest
- debug:
msg: "{{ result }}"
- ping:
Inventory
- Hosts file
- INI file with list of groups of hosts
[atomic]
192.168.1.1 ansible_ssh_private_key_file=<SSH_KEY_FIILE>
[nxos]
172.24.4.16 ansible_connection=local ansible_user=user ansible_ssh_pass=pass
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_user=mdehaan
Roles
-
Collection of tasks, playbooks
-
Run multiple times
-
One very large playbooks ?
-
Organize and Manage Files in maintainable manner
Variables
- Follow Variable Convention
- Keep Plays and Variables definition separate
# vars/guests.yml
---
domain: atomic-node
image: Fedora-Atomic-25-20170215.1.x86_64
cpu: 1
mem: 1536
os:
variant: fedora23
path: /tmp
Continued
---
# This playbook creates VM from the QCOW2 Image
- include_vars:
file: vars/guests.yml
- name: Copy qcow2 image to the instance qcow2
copy:
src: '{{ path }}/{{ image }}.qcow2'
dest: /var/lib/libvirt/images/{{ domain }}.qcow2
- name: Create VM from QCOW2
shell: virt-install --quiet --import --name={{ domain }}
--os-variant={{ os.variant }} --ram={{ mem }} --vcpus={{ cpu }}
--disk path=/var/lib/libvirt/images/{{ domain }}.qcow2,format=qcow2,bus=virtio
--disk path=/var/lib/libvirt/images/{{ domain }}.cidata.iso,device=cdrom,readonly=on
--network network=default --noautoconsole
- name: Make sure that the VM is running
virt: name={{ domain }} command=start
ignore_errors: yes
Single Playbook to Manage all
---
# Main Entrypoint
- name: Main Playbook
hosts: localhost
become: yes
become_method: sudo
tasks:
- include: create-vm.yml
- include: compose.yml
Make Ansible Repeat your task
---
# This playbook installs requirements
- name: Install the list of required packages
package: name={{ item }} state=installed
with_items:
- genisoimage
- libvirt
- libselinux-python
- qemu-img
- rpm-ostree
- ostree
- virt-install
- name: Download daemon rpm and Install
command: "{{ item }}"
with_items:
- wget -P /tmp http://libslack.org/daemon/download/daemon-0.6.4-1.x86_64.rpm
- rpm --install /tmp/daemon-0.6.4-1.x86_64.rpm
ignore_errors: yes
- name: Create directory structure
file: path={{ item }} state=directory mode=750
with_items:
- "/srv/repo"
- "/srv/cache"
- "{{ abs_path }}"
And a lot more....
https://docs.ansible.com
https://www.ansible.com
Source Code
- https://github.com/ansible/ansible
- https://github.com/ansible/ansible/blob/devel/CONTRIBUTING.md
- https://github.com/ansible/ansible/blob/devel/CODING_GUIDELINES.md
- https://github.com/ansible/ansible/tree/devel/docs/docsite/rst/dev_guide
Ansible 101
By Trishna Guha
Ansible 101
- 599