Installation Ansible in Ubuntu which sample playbook
ppa:ansible/ansible repository
sudapt-add-repository -y ppa:ansible/ansible
sudapt-get update
sudapt-get install -y ansible
This installs Ansible globally.
Virtualenv's
This is my preferred way to install Ansible.
We can also use Pip to install virtualenv, which lets us install Python libraries in their own little environment that won't affect others (nor force us to install tools globally).
Here's how:
# Install python2.7 (Ubuntu 16.04 comes with python 3 out of the box) and Pip
sudapt-add-repository -y ppa:ansible/ansible
sudapt-get update
sudapt-get install -y ansible
This installs Ansible globally.
Virtualenv's
This is my preferred way to install Ansible.
We can also use Pip to install virtualenv, which lets us install Python libraries in their own little environment that won't affect others (nor force us to install tools globally).
Here's how:
# Install python2.7 (Ubuntu 16.04 comes with python 3 out of the box) and Pip
sudo apt-get install -y python2.7 python-pip
## Use Pip to install virtualenv
### -U updates it if the package is already installed
sudo pip install -U virtualenv
Once we have pip and virtualenv installed globally, we can get Ansible inside of a virtual environment:
# Go to my user's home directory,
# Go to my user's home directory,
# make a directory to play with ansible
cd ~/
mkdir ansible-play
cd ansible-play
# Create a python virtual environment
virtualenv .venv
mkdir ansible-play
cd ansible-play
# Create a python virtual environment
virtualenv .venv
# Enable the virtual environment
source .venv/bin/activate
# Then anything we intall with pip will be
# inside that virtual environment
pip install ansible
Those commands will install the latest stable Ansible 2 (as of this writing).
Later, when you're done, you can deactivate the virtualenv via the deactivate command
At any time, you can update ansible by running:
# Assumes the virtualenv is active - `source .venv/bin/activate`
# Assuming the virtualenv is active
pip install -U ansible
Later, when you're done, you can deactivate the virtualenv via the deactivate command
At any time, you can update ansible by running:
# Assumes the virtualenv is active - `source .venv/bin/activate`
# Assuming the virtualenv is active
pip install -U ansible
Basic Playbook Structure
---
- name: Example Playbook
hosts: web_servers # Target group from inventory
become: yes # Run as root/sudo
vars: # Define variables
http_port: 80
package_name: nginx
tasks:
- name: Ensure Nginx is installed
apt:
name: "{{ package_name }}"
state: present
update_cache: yes
- name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
Comments
Post a Comment