User Tools

Site Tools


servers:management_tools:ansible

This is an old revision of the document!


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

  1. name: Install a package

hosts: web

    tasks:
      - name: Install nginx
        ansible.builtin.yum:
          name: nginx
          state: present
 
  • Playbook with variables:

yaml

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

  1. name: Install and configure web server

hosts: web

    roles:
      - webserver
 

Common Modules

  • Yum (Install package):

yaml

  1. name: Install package

ansible.builtin.yum:

      name: nginx
      state: present
 
  • Copy (Copy file):

yaml

  1. name: Copy file

ansible.builtin.copy:

      src: /local/path/to/file
      dest: /remote/path/to/file
 
  • Command (Run a command):

yaml

  1. name: Run a command

ansible.builtin.command: /bin/echo Hello World

  • File (Change file attributes):

yaml

  1. name: Change file permissions

ansible.builtin.file:

      path: /path/to/file
      mode: '0644'
 
  • Service (Manage services):

yaml

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

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

  1. name: Print host IP

debug:

      var: ansible_default_ipv4.address
 

Loops and Conditionals

  • Loop over items:

yaml

  1. name: Install multiple packages

ansible.builtin.yum:

      name: "{{ item }}"
      state: present
    loop:
      - nginx
      - vim
 
  • When conditional:

yaml

  1. name: Install nginx if variable is true

ansible.builtin.yum:

      name: nginx
      state: present
    when: install_nginx == true
 
  • Loop with index:

yaml

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

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

servers/management_tools/ansible.1739285390.txt.gz · Last modified: 2025/02/11 14:49 by jmbargallo