Ansible Modules Cheat Sheet

From WikiOD

Here is the cheatsheat of Ansible Modules and basic guide on how to use Ansible Modules {% raw %}

Format[edit | edit source]

Basic file[edit | edit source]

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

Place your modules inside tasks.

Task formats[edit | edit source]

One-line[edit | edit source]

- apt: pkg=vim state=present

Map[edit | edit source]

- apt:
    pkg: vim
    state: present

Foldable scalar[edit | edit source]

- apt: >
    pkg=vim
    state=present

Define your tasks in any of these formats. One-line format is preferred for short declarations, while maps are preferred for longer.

Modules[edit | edit source]

Aptitude[edit | edit source]

Packages[edit | edit source]

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

Deb files[edit | edit source]

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

Repositories[edit | edit source]

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

Repository keys[edit | edit source]

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

git[edit | edit source]

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

See: git module

git_config[edit | edit source]

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

See: git_config module

user[edit | edit source]

- user:
    state: present
    name: git
    system: yes
    shell: /bin/sh
    groups: admin
    comment: "Git Version Control"

See: user module

service[edit | edit source]

- service:
    name: nginx
    state: started
    enabled: yes     # optional

See: service module

Shell[edit | edit source]

shell[edit | edit source]

- shell: apt-get install nginx -y

Extra options[edit | edit source]

- shell: echo hello
  args:
    creates: /path/file  # skip if this exists
    removes: /path/file  # skip if this is missing
    chdir: /path         # cd here before running

Multiline example[edit | edit source]

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

See: shell module

script[edit | edit source]

- script: /x/y/script.sh
  args:
    creates: /path/file  # skip if this exists
    removes: /path/file  # skip if this is missing
    chdir: /path         # cd here before running

See: script module

Files[edit | edit source]

file[edit | edit source]

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

    # Optional:
    owner: bin
    group: wheel
    mode: 0644
    recurse: yes  # mkdir -p
    force: yes    # ln -nfs

See: file module

copy[edit | edit source]

- copy:
    src: /app/config/nginx.conf
    dest: /etc/nginx/nginx.conf

    # Optional:
    owner: user
    group: user
    mode: 0644
    backup: yes

See: copy module

template[edit | edit source]

- template:
    src: config/redis.j2
    dest: /etc/redis.conf

    # Optional:
    owner: user
    group: user
    mode: 0644
    backup: yes

See: template module

Local actions[edit | edit source]

local_action[edit | edit source]

- name: do something locally
  local_action: shell echo hello

debug[edit | edit source]

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

See: debug module {% endraw %}

Credit:rstacruz