Ansible 试用
简介
本文会简述 Ansible 的使用方式。Ansible 项目的目录结构如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # playbooks site.yml webservers.yml fooservers.yml roles/ common/ tasks/ handlers/ library/ files/ templates/ vars/ defaults/ meta/ webservers/ tasks/ defaults/ meta/
默认情况下,Ansible 将在角色中的每个目录中main.yml查找相关内容的文件(也main.yaml和main):
tasks/main.yml- 角色执行的任务的主要列表。
handlers/main.yml- 处理程序,可以在此角色内部或外部使用。
library/my_module.py- 可在此角色中使用的模块(有关更多信息,请参阅在角色中嵌入模块和插件)。
defaults/main.yml- 角色的默认变量(有关更多信息,请参阅使用变量)。这些变量在所有可用变量中具有最低优先级,并且可以很容易地被任何其他变量(包括库存变量)覆盖。
vars/main.yml- 角色的其他变量(有关更多信息,请参阅使用变量)。
files/main.yml- 角色部署的文件。
templates/main.yml- 角色部署的模板。
meta/main.yml- 角色的元数据,包括角色依赖项。
配置目标地址
在项目中会存在 hosts
或 hosts.yaml
文档,通过此文档可以指定安装服务的位置。
在官方实例中提供了 hosts.yaml
的四种样例:
1 2 3 4 5 6 7 8 9 all: hosts: green.example.com: ansible_ssh_host: 191.168 .100 .32 anyvariable: value blue.example.com: 192.168.100.1: 192.168.100.10:
1 2 3 4 5 6 7 8 9 webservers: hosts: alpha.example.org: beta.example.org: 192.168.1.100: 192.168.1.110: vars: http_port: 8080
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 webservers: hosts: gamma1.example.org: gamma2.example.org: testing: hosts: www[001:006].example.com: vars: testing1: value1 children: webservers: other: children: webservers: gamma3.example.org
注:testing 组包含下面所有主机:
gamma1.example.org
gamma2.example.org
gamma3.example.org
www001.example.com
www002.example.com
www003.example.com
www004.example.com
www005.example.com
www006.example.com
1 2 3 4 all: vars: commontoall: thisvar
配置运行文件
在项目中可以编写称为 playbook 的脚本文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 - hosts: all roles: - foo - bar - foo name: my heavily commented play gather_facts: yes remote_user: login_user become: True become_user: root become_method: sudo connection: ssh ignore_unreachable: yes vars: color: brown web: memcache: 192.168 .1 .2 httpd: apache mylist: - a - b - c is_color_blue: "{{ color == 'blue' }} " my_version: 1.2 .3 vars_files: - /srv/ansible/vars/vars_file.yml - vars/vars_file.yml - vars/{{something}}.yml - [ 'vars/{{platform}} .yml' , vars/default.yml ] - [ 'vars/{{host}} .yml' ] vars_prompt: - name: passphrase prompt: "Please enter the passphrase for the SSL certificate" private: yes - name: release_version prompt: "Please enter a release tag" private: no - name: package_version prompt: "Please enter a package version" default: '1.0' roles: tasks: - name: Check that the target can execute Ansible tasks action: ping - file: path=/tmp/secret mode=0600 owner=root group=root - name: Ensure secret is locked down file: path: /tmp/secret mode: '0600' owner: root group: root - name: Paint the server command: echo {{color }} - name: Ensure secret is locked down file: path: '{{secret_file}} ' mode: '0600' owner: root group: root vars: secret_file: /tmp/secret - debug: msg: "my_version is higher than 1.0.0" when: my_version is version('1.0.0', '>' ) - name: Attempt and graceful roll back demo block: - name: Print a message ansible.builtin.debug: msg: 'I execute normally' - name: Force a failure ansible.builtin.command: /bin/false - name: Never print this ansible.builtin.debug: msg: 'I never execute, due to the above task failing, :-(' rescue: - name: Print when errors ansible.builtin.debug: msg: 'I caught an error' - name: Force a failure in middle of recovery! >:-) ansible.builtin.command: /bin/false - name: Never print this ansible.builtin.debug: msg: 'I also never execute :-(' always: - name: Always do this ansible.builtin.debug: msg: "This always executes" - name: Template configuration file ansible.builtin.template: src: template.j2 dest: /etc/foo.conf - name: Ensure apache is at the latest version ansible.builtin.yum: name: httpd state: latest - name: Ensure apache is running ansible.builtin.service: name: httpd state: started - name: Do not count this as a failure ansible.builtin.command: /bin/false ignore_errors: yes register: bass_result - name: Fail task when both files are identical ansible.builtin.raw: diff foo/file1 bar/file2 register: diff_cmd failed_when: diff_cmd.rc == 0 or diff_cmd.rc >= 2 - name: Install cobbler ansible.builtin.package: name: cobbler state: present environment: http_proxy: http://proxy.example.com:8080 - name: Update the Apache config copy: src: httpd.conf dest: /etc/httpd/httpd.conf notify: Restart Apache - name: Update our app's configuration copy: src: myapp.conf dest: /etc/myapp/production.conf notify: - Restart Apache - Restart Redis - import_tasks: tasks/new_user.yml vars: myuser: sklar color: red - import_tasks: tasks/new_user.yml vars: myuser: mosh color: mauve - import_playbook: webservers.yml - name: Create a file named via variable in /tmp file: path=/tmp/{{item}} state=touched loop: - tangerine - lemon - name: Loop using a variable file: path=/tmp/{{item}} state=touched loop: '{{mylist}} ' vars: mylist: - tangerine - lemon - name: "shutdown all ubuntu" command: /sbin/shutdown -t now when: '{{is_ubuntu|bool}} ' - name: "shutdown the if host is in the government" command: /sbin/shutdown -t now when: "{{inventory_hostname in groups['government']}} " - name: "shutdown the if host is in the government" command: /sbin/shutdown -t now when: "{{'government' in group_names}} " - name: login in as postgres and dump all postgres databases shell: pg_dumpall -w -f /tmp/backup.psql remote_user: postgres become: False - name: login normally, but sudo to postgres to dump all postgres databases shell: pg_dumpall -w -f /tmp/backup.psql become: true become_user: postgres become_method: sudo - name: create tempfile local_action: shell dd if=/dev/urandom of=/tmp/random.txt count=100 - name: create tempfile shell: dd if=/dev/urandom of=/tmp/random.txt count=100 delegate_to: localhost handlers: - name: Restart nginx service: name: nginx state: restarted - name: redis restarter service: name: redis state: restarted listen: - Restart redis - name: restart application that should really be a service command: /srv/myapp/restart.sh listen: - Restart application - restart myapp - import_tasks: handlers/site.yml ...
使用自带的工具进行加密
1 2 3 4 5 6 7 ansible-vault [create (创建新)|decrypt (解密)edit (编辑加密文件encrypt (加密)rekey (修改口令)view (查看)] [options (选项)] [vaultfile.yml]
例如可以将主机与密码文件单独写出然后进行加密
1 ansible-vault encrypt hello.yml
配置用户的方式
可以使用如下的方式覆写:
在运行时使用 -u
参数
将用户相关信息存储在库中
将用户信息存储在配置文件中
设置环境变量
参考资料
官方文档
官方样例