Ansible(2) Ansible的playbook使用

playbook

  • playbook是由多个“play”组成的列表

  • play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓task无非就是调用ansible的一个moudle。

  • playbook采用YAML语言编写

在这里插入图片描述
YAML介绍

  • YAML是一个可读性高的用来表达资料顺序的格式,YAML参考了其它多种语言,包括:XML\C\Python\Perl以及电子邮件格式RFC2822等。Clark
    Evans在2001年在首次发表这种语言,另外Ingy dot Net 与 Oren Ben-kiki也是这种语言的共同设计者。
  • YAML Ain’t Markup Language,即YAML不是XML。不过,在开发的这种语言时,YAML的意思其实是:“Yet
    Another Markup Language”(仍是一种标及语言


特性:

  • YAML的可读性好

  • YAML和脚本语言的交互性好

  • YAML使用实现语言的数据类型

  • YAML有一个一致的信息模型

  • YAML易于实现

  • YAML可以给予流来处理

  • YAML表达能力强,扩展性好

更多的规范及内容可以查看官网:http://www.yaml.org

playbook核心元素

  • Hosts 执行的远程主机列表

  • Tasks 任务集

  • Varniables 内置变量或自定义变量在playbook中调用

  • Templates 模板,可替换模板文件中的变量并实现一些简单逻辑的文件

  • Handlers 和notity结合使用,由特定条件触发的操作,满足条件才执行否则不执行

  • tags 标签
    执行某条任务执行,用于选择运行playbook中的部分代码。ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此有些代码为测试其确实没有发生变化的时间依然会非常的长。此时如果确信其没有变化,就可以通过tags跳过此些代码片段

          ansible-playbook -t tagsname useradd.yml
    

1、创建一个安装httpd的yaml文件

[root@ansible ~]# vim httpd.yml
  - hosts: clienthosts           #主机组
   remote_user: root             #什么用户执行
   tasks:                        #任务组
   - name: yum install httpd     #说明\解释
     yum: name=httpd state=latest   #模块: 参数
   - name: systemctl start httpd #说明
     systemd: name=httpd state=started #模块: 参数
     
[root@ansible ~]# ansible-playbook httpd.yml

PLAY [clienthosts] ***********************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [123.207.166.69]

TASK [yum install httpd] *****************************************************************************************************************************
changed: [123.207.166.69]

TASK [systemctl start httpd] *************************************************************************************************************************
fatal: [123.207.166.69]: FAILED! => {"changed": false, "msg": "Unable to start service httpd: Job for httpd.service failed because the control process exited with error code. See \"systemctl status httpd.service\" and \"journalctl -xe\" for details.\n"}

PLAY RECAP *******************************************************************************************************************************************
123.207.166.69             : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

2、Handlers 和notity结合使用触发条件

Handlers
是task列表,这些task与前述的task没有 本质上的不同,用于当关注的资源发生变化时,才会采取一定的操作
Notify
此action可用于在每个play’的最后被触发,这样可避免多次有改变发生时每次都执行指定的操作,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作
在修改完文件后服务如果正在启动中,则不会再次执行启动,但这样就无法对修改的配置文件的新内容生效,需要重启服务,如下所示在copy文件下增加notify,只要copy配置文件这步执行就执行下面的handlers。

[root@ansible ~]# vim httpd.yml
  - hosts: clienthosts
   remote_user: root
   tasks:
     - name: yum install httpd
       yum: name=httpd state=latest
     - name: copy conf
       copy: src=/root/httpd.conf dest=/etc/httpd/conf backup=yes        #这一步修改了httpd服务的默认端口,把80端口修改成了81
       notify: restart service                         #这里要写handlers的name
     - name: systemctl start httpd
       systemd: name=httpd state=started enabled=yes
   handlers:
     - name: restart service
       service: name=httpd state=restarted

执行查看
[root@ansible ~]# ansible-playbook httpd.yml

PLAY [clienthosts] ***********************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [123.207.166.69]

TASK [yum install httpd] *****************************************************************************************************************************
ok: [123.207.166.69]

TASK [copy conf] *************************************************************************************************************************************
changed: [123.207.166.69]

TASK [systemctl start httpd] *************************************************************************************************************************
changed: [123.207.166.69]

RUNNING HANDLER [restart service] ********************************************************************************************************************
changed: [123.207.166.69]

PLAY RECAP *******************************************************************************************************************************************
123.207.166.69             : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@ansible ~]# ansible clienthosts -m shell -a "netstat -utpln | grep 81"
123.207.166.69 | CHANGED | rc=0 >>
tcp6       0      0 :::81                   :::*                    LISTEN      3990/httpd

可以看到服务的端口已变成81并且监听了。

3、tags使用

tags可以给某一个模块加标签,可以调用这个标签进行执行。多标签执行的话在-t后使用逗号分割标签名称,在yaml文件中可以多个动作公用一个标签,把标签名字都写成一样就可以。

[root@ansible ~]# vim httpd.yml
  - hosts: clienthosts
   remote_user: root
   tasks:
     - name: yum install httpd
       yum: name=httpd state=latest
       tags: installhttpd         #定义标签
     - name: copy conf
       copy: src=/root/httpd.conf dest=/etc/httpd/conf backup=yes
       notify: restart service
     - name: systemctl start httpd
       systemd: name=httpd state=started enabled=yes
       tags: starthttpd           #定义标签
   handlers:
     - name: restart service
       service: name=httpd state=restarted

执行测试,已starthttpd为例,先关闭httpd服务然后再执行标签

[root@ansible ~]# ansible clienthosts -m systemd -a "name=httpd state=stopped"     #systemd切换成service都可以
123.207.166.69 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "name": "httpd",
    "state": "stopped",
..............中间太多就省略了...............
        "WatchdogTimestampMonotonic": "950629359914",
        "WatchdogUSec": "0"
    }
}

[root@ansible ~]# ansible-playbook -t starthttpd httpd.yml       #多标签执行的话使用逗号分开就可以

PLAY [clienthosts] ***********************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [123.207.166.69]

TASK [systemctl start httpd] *************************************************************************************************************************
changed: [123.207.166.69]

PLAY RECAP *******************************************************************************************************************************************
123.207.166.69             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@ansible ~]#
[root@ansible ~]# ansible clienthosts -m shell -a "netstat -utpln | grep 81"
123.207.166.69 | CHANGED | rc=0 >>
tcp6       0      0 :::81                   :::*                    LISTEN      19422/httpd

4、变量的使用

[root@ansible ~]# vim lrzsz.yml
---
- hosts: clienthosts
  remote_user: root
  tasks:
  - name: install lrzsz
    yum: name={{ pkname }}

执行palybook
[root@ansible ~]# ansible-playbook -e 'pkname=lrzsz' lrzsz.yml   #-e后面指定我们刚在在yml文件中定义的pkname的变量值

PLAY [clienthosts] ***********************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [123.207.166.69]

TASK [install lrzsz] *********************************************************************************************************************************
ok: [123.207.166.69]

PLAY RECAP *******************************************************************************************************************************************
123.207.166.69             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@ansible ~]# ansible clienthosts -m shell -a "rpm -qa | grep lrzsz"            #查看是否安装了lrzsz
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command because yum, dnf or zypper is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
123.207.166.69 | CHANGED | rc=0 >>
lrzsz-0.12.20-36.el7.x86_64
[root@ansible ~]#

在playbook中定义变量

[root@ansible ~]# vim lrzsz.yml
---
- hosts: clienthosts
  remote_user: root
  vars:
    - pkname: lrzsz
  tasks:
  - name: install lrzsz
    yum: name={{ pkname }}

在独立的文件中定义

[root@ansible ~]# cat lrzsz_var.yml
pkname: lrzsz
[root@ansible ~]# cat lrzsz.yml
---
- hosts: clienthosts
  remote_user: root
  vars_files:
    - lrzsz_var.yml
  tasks:
  - name: install lrzsz
    yum: name={{ pkname }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值