ANSIBLE CHEAT SHEET - parthibann/My_wiki GitHub Wiki
Ansible is an open source Continuous Deployment, Configuration Management, & Orchestration tool. This tool aims to provide large productivity gains to a wide variety of automation challenges and is powerful enough to automate complex multi-tier IT application environments.
- Add ansible repository
sudo apt-add-repository ppa:ansible/ansible - Run the update command
sudo apt-get update - Install ansible package
sudo apt-get install ansible - Check ansible version
ansible -version
Ansible uses SSH to communicate between the nodes.
- Setting up the SSH command
sudo apt-get install openssh-server - Generating the SSH key
ssh-keygen - Copy the SSH key on the hosts
ssh-copy-id <hostname> - Check the SSH connection
ssh <hostname>
The below is an example inventory file, which you can refer to understand the various parameters.
| Host details | Description |
|---|---|
| ungrouped.example.com | An ungrouped host |
| [webservers] beta.example.com ansible_host = 10.0.0.5 github.example.com ansible_ssh_user = abc |
A group called webservers ssh to 10.0.0.5 ssh as user abc |
| [clouds] cloud.example.com fileuser = alice |
fileuser is a host variable |
| [mascow] beta.example.com telecom.example.com |
host (DNS will resolve) host (DNS will resolve) |
| [dev1:children] webservers clouds |
dev1 is a group containing All hosts in group webservers All hosts in group clouds |
| Patterns | Description |
|---|---|
| all | All hosts in inventory |
| * | All hosts in inventory |
| ungrouped | All hosts in inventory not appearing within a group |
| 10.0.0.* | All hosts with an ip starting 10.0.0.* |
| webservers | The group webservers |
| webservers:!moscow | Only hosts in webservers, not also in group mascow |
| webservers:&mascow | Only hosts in the group's webservers and mascow |
-
To run a shell command
ansible <inventory file> -a "/sbin/reboot" -f 20to run as root user, specify--becomeparameter, use-Kparameter in order to get password during runtime. -
Transfer a file directly to many servers
ansible <inventory file> -m copy -a "src=/etc/hosts dest=/tmp/hosts" -
To change the ownership and permissions on files
ansible <inventory file> -m file -a "dest=/srv/foo/a.txt mode=600
ansible <inventory file> -m file -a "dest=/srv/foo/a.txt mode=600 owner=example group=example -
To create directories
ansible <inventory file> -m file -a "dest=/path/to/c mode=755 owner=example group=example state=directory -
To delete directories (recursively) and delete files
ansible <inventory file> -m file -a "dest=/path/to/c state=absent -
To ensure that a package is installed, but doesn't get updated
ansible <inventory file> -m apt -a "name=acme state=present" -
To ensure that a package is installed to a specific version
ansible <inventory file> -m apt -a "name=acme=1.5 state=present" -
To ensure that a package at the latest version
ansible <inventory file> -m apt -a "name=acme=1.5 state=latest" -
To ensure that the package is not installed
ansible <inventory file> -m apt -a "name=acme=1.5 state=absent" -
To ensure a service is started on all webservers
ansible <inventory file> -m service -a "name=httpd state=started" -
To restart a service on all webservers
ansible <inventory file> -m service -a "name=httpd state=restarted" -
To ensure a service is stopped
ansible <inventory file> -m service -a "name=httpd state=stopped" -
Deploying from source control
ansible <inventory file> -m git -a "repo=https://foo.example.org/repo.git dest=/src/myapp version=HEAD