cheatsheets/ansible.md

102 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2014-02-25 10:32:14 +00:00
---
title: Ansible
2015-11-24 04:32:36 +00:00
category: Ansible
2014-02-25 10:32:14 +00:00
---
2017-08-28 15:51:51 +00:00
{% raw %}
2014-02-25 10:32:14 +00:00
## Getting started
### About
{: .-intro}
- <https://www.ansible.com/>
2014-02-25 10:32:14 +00:00
### Hosts
$ sudo mkdir /etc/ansible
$ sudo vim /etc/ansible/hosts
[example]
192.0.2.101
192.0.2.102
### Running a playbook
$ ansible-playbook playbook.yml
## Playbook files
### Tasks
2014-02-25 10:32:14 +00:00
- hosts: all
user: root
sudo: no
vars:
aaa: bbb
tasks:
- ...
handlers:
- ...
### Includes
tasks:
- include: db.yml
handlers:
- include: db.yml user=timmy
### Handlers
2014-02-25 10:32:14 +00:00
handlers:
- name: start apache2
action: service name=apache2 state=started
tasks:
- name: install apache
action: apt pkg=apache2 state=latest
notify:
- start apache2
### Vars
2014-02-25 10:32:14 +00:00
- host: lol
vars_files:
- vars.yml
vars:
project_root: /etc/xyz
tasks:
- name: Create the SSH directory.
file: state=directory path=${project_root}/home/.ssh/
only_if: "$vm == 0"
### Roles
2014-02-25 10:32:14 +00:00
- host: xxx
roles:
- db
- { role:ruby, sudo_user:$user }
- web
# Uses:
# roles/db/tasks/*.yml
# roles/db/handlers/*.yml
### Task: Failures
- name: my task
command: ...
register: result
failed_when: "'FAILED' in result.stderr"
ignore_errors: yes
changed_when: "result.rc != 2"
### Env vars
2014-06-07 15:07:14 +00:00
vars:
local_home: "{{ lookup('env','HOME') }}"
2014-02-25 10:32:14 +00:00
2017-08-28 15:51:51 +00:00
{% endraw %}