Configure a host
Introduction
Modules are the units of work in a playbook. Use fully qualified names such as ansible.builtin.apt so tasks resolve to the intended collection.
- Ansible playbooks (docs.ansible.com)
- Module index (docs.ansible.com)
- Delegation and local actions (docs.ansible.com)
- Installing Ansible (docs.ansible.com)
Install a package
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
Installs a package from the configured repositories.
See: apt module
Copy a config file
- name: Write an nginx config
ansible.builtin.copy:
content: "listen 8080;\n"
dest: /etc/nginx/listen.conf
mode: "0644"
Writes a static file from the controller.
See: copy module
Start a service
- name: Start nginx
ansible.builtin.service:
name: nginx
state: started
Leaves the service running without changing its boot setting.
See: service module
Playbook basics
First play
---
- name: Configure web servers
hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
Modules run under tasks; become escalates privileges.
See: Ansible playbooks
Task keywords
- name: Write nginx config
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart nginx
tags: [config]
- name: Start nginx
ansible.builtin.service:
name: nginx
state: started
when: ansible_facts['distribution'] == 'Debian'
Use when for conditions, tags for selection, and notify for handlers.
See: Playbook keywords
Module arguments
Short form
- ansible.builtin.debug: msg=hello
Mapping form
- ansible.builtin.debug:
msg: hello
Prefer the mapping form: it handles lists and nested options cleanly.
See: Ansible playbooks
Packages
Install packages
- name: Install packages
ansible.builtin.apt:
name: [nginx, curl]
state: present
update_cache: true
Pass a list to name so apt handles it in one transaction.
See: apt module
Remove and upgrade
Remove
- name: Remove a package
ansible.builtin.apt:
name: curl
state: absent
Upgrade
- name: Upgrade installed packages
ansible.builtin.apt:
upgrade: dist
update_cache: true
See: apt module
Local package files
- name: Install a downloaded deb
ansible.builtin.apt:
deb: /tmp/nginx.deb
See: apt module
APT repositories
- name: Add a signed APT repository
ansible.builtin.deb822_repository:
name: example
types: deb
uris: https://repo.example.com/debian
suites: stable
components: [main]
signed_by: https://repo.example.com/key.asc
deb822_repository manages the key and source together; apt_key and apt_repository are deprecated and removed in core 2.25.
Source control
Clone a repository
- name: Clone a repository
ansible.builtin.git:
repo: https://github.com/example/app.git
dest: /srv/app
version: main
depth: 1
Use HTTPS or SSH URLs; GitHub disabled unauthenticated git:// in 2022.
See: git module
Configure git
- name: Set a global git option
community.general.git_config:
name: user.email
scope: global
value: hi@example.com
git_config lives in community.general, not ansible-core; scope defaults to system.
See: git_config module
Users and services
User account
- name: Create a service account
ansible.builtin.user:
name: git
system: true
shell: /bin/sh
groups: [wheel]
append: true
comment: Git Version Control
append is required with groups in core 2.21+ and keeps existing memberships.
See: user module
Service state
- name: Start and enable nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
service is portable; use ansible.builtin.systemd_service for masking or daemon reloads.
See: service module
Shell and scripts
Shell command
- name: Append a line once
ansible.builtin.shell:
cmd: "echo hello >> /tmp/hello.txt"
creates: /tmp/hello.txt
Prefer ansible.builtin.command when pipes and redirects are not needed.
See: shell module
Multiline shell
- name: Run several commands
ansible.builtin.shell: |
echo "hello there"
echo "multiple lines"
See: shell module
Script file
- name: Run a local script on the target
ansible.builtin.script:
cmd: files/setup.sh
creates: /tmp/setup.done
The script is read from the controller; creates is checked on the target.
See: script module
Files
Directories
- name: Create a directory
ansible.builtin.file:
path: /etc/myapp
state: directory
mode: "0755"
See: file module
Recursive attributes and links
Apply attributes recursively
- name: Set ownership recursively
ansible.builtin.file:
path: /etc/myapp
state: directory
owner: root
group: root
recurse: true
Create a link
- name: Link the active config
ansible.builtin.file:
src: /etc/myapp/config.yml
dest: /etc/myapp/current.yml
state: link
force: true
state: directory creates missing parents; recurse applies attributes to contents and force: true replaces a file with a link.
See: file module
Copy files
- name: Copy a config file
ansible.builtin.copy:
src: files/nginx.conf
dest: /etc/nginx/nginx.conf
mode: "0644"
backup: true
See: copy module
Render templates
- name: Render a config file
ansible.builtin.template:
src: redis.conf.j2
dest: /etc/redis.conf
mode: "0644"
Use template for Jinja variables; copy for static files.
See: template module
Delegation and output
Run on the controller
- name: Read the controller hostname
ansible.builtin.command: hostname
delegate_to: localhost
The local_action: shorthand still works, but delegate_to is preferred.
Print messages
Fact
- name: Show a fact
ansible.builtin.debug:
var: "ansible_facts['distribution']"
Message
- name: Show the target
ansible.builtin.debug:
msg: "Deploying {{ inventory_hostname }}"
debug always runs on the control node.
See: debug module
0 Comments for this cheatsheet. Write yours!