Skip to content

Ansible Modules Cheat Sheet

Ansible is a powerful automation operations tool. This article will introduce the usage of its common modules.

Format

Basic File Structure

---
- hosts: production
  remote_user: root
  tasks:
  - ···

Please place your modules under tasks.

Task Format

Single Line Format

- apt: pkg=vim state=present

Mapping Format

- apt:
    pkg: vim
    state: present

Folded Scalar Format

- apt: >
    pkg=vim
    state=present

You can use any of the above formats to define tasks. For short declarations, the single line format is recommended; for longer declarations, the mapping format is recommended.

Modules

Aptitude

Package Management

- apt:
    pkg: nodejs
    state: present # absent | latest
    update_cache: yes
    force: no

Deb Package File

- apt:
    deb: "https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb"

Repository Management

- apt_repository:
    repo: "deb https://··· raring main"
    state: present

Repository Key

- apt_key:
    id: AC40B2F7
    url: "http://···"
    state: present

Git Related

- git:
    repo: git://github.com/
    dest: /srv/checkout
    version: master
    depth: 10
    bare: yes

Reference: git module

Git Config

- git_config:
    name: user.email
    scope: global # local | system
    value: hi@example.com

Reference: git_config module

User Management

- user:
    state: present    # State: present
    name: git        # Username
    system: yes      # System user
    shell: /bin/sh   # Login shell
    groups: admin    # Groups
    comment: "Git Version Control"  # Comment

Reference: user module

Service Management

- service:
    name: nginx      # Service name
    state: started   # State: started
    enabled: yes     # Whether to enable at boot

Reference: service module

Shell Related

shell Command

- shell: apt-get install nginx -y

Extra Options

- shell: echo hello
  args:
    creates: /path/file  # Skip if file exists
    removes: /path/file  # Skip if file does not exist
    chdir: /path        # Switch to this directory before execution

Multi-line Command Example

- shell: |
    echo "hello there"
    echo "multiple lines"

Reference: shell module

Script Execution

- script: /x/y/script.sh
  args:
    creates: /path/file  # Skip if file exists
    removes: /path/file  # Skip if file does not exist  
    chdir: /path        # Switch to this directory before execution

Reference: script module

File Operations

File Management

- file:
    path: /etc/dir
    state: directory # Type: directory|file|link|hard|touch|absent

    # Optional parameters:
    owner: bin      # Owner
    group: wheel    # Group
    mode: 0644      # Permissions
    recurse: yes    # Recursive creation
    force: yes      # Force creation of soft link

Reference: file module

File Copy

- copy:
    src: /app/config/nginx.conf   # Source file
    dest: /etc/nginx/nginx.conf   # Target location

    # Optional parameters:
    owner: user     # Owner
    group: user     # Group
    mode: 0644      # Permissions
    backup: yes     # Whether to backup

Reference: copy module

Template

- template:
    src: config/redis.j2       # Template source file
    dest: /etc/redis.conf      # Target location

    # Optional parameters:
    owner: user     # Owner
    group: user     # Group
    mode: 0644      # Permissions
    backup: yes     # Whether to backup

Reference: template module

Local Operations

Local Execution

- name: Execute operation locally
  local_action: shell echo hello

Debug Output

- debug:
    msg: "Hello {{ var }}"

Reference: debug module