cheatsheets/ansible-modules.md

247 lines
3.5 KiB
Markdown
Raw Permalink Normal View History

2014-02-25 10:32:14 +00:00
---
title: Ansible modules
2015-11-24 04:32:36 +00:00
category: Ansible
2017-10-02 18:00:21 +00:00
prism_languages: [yaml]
updated: 2017-10-03
2014-02-25 10:32:14 +00:00
---
2017-10-02 18:01:05 +00:00
{% raw %}
2017-10-02 18:16:31 +00:00
## Format
2014-02-25 10:32:14 +00:00
2017-10-02 18:16:31 +00:00
### Basic file
2016-06-18 16:19:06 +00:00
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
---
- hosts: production
remote_user: root
tasks:
- ···
2017-10-02 17:57:50 +00:00
```
2014-02-25 10:32:14 +00:00
2017-10-02 18:16:31 +00:00
Place your modules inside `tasks`.
### Task formats
#### One-line
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- apt: pkg=vim state=present
2017-10-02 17:57:50 +00:00
```
2016-06-18 16:19:06 +00:00
2017-10-02 18:16:31 +00:00
#### Map
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- apt:
pkg: vim
state: present
2017-10-02 17:57:50 +00:00
```
2014-02-25 10:32:14 +00:00
2017-10-02 18:16:31 +00:00
#### Foldable scalar
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- apt: >
pkg=vim
2017-10-02 18:00:21 +00:00
state=present
```
2017-10-02 18:16:31 +00:00
Define your tasks in any of these formats. One-line format is preferred for short declarations, while maps are preferred for longer.
## Modules
2014-02-25 10:32:14 +00:00
2017-10-02 18:16:31 +00:00
### Aptitude
#### Packages
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- apt:
pkg: nodejs
state: present # absent | latest
update_cache: yes
force: no
2017-10-02 17:57:50 +00:00
```
2017-10-02 18:16:31 +00:00
#### Deb files
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- apt:
deb: "https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb"
2017-10-02 17:57:50 +00:00
```
2017-10-02 18:16:31 +00:00
#### Repositories
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- apt_repository:
repo: "deb https://··· raring main"
state: present
```
#### Repository keys
```yaml
- apt_key:
id: AC40B2F7
url: "http://···"
state: present
2017-10-02 17:57:50 +00:00
```
2014-02-25 10:32:14 +00:00
### git
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- git:
repo: git://github.com/
dest: /srv/checkout
version: master
depth: 10
bare: yes
2017-10-02 17:57:50 +00:00
```
2014-02-25 10:32:14 +00:00
See: [git module](https://devdocs.io/ansible/git_module)
2017-10-02 18:16:31 +00:00
### git_config
```yaml
- git_config:
name: user.email
scope: global # local | system
value: hi@example.com
```
See: [git_config module](https://devdocs.io/ansible/git_config_module)
2017-10-02 18:16:31 +00:00
2014-02-25 10:32:14 +00:00
### user
2017-10-02 18:16:31 +00:00
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- user:
state: present
name: git
system: yes
shell: /bin/sh
groups: admin
comment: "Git Version Control"
2017-10-02 17:57:50 +00:00
```
2014-02-25 10:32:14 +00:00
See: [user module](https://devdocs.io/ansible/user_module)
2017-10-02 18:16:31 +00:00
2014-02-25 10:32:14 +00:00
### service
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 18:16:31 +00:00
- service:
name: nginx
state: started
enabled: yes # optional
2017-10-02 17:57:50 +00:00
```
2014-02-25 10:32:14 +00:00
See: [service module](https://devdocs.io/ansible/service_module)
2017-10-02 18:16:31 +00:00
## Shell
2014-02-25 10:32:14 +00:00
### shell
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 17:57:50 +00:00
- shell: apt-get install nginx -y
2017-10-02 18:16:31 +00:00
```
#### Extra options
```yaml
- 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
```yaml
- shell: |
echo "hello there"
echo "multiple lines"
```
See: [shell module](https://devdocs.io/ansible/shell_module)
2017-10-02 18:16:31 +00:00
### script
```yaml
2017-10-02 17:57:50 +00:00
- script: /x/y/script.sh
2017-10-02 18:16:31 +00:00
args:
creates: /path/file # skip if this exists
removes: /path/file # skip if this is missing
chdir: /path # cd here before running
2017-10-02 17:57:50 +00:00
```
2014-02-25 10:32:14 +00:00
See: [script module](https://devdocs.io/ansible/script_module)
2017-10-02 18:16:31 +00:00
## Files
### file
```yaml
- 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](https://devdocs.io/ansible/file_module)
2017-10-02 18:16:31 +00:00
### copy
```yaml
- copy:
src: /app/config/nginx.conf
dest: /etc/nginx/nginx.conf
# Optional:
owner: user
group: user
mode: 0644
backup: yes
```
See: [copy module](https://devdocs.io/ansible/copy_module)
2017-10-02 18:16:31 +00:00
### template
```yaml
- template:
src: config/redis.j2
dest: /etc/redis.conf
# Optional:
owner: user
group: user
mode: 0644
backup: yes
```
See: [template module](https://devdocs.io/ansible/template_module)
2017-10-02 18:16:31 +00:00
## Local actions
2016-06-18 16:19:06 +00:00
### local_action
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 17:57:50 +00:00
- name: do something locally
local_action: shell echo hello
```
2017-02-20 09:25:13 +00:00
### debug
2017-10-02 18:00:21 +00:00
```yaml
2017-10-02 17:57:50 +00:00
- debug:
msg: "Hello {{ var }}"
```
2017-10-02 18:01:05 +00:00
See: [debug module](https://devdocs.io/ansible/debug_module)
2017-10-02 18:01:05 +00:00
{% endraw %}
2017-10-02 18:16:31 +00:00