This is an old revision of the document!
Table of Contents
Ansible Cheatsheet
Basics
- Run a playbook:
ansible-playbook playbook.yml
- Run an ad-hoc command:
ansible <host> -m <module> -a “<arguments>”
- List available modules:
ansible-doc -l
- Check if a host is reachable:
ansible <host> -m ping
- List facts about a host:
ansible <host> -m setup
Inventory
- Specify inventory file:
ansible-playbook -i inventory_file playbook.yml
- Host group example in inventory file:
[web]
webserver1 webserver2
- Dynamic Inventory (AWS example):
ansible-playbook -i aws_ec2.py playbook.yml
Playbook Structure
- Simple playbook:
yaml
- –
- name: Install a package
hosts: web
tasks:
- name: Install nginx
ansible.builtin.yum:
name: nginx
state: present
- Playbook with variables:
yaml
- –
- name: Install package with variable
hosts: web
vars:
package_name: nginx
tasks:
- name: Install nginx
ansible.builtin.yum:
name: "{{ package_name }}"
state: present
- Playbook with roles:
yaml
- –
- name: Install and configure web server
hosts: web
roles:
- webserver
Common Modules
- Yum (Install package):
yaml
- name: Install package
ansible.builtin.yum:
name: nginx
state: present
- Copy (Copy file):
yaml
- name: Copy file
ansible.builtin.copy:
src: /local/path/to/file
dest: /remote/path/to/file
- Command (Run a command):
yaml
- name: Run a command
ansible.builtin.command: /bin/echo Hello World
- File (Change file attributes):
yaml
- name: Change file permissions
ansible.builtin.file:
path: /path/to/file
mode: '0644'
- Service (Manage services):
yaml
- name: Start nginx service
ansible.builtin.service:
name: nginx
state: started
Variables
- Define variables in playbook:
yaml
vars:
var_name: value
- Use variables in tasks:
yaml
- name: Install package
ansible.builtin.yum:
name: "{{ var_name }}"
state: present
- Define host variables in inventory:
[web]
webserver1 ansible_ssh_user=ubuntu
- Use facts in playbook:
yaml
- name: Print host IP
debug:
var: ansible_default_ipv4.address
Loops and Conditionals
- Loop over items:
yaml
- name: Install multiple packages
ansible.builtin.yum:
name: "{{ item }}"
state: present
loop:
- nginx
- vim
- When conditional:
yaml
- name: Install nginx if variable is true
ansible.builtin.yum:
name: nginx
state: present
when: install_nginx == true
- Loop with index:
yaml
- name: Print index of items
debug:
msg: "Item {{ item }} is at index {{ ansible_loop.index }}"
loop:
- one
- two
- three
Handlers
- Define a handler:
yaml
handlers:
- name: Restart nginx
ansible.builtin.systemd:
name: nginx
state: restarted
- Notify a handler:
yaml
- name: Modify nginx config
ansible.builtin.copy:
src: nginx.conf
dest: /etc/nginx/nginx.conf
notify: Restart nginx
Ansible Vault
- Create a new encrypted file:
ansible-vault create secret.yml
- Edit an encrypted file:
ansible-vault edit secret.yml
- Encrypt an existing file:
ansible-vault encrypt existing_file.yml
- Decrypt a file:
ansible-vault decrypt secret.yml
- Run a playbook with vault password:
ansible-playbook –ask-vault-pass playbook.yml
Useful Commands
- Check syntax of a playbook:
ansible-playbook –syntax-check playbook.yml
- Run playbook in check mode (dry-run):
ansible-playbook –check playbook.yml
- Run playbook with verbose output:
ansible-playbook -v playbook.yml
- Display detailed information about a module:
ansible-doc <module_name>
Resources
- [Ansible Documentation](https://docs.ansible.com/)
- [Ansible GitHub Repository](https://github.com/ansible/ansible)
