Ansible
Ansible is a configuration management and provisioning tool.
- Example:
$ ansible all -m ping -s -k -u vagrant
$ ansible all -s -m shell -a 'apt-get install nginx'
all -- Use all defined servers form the inventory file
-m ping -- Use the "ping" module, which simply runs the ping command and returns the results
-s -- Use "sudo" to run the commands
-k -- Ask for a password rather than use key-based authentication
-u -- Login into servers using user vagrant
-a -- The -a flag is used to pass any arguments to the module.
Modules
Ansible uses "modules" to accomplish most of its Tasks. Ansible modules ensure indempotence -- we can run the same Task over and over without affecting the final result.
$ ansible all -s -m apt -a 'pkg=nginx state=installed update_cache=true'
-a 'pkg=nginx state=installed update_cache=true': Provide the arguments for the apt-module, including the package name,
our desired end state and whether to update the package repository cache or not.
Basic PlayBook
playbook can run multiples Tasks and provide some advanced functionality that we would miss out on using ad-hoc commands.
Playbooks and Roles in Ansible all use Yaml.
- Save the following code as nginx.yml file.
---
- host: local
tasks:
- name: Install Nginx
apt: pkg=nginx state=installed update_cache=true
2. Execute the nginx.yml file as ansible-playbook.
$ ansible-playbook -s nginx.yml
-s Tell ansible to use sudo again, and then pass the Playbook file.
Handler
A handler is exactly the same as a Task, but it will