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=presentMapping Format
- apt:
pkg: vim
state: presentFolded Scalar Format
- apt: >
pkg=vim
state=presentYou 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: noDeb 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: presentRepository Key
- apt_key:
id: AC40B2F7
url: "http://···"
state: presentGit Related
- git:
repo: git://github.com/
dest: /srv/checkout
version: master
depth: 10
bare: yesReference: git module
Git Config
- git_config:
name: user.email
scope: global # local | system
value: hi@example.comReference: 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" # CommentReference: user module
Service Management
- service:
name: nginx # Service name
state: started # State: started
enabled: yes # Whether to enable at bootReference: service module
Shell Related
shell Command
- shell: apt-get install nginx -yExtra 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 executionMulti-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 executionReference: 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 linkReference: 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 backupReference: 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 backupReference: template module
Local Operations
Local Execution
- name: Execute operation locally
local_action: shell echo helloDebug Output
- debug:
msg: "Hello {{ var }}"Reference: debug module