OSDN Git Service

Regular updates
[twpd/master.git] / ansible-modules.md
1 ---
2 title: Ansible modules
3 category: Ansible
4 layout: 2017/sheet
5 prism_languages: [yaml]
6 updated: 2017-10-03
7 ---
8
9 {% raw %}
10
11 ## Format
12
13 ### Basic file
14
15 ```yaml
16 ---
17 - hosts: production
18   remote_user: root
19   tasks:
20   - ···
21 ```
22
23 Place your modules inside `tasks`.
24
25 ### Task formats
26
27 #### One-line
28
29 ```yaml
30 - apt: pkg=vim state=present
31 ```
32
33 #### Map
34
35 ```yaml
36 - apt:
37     pkg: vim
38     state: present
39 ```
40
41 #### Foldable scalar
42
43 ```yaml
44 - apt: >
45     pkg=vim
46     state=present
47 ```
48
49 Define your tasks in any of these formats. One-line format is preferred for short declarations, while maps are preferred for longer.
50
51 ## Modules
52
53 ### Aptitude
54
55 #### Packages
56
57 ```yaml
58 - apt:
59     pkg: nodejs
60     state: present # absent | latest
61     update_cache: yes
62     force: no
63 ```
64
65 #### Deb files
66
67 ```yaml
68 - apt:
69     deb: "https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb"
70 ```
71
72 #### Repositories
73
74 ```yaml
75 - apt_repository:
76     repo: "deb https://··· raring main"
77     state: present
78 ```
79
80 #### Repository keys
81
82 ```yaml
83 - apt_key:
84     id: AC40B2F7
85     url: "http://···"
86     state: present
87 ```
88
89 ### git
90
91 ```yaml
92 - git:
93     repo: git://github.com/
94     dest: /srv/checkout
95     version: master
96     depth: 10
97     bare: yes
98 ```
99
100 See: [git module](http://devdocs.io/ansible/git_module)
101
102 ### git_config
103
104 ```yaml
105 - git_config:
106     name: user.email
107     scope: global # local | system
108     value: hi@example.com
109 ```
110
111 See: [git_config module](http://devdocs.io/ansible/git_config_module)
112
113 ### user
114
115 ```yaml
116 - user:
117     state: present
118     name: git
119     system: yes
120     shell: /bin/sh
121     groups: admin
122     comment: "Git Version Control"
123 ```
124
125 See: [user module](http://devdocs.io/ansible/user_module)
126
127 ### service
128
129 ```yaml
130 - service:
131     name: nginx
132     state: started
133     enabled: yes     # optional
134 ```
135
136 See: [service module](http://devdocs.io/ansible/service_module)
137
138 ## Shell
139
140 ### shell
141
142 ```yaml
143 - shell: apt-get install nginx -y
144 ```
145
146 #### Extra options
147
148 ```yaml
149 - shell: echo hello
150   args:
151     creates: /path/file  # skip if this exists
152     removes: /path/file  # skip if this is missing
153     chdir: /path         # cd here before running
154 ```
155
156 #### Multiline example
157
158 ```yaml
159 - shell: |
160     echo "hello there"
161     echo "multiple lines"
162 ```
163
164 See: [shell module](http://devdocs.io/ansible/shell_module)
165
166 ### script
167
168 ```yaml
169 - script: /x/y/script.sh
170   args:
171     creates: /path/file  # skip if this exists
172     removes: /path/file  # skip if this is missing
173     chdir: /path         # cd here before running
174 ```
175
176 See: [script module](http://devdocs.io/ansible/script_module)
177
178 ## Files
179
180 ### file
181
182 ```yaml
183 - file:
184     path: /etc/dir
185     state: directory # file | link | hard | touch | absent
186
187     # Optional:
188     owner: bin
189     group: wheel
190     mode: 0644
191     recurse: yes  # mkdir -p
192     force: yes    # ln -nfs
193 ```
194
195 See: [file module](http://devdocs.io/ansible/file_module)
196
197 ### copy
198
199 ```yaml
200 - copy:
201     src: /app/config/nginx.conf
202     dest: /etc/nginx/nginx.conf
203
204     # Optional:
205     owner: user
206     group: user
207     mode: 0644
208     backup: yes
209 ```
210
211 See: [copy module](http://devdocs.io/ansible/copy_module)
212
213 ### template
214
215 ```yaml
216 - template:
217     src: config/redis.j2
218     dest: /etc/redis.conf
219
220     # Optional:
221     owner: user
222     group: user
223     mode: 0644
224     backup: yes
225 ```
226
227 See: [template module](http://devdocs.io/ansible/template_module)
228
229 ## Local actions
230
231 ### local_action
232
233 ```yaml
234 - name: do something locally
235   local_action: shell echo hello
236 ```
237
238 ### debug
239
240 ```yaml
241 - debug:
242     msg: "Hello {{ var }}"
243 ```
244
245 See: [debug module](http://devdocs.io/ansible/debug_module)
246 {% endraw %}
247