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.

Installation:

  1. Add ansible repository sudo apt-add-repository ppa:ansible/ansible
  2. Run the update command sudo apt-get update
  3. Install ansible package sudo apt-get install ansible
  4. Check ansible version ansible -version

SSH Key Generation

Ansible uses SSH to communicate between the nodes.

  1. Setting up the SSH command sudo apt-get install openssh-server
  2. Generating the SSH key ssh-keygen
  3. Copy the SSH key on the hosts ssh-copy-id <hostname>
  4. Check the SSH connection ssh <hostname>

Ansible Inventory files

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

Ansible Hosts Patterns

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

Ansible Ad-Hoc Commands

  1. To run a shell command ansible <inventory file> -a "/sbin/reboot" -f 20 to run as root user, specify --become parameter, use -K parameter in order to get password during runtime.

  2. Transfer a file directly to many servers ansible <inventory file> -m copy -a "src=/etc/hosts dest=/tmp/hosts"

  3. 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

  4. To create directories ansible <inventory file> -m file -a "dest=/path/to/c mode=755 owner=example group=example state=directory

  5. To delete directories (recursively) and delete files ansible <inventory file> -m file -a "dest=/path/to/c state=absent

  6. To ensure that a package is installed, but doesn't get updated ansible <inventory file> -m apt -a "name=acme state=present"

  7. To ensure that a package is installed to a specific version ansible <inventory file> -m apt -a "name=acme=1.5 state=present"

  8. To ensure that a package at the latest version ansible <inventory file> -m apt -a "name=acme=1.5 state=latest"

  9. To ensure that the package is not installed ansible <inventory file> -m apt -a "name=acme=1.5 state=absent"

  10. To ensure a service is started on all webservers ansible <inventory file> -m service -a "name=httpd state=started"

  11. To restart a service on all webservers ansible <inventory file> -m service -a "name=httpd state=restarted"

  12. To ensure a service is stopped ansible <inventory file> -m service -a "name=httpd state=stopped"

  13. Deploying from source control ansible <inventory file> -m git -a "repo=https://foo.example.org/repo.git dest=/src/myapp version=HEAD

⚠️ **GitHub.com Fallback** ⚠️