概述

这周在工作中需要去修改 nginx 的配置,发现了同事在使用 ansible 管理者系统几乎所有的配置,从数据库的安装、nginx 的安装及配置。于是这周研究起了 ansible 的基础用法。回过头再去看他的工程,终于能看明白了,嘿嘿
前面已经总结了三篇文章了,本章做个总结:

资料就列这么多了,其实,过犹不及,基本找一个看着舒服的教程看完,常用的命令就能看懂了。

复杂示例

写出好的 ansible-playbook 还是要多阅读优秀的 playbook,这里先列出几个可供学习的示例资源:

  • ansible/ansible-examples 一个面向初学者的 ansible playbook 收集仓库
  • galaxy ansible 这里就有很多流行的应用示例了,进阶看

    列出一个感觉比较清晰简洁的例子

  • https://github.com/ansible/ansible-examples/tree/master/mongodb

  • https://sourcegraph.com/github.com/ansible/ansible-examples/-/blob/mongodb/site.yml 对上面 mongodb 项目的查看,方便的工具,推荐
    1. .
    2. ├── LICENSE.md
    3. ├── README.md
    4. ├── group_vars
    5. │ └── all
    6. ├── hosts
    7. ├── images
    8. │ ├── check.png
    9. │ ├── nosql_primer.png
    10. │ ├── replica_set.png
    11. │ ├── scale.png
    12. │ ├── sharding.png
    13. │ └── site.png
    14. ├── playbooks
    15. │ └── testsharding.yml
    16. ├── roles
    17. │ ├── common
    18. │ │ ├── files
    19. │ │ │ ├── 10gen.repo.j2
    20. │ │ │ ├── RPM-GPG-KEY-EPEL-6
    21. │ │ │ └── epel.repo.j2
    22. │ │ ├── handlers
    23. │ │ │ └── main.yml
    24. │ │ ├── tasks
    25. │ │ │ └── main.yml
    26. │ │ └── templates
    27. │ │ ├── hosts.j2
    28. │ │ └── iptables.j2
    29. │ ├── mongoc
    30. │ │ ├── files
    31. │ │ │ └── secret
    32. │ │ ├── tasks
    33. │ │ │ └── main.yml
    34. │ │ └── templates
    35. │ │ ├── adduser.j2
    36. │ │ ├── mongoc.conf.j2
    37. │ │ └── mongoc.j2
    38. │ ├── mongod
    39. │ │ ├── files
    40. │ │ │ └── secret
    41. │ │ ├── tasks
    42. │ │ │ ├── main.yml
    43. │ │ │ └── shards.yml
    44. │ │ └── templates
    45. │ │ ├── mongod.conf.j2
    46. │ │ ├── mongod.j2
    47. │ │ ├── repset_init.j2
    48. │ │ └── shard_init.j2
    49. │ └── mongos
    50. │ ├── files
    51. │ │ └── secret
    52. │ ├── tasks
    53. │ │ └── main.yml
    54. │ └── templates
    55. │ ├── enablesharding.j2
    56. │ ├── mongos.conf.j2
    57. │ ├── mongos.j2
    58. │ └── testsharding.j2
    59. ├── site.yml
    60. └── tree.txt
    61. 21 directories, 38 files
    site.yml 文件的内容如下: ```shell

This Playbook would deploy the whole mongodb cluster with replication and sharding.

  • hosts: all roles:

    • role: common
  • hosts: mongo_servers roles:

    • role: mongod
  • hosts: mongoc_servers roles:

    • role: mongoc
  • hosts: mongos_servers roles:

    • role: mongos
  • hosts: mongo_servers tasks:

    • include: roles/mongod/tasks/shards.yml ```