ansible 脚手架-3-使用 playbook 做个导演

playbook和运行方法

shell脚本一般以.sh结尾,保存了常用的命令,可以多次运行,ansible也有类似的脚本,用的是YAML格式,起了个名字叫playbook

运行方式:
ansible-playbook deploy.yml

用playbook部署apache

新建文件: deploy.yml

手动部署步骤:

  1. 安装apache包
  2. 拷贝httpd的配置文件,之后重启apache
  3. 拷贝默认的测试页面 index.html
  4. 启动apache服务

跟随手动部署的步骤,我们可以写playbook了,就好像导演安排演员演戏一样。

首先普及几个playbook的关键字,放心,很少,很简单。

  • hosts: 主机ip,或主机组名,或关键字all
  • remote_user:用户名,用来执行脚本
  • vars:变量
  • tasks:分派任务,定义一些列动作来执行playbook。每个动作叫做action,调用一个模块
    • action语法:
      • module:key=value
    • 常用模块:
      • yum
      • copy
      • template
      • ...
  • handers: 是playbook中的event,默认不执行。在action里触发才执行,并只执行一次,多次触发无效。

show me the code:

---
- hosts: web
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: pkg=httpd state=latest

  - name: Write the configuration file
    template: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart apache

  - name: Write the default index.html file
    template: src=templates/index.html.j2 dest=/var/www/html/index.html

  - name: ensure apache is running
    service: name=httpd state=started
  handlers:
    - name: restart apache
      service: name=httpd state=restarted

测试运行记录:

[root@master ~/ansible-note]# vim deploy_apache.yml # 写入上面的yml内容
[root@master ~/ansible-note]# mkdir templates
[root@master ~/ansible-note]# cd templates/
[root@master ~/ansible-note/templates]# echo "hello ansible">index.html.j2   # 创建html模板
[root@master ~/ansible-note/templates]# ll
total 4
-rw-r--r-- 1 root root 14 Dec 28 19:03 index.html.j2
[root@master ~/ansible-note/templates]# mv httpd.conf httpd.conf.j2
[root@master ~/ansible-note/templates]# vim httpd.conf.j2  # 修改默认配置
    
    1003 
    1004 #    ServerAdmin [email protected]
    1005     DocumentRoot /var/www/html
    1006     ServerName ansible.howie.wang
    1007 #    ErrorLog logs/dummy-host.example.com-error_log
    1008 #    CustomLog logs/dummy-host.example.com-access_log common
    1009 


[root@master ~/ansible-note/templates]# cd ..
[root@master ~/ansible-note]# ansible-playbook deploy_apache.yml 

PLAY [test] ********************************************************************

TASK [setup] *******************************************************************
ok: [10.129.204.250]

TASK [ensure apache is at the latest version] **********************************
changed: [10.129.204.250]

TASK [write the configuration file] ********************************************
changed: [10.129.204.250]

TASK [write the default index.html file] ***************************************
changed: [10.129.204.250]

TASK [ensure apache is running] ************************************************
fatal: [10.129.204.250]: FAILED! => {"changed": false, "failed": true, "msg": "httpd: apr_sockaddr_info_get() failed for client1\nhttpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName\n(98)Address already in use: make_sock: could not bind to address [::]:80\n(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80\nno listening sockets available, shutting down\nUnable to open logs\n"}

# 这里出错了,因为原机器占用80端口在运行nginx,下面去把nginx停一下。

RUNNING HANDLER [restart apache] ***********************************************
    to retry, use: --limit @/root/ansible-note/deploy_apache.retry

PLAY RECAP *********************************************************************
10.129.204.250             : ok=4    changed=3    unreachable=0    failed=1   

[root@master ~/ansible-note]# ansible test -a "service nginx stop"
10.129.204.250 | SUCCESS | rc=0 >>
Stopping nginx daemon: nginx
Stop nginx success.

[root@master ~/ansible-note]# ansible-playbook deploy_apache.yml 

PLAY [test] ********************************************************************

TASK [setup] *******************************************************************
ok: [10.129.204.250]

TASK [ensure apache is at the latest version] **********************************
ok: [10.129.204.250]

TASK [write the configuration file] ********************************************
ok: [10.129.204.250]

TASK [write the default index.html file] ***************************************
ok: [10.129.204.250]

TASK [ensure apache is running] ************************************************
changed: [10.129.204.250]

PLAY RECAP *********************************************************************
10.129.204.250             : ok=5    changed=1    unreachable=0    failed=0   

添加10.129.204.250 ansible.howie.wang到本地的hosts文件
打开浏览器,访问ansible.howie.wang

你会看到刚才写进html的内容:

hello ansible

成功!

最后,听说还有个网站能进行json和yaml的互转,简直不能再强大了!

提供json和yml互转的在线网站: http://www.json2yaml.com/

参考

https://ansible-book.gitbooks.io/ansible-first-book/content/ansibleyong_jiao_ben_guan_li_zhu_ji.html

你可能感兴趣的:(ansible 脚手架-3-使用 playbook 做个导演)