ansible的playbook模式的简介

xiaoxiao2021-02-28  89

playbook与之前介绍过的ansible自带的命令行中加-m的那些模块有所不同,playbook使用YAML编写的脚本,比之前的更加灵活功能更加强大。

playbook的模式简介

在ansible官方网站提供了一个配置httpd的一个yaml脚本案例,如下

#选择的主机组,比如写在/etc/ansible/hosts下的组 - hosts: webserver #定义的一些参数 vars: user: www group: www maxclients: 2000 DocumentRoot: /var/www/html #远程控制的用户,使用root,如果是别的用户权限不够需要使用sudo remote_user: root #被控制的主机所需要执行的任务 tasks: #-name为这个任务的名称 - name: ensure apache is at the latest version #安装模块 yum: pkg=httpd state=latest - name: Apache config file #template模块用于传送文件,这里是配置文件 template: src=/home/zyc/httpd.conf.bak dest=/etc/httpd/conf/httpd.conf #一旦配置文件修改,触发服务重启 notify: - restart apache #service模块,开启httpd模块 - name: ensusre apache is running service: name=httpd state=started handlers: - name: restart apache service: name=httpd state=restarted

我们检查语法错误的时候可以使用

ansible-playbook /home/zyc/httpd.yml --list-hosts --list-tasks

执行写好的yaml脚本使用如下脚本

ansible-playbook -i /etc/ansible/hosts(主机组) httpd.yml(脚本文件名)

返回的结果为如下

PLAY [webserver] *************************************************************** TASK [Gathering Facts] ********************************************************* ok: [172.25.254.41] ok: [172.25.254.45] TASK [ensure apache is at the latest version] ********************************** changed: [172.25.254.41] changed: [172.25.254.45] TASK [Apache config file] ****************************************************** ok: [172.25.254.41] ok: [172.25.254.45] TASK [ensusre apache is running] *********************************************** changed: [172.25.254.41] changed: [172.25.254.45] PLAY RECAP ********************************************************************* 172.25.254.41 : ok=4 changed=2 unreachable=0 failed=0 172.25.254.45 : ok=4 changed=2 unreachable=0 failed=0
转载请注明原文地址: https://www.6miu.com/read-96231.html

最新回复(0)