OSDN Git Service

Regular updates
[twpd/master.git] / ansible.md
1 ---
2 title: Ansible
3 category: Ansible
4 ---
5
6 {% raw %}
7
8 ## Getting started
9
10 ### Hosts
11
12     $ sudo mkdir /etc/ansible
13     $ sudo vim /etc/ansible/hosts
14
15     [example]
16     192.0.2.101
17     192.0.2.102
18
19 ### Running a playbook
20
21     $ ansible-playbook playbook.yml
22
23 ## Tasks
24
25     - hosts: all
26       user: root
27       sudo: no
28       vars:
29         aaa: bbb
30       tasks:
31         - ...
32       handlers:
33         - ...
34
35 ### Includes
36
37     tasks:
38       - include: db.yml
39     handlers:
40       - include: db.yml user=timmy
41
42 ## Handlers
43
44     handlers:
45       - name: start apache2
46         action: service name=apache2 state=started
47
48     tasks:
49       - name: install apache
50         action: apt pkg=apache2 state=latest
51         notify:
52           - start apache2
53
54 ## Vars
55
56     - host: lol
57       vars_files:
58         - vars.yml
59       vars:
60         project_root: /etc/xyz
61       tasks:
62         - name: Create the SSH directory.
63           file: state=directory path=${project_root}/home/.ssh/
64           only_if: "$vm == 0"
65
66 ## Roles
67
68     - host: xxx
69       roles:
70         - db
71         - { role:ruby, sudo_user:$user }
72         - web
73
74     # Uses:
75     # roles/db/tasks/*.yml
76     # roles/db/handlers/*.yml
77
78 ### Task: Failures
79
80     - name: my task
81       command: ...
82       register: result
83       failed_when: "'FAILED' in result.stderr"
84
85       ignore_errors: yes
86
87       changed_when: "result.rc != 2"
88
89 ### Env vars
90
91     vars:
92       local_home: "{{ lookup('env','HOME') }}"
93
94 ## References
95
96   * [Intro](http://www.ansibleworks.com/docs/intro_configuration.html)
97   * [Modules](http://www.ansibleworks.com/docs/modules.html)
98
99 {% endraw %}