[关闭]
@nalan90 2017-10-20T14:50:47.000000Z 字数 8989 阅读 712

Ansible快速入门

自动化运维


摘自:http://getansible.com
示例代码: https://github.com/ansible-book/playbook-examples-chapter3-ansible-advanced

配置文件

看一个完整的anbile配置文件例子,就能基本了解到ansible都能配置什么了:
https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg

对ansible配置文件里面的关键字不能完整理解,还可以参考关键词解释列表:
http://docs.ansible.com/ansible/intro_configuration.html#explanation-of-values-by-section

ansible的默认配置文件是/etc/ansible/ansible.cfg。
其实ansible会按照下面的顺序查找配置文件,并使用第一个发现的配置文件。

配置示例

  1. [ssh_connection]
  2. pipelining=True
  3. #ssh_args = -F ./ssh-bastion.conf -o ControlMaster=auto -o ControlPersist=30m
  4. #control_path = ~/.ssh/ansible-%%r@%%h:%%p
  5. [defaults]
  6. host_key_checking=False
  7. gathering = smart
  8. fact_caching = jsonfile
  9. fact_caching_connection = /tmp
  10. stdout_callback = skippy
  11. library = ./library
  12. callback_whitelist = profile_tasks

实验使用的配置

  1. ##ansible.cfg
  2. work:ansible ys$ cat /etc/ansible/ansible.cfg
  3. [defaults]
  4. host_key_checking = False ##关闭ssh指纹识别
  5. [ssh_connection]
  6. ssh_args=-o ForwardAgent=yes ##开启ssh-agent forward

远程主机的分组

简单的分组[]内是组名

  1. mail.example.com
  2. [webservers]
  3. foo.example.com
  4. bar.example.com
  5. [dbservers]
  6. one.example.com
  7. two.example.com
  8. three.example.com
  9. [webservers]
  10. www[01:50].example.com
  11. [databases]
  12. db-[a:f].example.com

分组usa的子组还可以是其它的组,
例如[usa:children]中还可以包含southeast子组,
[southeast:children]中还可以包含atlanta和releigh

  1. [atlanta]
  2. host1
  3. host2
  4. [raleigh]
  5. host2
  6. host3
  7. [southeast:children]
  8. atlanta
  9. raleigh
  10. [usa:children]
  11. southeast
  12. northeast
  13. southwest
  14. northwest

实验环境分组

  1. ##HostInventory 配置文件
  2. 默认的文件是:/etc/ansible/hosts,可通过-i指定文件
  3. work:ansible ys$ cat hosts
  4. [hadoop]
  5. 172.16.1.163
  6. 172.16.1.162
  7. [web]
  8. 172.16.1.121
  9. 172.16.1.122
  10. 172.16.1.123

远程主机的连接参数和变量
  1. ##指定Server的连接参数,其中包括连接方法,用户等。
  2. [targets]
  3. localhost ansible_connection=local
  4. other1.example.com ansible_connection=ssh ansible_user=mpdehaan
  5. other2.example.com ansible_connection=ssh ansible_user=mdehaan
  6. [atlanta]
  7. host1 http_port=80 maxRequestsPerChild=808
  8. host2 http_port=303 maxRequestsPerChild=909

所有可以指定的参数在文档中 http://docs.ansible.com/ansible/intro_inventory.html#list-of-behavioral-inventory-parameters

  1. ##为一个组指定变量
  2. [atlanta]
  3. host1
  4. host2
  5. [atlanta:vars]
  6. ntp_server=ntp.atlanta.example.com
  7. proxy=proxy.atlanta.example.com

示例命令
  1. ## 打印hello
  2. work:ansible ys$ ansible web -a "/bin/echo hello" -u zhangshuang -i hosts
  3. 172.16.1.123 | SUCCESS | rc=0 >>
  4. hello
  5. 172.16.1.122 | SUCCESS | rc=0 >>
  6. hello
  7. 172.16.1.121 | SUCCESS | rc=0 >>
  8. hello
  9. --------------------------------------------------
  10. ## ping 所有主机
  11. work:ansible ys$ ansible all -m ping -u zhangshuang -i hosts
  12. 172.16.1.162 | SUCCESS => {
  13. "changed": false,
  14. "ping": "pong"
  15. }
  16. 172.16.1.123 | SUCCESS => {
  17. "changed": false,
  18. "ping": "pong"
  19. }
  20. 172.16.1.122 | SUCCESS => {
  21. "changed": false,
  22. "ping": "pong"
  23. }
  24. 172.16.1.163 | SUCCESS => {
  25. "changed": false,
  26. "ping": "pong"
  27. }
  28. 172.16.1.121 | SUCCESS => {
  29. "changed": false,
  30. "ping": "pong"
  31. }
  32. --------------------------------------------------
  33. ## 文件拷贝
  34. work:ansible ys$ ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts" -u zhangshuang -i hosts
  35. 172.16.1.123 | SUCCESS => {
  36. "changed": true,
  37. "checksum": "f687508cb64fbe053d7c9e9ec72c4426dbb64903",
  38. "dest": "/tmp/hosts",
  39. "gid": 2006,
  40. "group": "zhangshuang",
  41. "md5sum": "080286ed718f4c63119b91820f434dd2",
  42. "mode": "0664",
  43. "owner": "zhangshuang",
  44. "size": 861,
  45. "src": "/home/zhangshuang/.ansible/tmp/ansible-tmp-1508404372.35-206978874040737/source",
  46. "state": "file",
  47. "uid": 2006
  48. }
  49. 172.16.1.122 | SUCCESS => {
  50. "changed": true,
  51. "checksum": "f687508cb64fbe053d7c9e9ec72c4426dbb64903",
  52. "dest": "/tmp/hosts",
  53. "gid": 2006,
  54. "group": "zhangshuang",
  55. "md5sum": "080286ed718f4c63119b91820f434dd2",
  56. "mode": "0664",
  57. "owner": "zhangshuang",
  58. "secontext": "unconfined_u:object_r:user_tmp_t:s0",
  59. "size": 861,
  60. "src": "/home/zhangshuang/.ansible/tmp/ansible-tmp-1508404373.17-143062131252567/source",
  61. "state": "file",
  62. "uid": 2006
  63. }
  64. 172.16.1.121 | SUCCESS => {
  65. "changed": true,
  66. "checksum": "f687508cb64fbe053d7c9e9ec72c4426dbb64903",
  67. "dest": "/tmp/hosts",
  68. "gid": 2003,
  69. "group": "zhangshuang",
  70. "md5sum": "080286ed718f4c63119b91820f434dd2",
  71. "mode": "0664",
  72. "owner": "zhangshuang",
  73. "secontext": "unconfined_u:object_r:user_tmp_t:s0",
  74. "size": 861,
  75. "src": "/home/zhangshuang/.ansible/tmp/ansible-tmp-1508404373.24-123352246327910/source",
  76. "state": "file",
  77. "uid": 2003
  78. }
  79. --------------------------------------------------
  80. ##添加指定用户 password为加密后的密码
  81. work:ansible ys$ ansible hadoop -m user -a "name=test1 password=fsfsdfxvcxv" -u zhangshuang -b -i hosts
  82. 172.16.1.163 | SUCCESS => {
  83. "changed": true,
  84. "comment": "",
  85. "createhome": true,
  86. "group": 2011,
  87. "home": "/home/test1",
  88. "name": "test1",
  89. "password": "NOT_LOGGING_PASSWORD",
  90. "shell": "/bin/bash",
  91. "state": "present",
  92. "system": false,
  93. "uid": 2011
  94. }
  95. 172.16.1.162 | SUCCESS => {
  96. "changed": true,
  97. "comment": "",
  98. "createhome": true,
  99. "group": 2011,
  100. "home": "/home/test1",
  101. "name": "test1",
  102. "password": "NOT_LOGGING_PASSWORD",
  103. "shell": "/bin/bash",
  104. "state": "present",
  105. "system": false,
  106. "uid": 2011
  107. }
  108. --------------------------------------------------
  109. ## 更新代码
  110. work:ansible ys$ ansible hadoop -m git -a "repo=git@git.sunfund.com:9douyu/9douyu.git dest=/home/zhangshuang/git/9douyu version=HEAD" -u zhangshuang -i hosts
  111. 172.16.1.162 | SUCCESS => {
  112. "after": "d9e3c9f9ce24b61776cfd54401e090db8b5c9451",
  113. "before": null,
  114. "changed": true,
  115. "warnings": []
  116. }
  117. 172.16.1.163 | SUCCESS => {
  118. "after": "d9e3c9f9ce24b61776cfd54401e090db8b5c9451",
  119. "before": null,
  120. "changed": true,
  121. "warnings": []
  122. }
  123. --------------------------------------------------
  124. ## 安装nginx
  125. work:ansible ys$ ansible hadoop -m yum -a "name=nginx state=present" -u zhangshuang -b -i hosts
  126. 172.16.1.163 | SUCCESS => {
  127. "changed": false,
  128. "msg": "",
  129. "rc": 0,
  130. "results": [
  131. "nginx-1:1.10.2-2.el7.x86_64 providing nginx is already installed"
  132. ]
  133. }
  134. 172.16.1.162 | SUCCESS => {
  135. "changed": false,
  136. "msg": "",
  137. "rc": 0,
  138. "results": [
  139. "nginx-1:1.10.2-2.el7.x86_64 providing nginx is already installed"
  140. ]
  141. }
  142. --------------------------------------------------
  143. ##启动nginx服务
  144. work:ansible ys$ ansible hadoop -m service -a "name=nginx state=started" -u zhangshuang -b -i hosts
  145. [zhangshuang@master 9douyu]$ ps -ef | grep nginx
  146. root 15880 15864 0 Sep13 pts/1 00:00:00 nginx: master process nginx -g daemon off;
  147. 101 15897 15880 0 Sep13 pts/1 00:00:00 nginx: worker process
  148. root 16107 16102 0 Sep13 pts/2 00:00:00 tail -f /var/log/nginx/access.log /var/log/nginx/error.log
  149. root 28035 1 0 17:17 ? 00:00:00 nginx: master process /usr/sbin/nginx
  150. nginx 28036 28035 0 17:17 ? 00:00:00 nginx: worker process
  151. nginx 28037 28035 0 17:17 ? 00:00:00 nginx: worker process
  152. zhangsh+ 28063 26475 0 17:18 pts/0 00:00:00 grep --color=auto nginx
  153. --------------------------------------------------
  154. ##启动10个并行进行执行重起
  155. $ansible all -a "/sbin/reboot" -f 10 -u zhangshuang -b -i hosts
  156. ##查看远程主机的全部系统信息!!!
  157. work:ansible ys$ ansible hadoop -m setup -u zhangshuang -b -i hosts

ansible-playbook(安装apache)

deploy.yml的功能为hadoop组的主机部署apache, 其中包含以下部署步骤:

playbook deploy.yml包含下面几个关键字,每个关键字的含义:

具体示例如下:

  1. ##目录结构
  2. work:ansible ys$ tree
  3. .
  4. ├── deploy.retry
  5. ├── deploy.yml
  6. ├── hosts
  7. └── templates
  8. ├── httpd.conf.j2
  9. └── index.html.j2
  10. --------------------------------------------------
  11. ##ansible-playbook文件
  12. work:ansible ys$ cat deploy.yml
  13. ---
  14. - hosts: hadoop
  15. vars:
  16. http_port: 80
  17. max_clients: 200
  18. remote_user: root
  19. tasks:
  20. - name: ensure apache is at the latest version
  21. yum: pkg=httpd state=latest
  22. - name: write the configuration file
  23. template: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  24. notify:
  25. - restart apache
  26. - name: write the default index.html file
  27. template: src=templates/index.html.j2 dest=/var/www/html/index.html
  28. - name: ensure apache is running
  29. service: name=httpd state=started
  30. handlers:
  31. - name: restart apache
  32. service: name=httpd state=restarted
  33. --------------------------------------------------
  34. ##模板文件
  35. httpd.conf
  36. https://github.com/ansible-book/playbook-examples-chapter3-ansible-advanced/blob/master/templates/httpd.conf.j2
  37. index.html
  38. https://github.com/ansible-book/playbook-examples-chapter3-ansible-advanced/blob/master/templates/index.html.j2
  39. --------------------------------------------------
  40. ##运行命令
  41. work:ansible ys$ ansible-playbook deploy.yml -i hosts -u zhangshuang -b
  42. PLAY [hadoop] ******************************************************************
  43. TASK [setup] *******************************************************************
  44. ok: [172.16.1.163]
  45. ok: [172.16.1.162]
  46. TASK [ensure apache is at the latest version] **********************************
  47. ok: [172.16.1.162]
  48. ok: [172.16.1.163]
  49. TASK [write the configuration file] ********************************************
  50. ok: [172.16.1.162]
  51. ok: [172.16.1.163]
  52. TASK [write the default index.html file] ***************************************
  53. ok: [172.16.1.162]
  54. ok: [172.16.1.163]
  55. TASK [ensure apache is running] ************************************************
  56. ok: [172.16.1.163]
  57. changed: [172.16.1.162]
  58. PLAY RECAP *********************************************************************
  59. 172.16.1.162 : ok=5 changed=1 unreachable=0 failed=0
  60. 172.16.1.163 : ok=5 changed=0 unreachable=0 failed=0
  61. --------------------------------------------------
  62. ##测试服务
  63. work:ansible ys$ curl http://172.16.1.162
  64. <html>
  65. <body>
  66. <div class="block" style="height: 99%;">
  67. <div class="centered">
  68. <h1>#46 Demo</h1>
  69. <p>Served by master (172.16.1.162).</p>
  70. </div>
  71. </div>
  72. </body>
  73. </html>
  74. work:ansible ys$ curl http://172.16.1.163
  75. <html>
  76. <body>
  77. <div class="block" style="height: 99%;">
  78. <div class="centered">
  79. <h1>#46 Demo</h1>
  80. <p>Served by slave1 (172.16.1.163).</p>
  81. </div>
  82. </div>
  83. </body>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注