【Ansible】Playbook实例
Learn to build Ansible playbooks with our guide, one step at a time
In our previous posts, we introduced Ansible fundamentals, and dove deeper into Ansible playbooks. Now let’s learn to create an Ansible playbook step by step. Working with a playbook, we’ll go from deploying a simple HTML website to a complete LAMP stack.
Deploying Simple HTML Page
To deploy a simple HTML page, we need to ensure that apache is installed and configured on our host machine. So therefore, in this section we will:
- install Apache
- start the Apache service
- deploy a static webpage with images – This static webpage will leverage Ansible templates where it will display the text “Thank you for reading this post. My IP Address is <ip-address-of-instance>” and cloudacademy logo. To fetch the IP address of host, it will rely on Ansible Fact
- restart Apache once the deployment is over
Before we move forward, let’s have a look at the high-level structure of this simple Ansible playbook.
1
2
3
4
5
6
7
8
9
10
11
12
|
site.yml – starting point of our ansible playbook
hosts – carrying hosts information
roles/ - defining what each type of server has to perform
webservers/
tasks/ - tasks performed on webservers
main.yml
handlers/ - running tasks under particular events
main.yml
templates/ - configuration files which can reference variables
index.html.j2
files/ - files to be copied to webservers
cloud.png
|
Lets go through the configuration file line by line and see how configuration works.
hosts – points to Ansible hosts. Here’s a possible syntax:
1
2
|
[webservers]
10.0.0.156
|
site.yml – the starting point for executing our Ansible playbook. Includes information about hosts and roles associated with them.
1
2
3
4
5
6
7
|
---
- name: install and configure webservers
hosts: webservers
remote_user: ec2-user
sudo: yes
roles:
- webservers
|
If we want to log into our host machines using a different username and with sudo privileges, we need to use the “remote_user” and “sudo: yes” parameter in our site.yml file. There can be additional parameters too, but they’re not needed right now. Here, we have also defined roles granted to hosts in the [webservers] group.
main.yml (Tasks) – This configuration file defines tasks to be executed on hosts that have webservers roles granted. It looks like:
1
2
3
4
5
6
7
8
9
10
11
|
---
# This task installs and enables apache on webservers
- name: ensure apache is installed
yum: pkg=httpd state=latest
- name: ensure apache is running
service: name=httpd state=running enabled=yes
- name: copy files to document root
copy: src=cloud.png dest=/var/www/html/cloud.png
- name: copy application code to document root
template: src=index.html.j2 dest=/var/www/html/index.html
notify: restart apache
|
Since YAML files are so intuitive, we can easily see that this will install and run Apache on host instances and copy certain files and templates to the host’s document root.
main.yml (handlers) – This configuration file defines the action to be performed only upon notification of tasks or state changes. In main.yml (tasks), we defined notify: restart apache handler which will restart Apache once the files and templates are copied to hosts.
1
2
3
|
---
- name: restart apache
service: name=httpd state=restarted
|
index.html.j2 (template) – a file you can deploy on hosts. However, template files also include some reference variables which are pulled from variables defined as part of an Ansible playbook or facts gathered from the hosts. Our index.html.j2 file looks like a regular html webpage with a referenced variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<html>
<head>
<title>CloudAcademy Ansible Demo</title>
</head>
<body>
<h1>
Thank you for reading this post.
My IP Address is {{ ansible_eth0.ipv4.address }}
</h1>
<br/><br/><br/>
<p>
<img src="cloud.png" alt="CloudAcademy Logo"/>
</p>
</body>
</html>
|
We have declared a reference variable “{{ ansible_eth0.ipv4.address }}” which will print the IP address of the host on which this Ansible playbook is executed.
cloud.png (files) – The regular image file to be copied to hosts.
Once we have all the files created and present, we can execute an ansible-playbook command and configure our hosts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
build# ansible-playbook site.yml -i hosts
PLAY [install and configure webservers] ***************************************
GATHERING FACTS ***************************************************************
ok: [10.0.0.156]
TASK: [webservers | ensure apache is installed] *******************************
changed: [10.0.0.156]
TASK: [webservers | ensure apache is running] *********************************
changed: [10.0.0.156]
TASK: [webservers | copy files to document root] ******************************
changed: [10.0.0.156]
TASK: [webservers | copy application code to document root] *******************
changed: [10.0.0.156]
NOTIFIED: [webservers | restart apache] ***************************************
changed: [10.0.0.156]
PLAY RECAP ********************************************************************
10.0.0.156 : ok=6 changed=5 unreachable=0 failed=0
|
That’s it. We have installed Apache and deployed our webpage using host-based files. On browsing our host’s IP address, we will see our static webpage with the referenced variables value defined.
Deploying a PHP webpage configured to work with a MySQL database
So until now, we’ve installed and started Apache, deployed a static webpage, and restarted Apache using handlers. Now we will upgrade the functionality of our existing Ansible playbook by adding additional features. Specifically, we’ll:
- install php and related packages
- install mysql server
- create databases in mysql server
- grant privileges to databases
- deploy a php web page which will list the names of all the databases in our mysql server and print certain facts about our host.
This will modify the structure our existing Ansible playbook:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
site.yml – starting point of our ansible playbook
hosts – carrying hosts information
group_vars
all – carrying variables for groups
roles/ - defining what each type of server has to perform
webservers/
tasks/ - tasks performed on webservers
main.yml
handlers/ - running tasks under particular events
main.yml
templates/ - configuration files which can reference variables
index.php.j2
files/ - files to be copied to webservers
cloud.png
dbservers
tasks/
main.yml
|
all (group_vars) : contains group-specific variables. Currently, we have only one group i.e., all.
1
2
|
dbuser: ansible
dbpassword: 12345
|
hosts : We have to update our hosts file if the webserver and database server are configured on the same host.
1
2
|
[all]
10.0.0.156
|
site.yml : Once we have updated our hosts file with a new group “all”, we have to update our site.yml file which will grant the webserver and dbserver role to the “all” host group.
1
2
3
4
5
6
7
8
|
---
- name: install and configure webservers
hosts: all
remote_user: ec2-user
sudo: yes
roles:
- webservers
- dbservers
|
main.yml (tasks for webservers) : This YAML file will now install additional php related packages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
---
# These task installs and enables apache on webservers
- name: ensure apache,php related packages are installed
yum: name={{ item }} state=present
with_items:
- httpd
- php
- php-mysql
- name: ensure apache is running
service: name=httpd state=running enabled=yes
- name: copy files to document root
copy: src=cloud.png dest=/var/www/html/cloud.png
- name: copy application code to document root
template: src=index.php.j2 dest=/var/www/html/index.php
notify: restart apache
|
index.php.j2 (templates) : Instead of an html file, we’ve moved to index.php which includes application code to print names of all databases and other operating system related information:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<html>
<head>
<title>CloudAcademy Ansible Demo</title>
</head>
<body>
<h3>
Thank you for reading this post. My IP Address is {{ ansible_eth0.ipv4.address }}.
This is {{ ansible_system }} OS with {{ ansible_userspace_architecture }} architecture
</h3>
<p>
<strong>List of Databases:</strong> <br/>
<?php
//Spoiler: don't do this at home!
$dbobj = mysql_connect('{{ ansible_lo.ipv4.address }}', '{{ dbuser }}', '{{ dbpassword }}');
if (!$dbobj) { die('Could not connect: ' . mysql_error()); }
$result = mysql_query("SHOW DATABASES");
while ($res = mysql_fetch_assoc($result)){
echo $res['Database'] . "<br/>";
}
?>
</p>
<br/>
<p><img src="cloud.png" alt="CloudAcademy Logo"></p>
</body>
</html>
|
main.yml (tasks for dbservers) : This configuration file will install the mysql-server, and mysql python packages, create databases, and create database users.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
---
# These task installs and enables apache on webservers
- name: ensure mysql is installed
yum: name={{ item }} state=present
with_items:
- mysql-server
- MySQL-python
- name: ensure mysql is running
service: name=mysqld state=running enabled=yes
- name: create application database
mysql_db: name={{ item }} state=present
with_items:
- ansible_db01
- ansible_db02
- name: create application user
mysql_user: name={{ dbuser }} password={{ dbpassword }} priv=*.*:ALL state=present
|
That’s it. Our Ansible playbook to deploy a LAMP stack is now ready. We built up a playbook that will install Apache, php, mysql-server, create a mysql user and databases and deploy our application code which prints information about Ansible’s host and list of databases.
To execute this Ansible playbook on host, we will use the ansible-playbook command:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#ansible-playbook site.yml -i hosts
PLAY [install and configure webservers] ***************************************
GATHERING FACTS ***************************************************************
ok: [10.0.0.156]
TASK: [webservers | ensure apache,php related packages are installed] *********
changed: [10.0.0.156] => (item=httpd,php,php-mysql)
TASK: [webservers | ensure apache is running] *********************************
changed: [10.0.0.156]
TASK: [webservers | copy files to document root] ******************************
changed: [10.0.0.156]
TASK: [webservers | copy application code to document root] *******************
changed: [10.0.0.156]
TASK: [dbservers | ensure mysql is installed] *********************************
changed: [10.0.0.156] => (item=mysql-server,MySQL-python)
TASK: [dbservers | ensure mysql is running] ***********************************
changed: [10.0.0.156]
TASK: [dbservers | create application database] *******************************
changed: [10.0.0.156] => (item=ansible_db01)
changed: [10.0.0.156] => (item=ansible_db02)
TASK: [dbservers | create application user] ***********************************
changed: [10.0.0.156]
NOTIFIED: [webservers | restart apache] ***************************************
changed: [10.0.0.156]
PLAY RECAP *******************************************************************
10.0.0.156 : ok=10 changed=9 unreachable=0 failed=0
|
Browsing to our host IP address will display:
There’s lots more to learn about Ansible in future posts!
参考资料:https://cloudacademy.com/blog/building-ansible-playbooks-step-by-step/
【Ansible】Playbook实例的更多相关文章
- ansible playbook详解
ansible playbook是由yml语法书写,结构清晰,可读性强,所以必须掌握yml基础语法 语法 描述 缩进 YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成,不能使用tabs键 ...
- Ansible playbook API 开发 调用测试
Ansible是Agentless的轻量级批量配置管理工具,由于出现的比较晚(13年)基于Ansible进行开发的相关文档较少,因此,这里通过一些小的实验,结合现有资料以及源码,探索一下Ansible ...
- ansible playbook实践(四)-如何调试写好的playbook文件
有时,我们写了一个长长,功能很强悍的yaml文件,但是,我们有可能会担心,写的yaml文件是否正确,是否有漏洞危机,毕竟是要修改线上的机器,那么,有可能我们可以从以下几个检查维度来进行,确保在大规模应 ...
- ansible playbook批量改ssh配置文件,远程用户Permission denied
最近手里的数百台服务器需要改/etc/ssh/sshd_config的参数,禁止root直接登陆,也就是说 [root@t0 ~]# cat /etc/ssh/sshd_config | grep R ...
- ansible笔记(11):初识ansible playbook(二)
ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...
- ansible笔记(10):初识ansible playbook
ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...
- Ansible playbook 批量修改服务器密码 先普通后root用户
fsckzy Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...
- 写Ansible playbook添加zabbix被监控的对象
本主题达到的效果是能通过编写Ansible Playbook,创建zabbix主机组,把被监控的对象加入到zabbix监控系统中,同时链接到对象的模板. 1.准备工作 在zabbix服务器上面,我们需 ...
- Ansible playbook基础组件介绍
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
随机推荐
- Oracle win32_11gR2_client.zip
先将下载下来的ZIP文件解压,并运行setup.exe文件. 第一步:选择管理员(0MB)(A),然后点击下一步 第二步:选择语言,点击下一步 第三步:选择安装的路径,然后点击下一步 第四步:执行到第 ...
- 用Python开始机器学习(3:数据拟合与广义线性回归)
机器学习中的预测问题通常分为2类:回归与分类. 简单的说回归就是预测数值,而分类是给数据打上标签归类. 本文讲述如何用Python进行基本的数据拟合,以及如何对拟合结果的误差进行分析. 本例中使用一个 ...
- Linux设备驱动模型(sysfs)
<总线模型概述> 随着技术的发展,系统的拓扑结构也越来越复杂,对热插拔.跨平台移植性的要求越来越高,从Linux2.6内核开始提供全新的设备模型.将所有的驱动挂载到计算机的总线上(比如US ...
- UOJ 310 黎明前的巧克力(FWT)
[题目链接] http://uoj.ac/problem/310 [题目大意] 给出一个数集,A从中选择一些数,B从中选择一些数,不能同时不选 要求两者选择的数异或和为0,问方案数 [题解] 题目等价 ...
- 20162327WJH实验四——图的实现与应用
20162327WJH实验四--图的实现与应用 实 验 报 告 课程:程序设计与数据结构 班级: 1623 姓名: 王旌含 学号:20162327 成绩: 指导教师:娄嘉鹏 王志强 实验日期:11月2 ...
- 【洛谷】4180:【模板】严格次小生成树[BJWC2010]【链剖】【线段树维护最大、严格次大值】
P4180 [模板]严格次小生成树[BJWC2010] 题目描述 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当小C洋洋得意之时,小P又来泼小C冷水了.小P说, ...
- 【对比分析六】JavaScript中GET和POST的区别及使用场景
区别: GET:一般用于信息获取,使用URL传递参数,对所发送信息的数量也有限制,一般在2000个字符 POST:一般用于修改服务器上的资源,对所发送的信息没有限制 GET方式需要使用 Request ...
- opencv hog算子
梯度直方图特征(HOG) 是一种对图像局部重叠区域的密集型描述符, 它通过计算局部区域的梯度方向直方图来构成特征.Hog特征结合SVM分类器已经被广泛应用于图像识别中,尤其在行人检测中获得了极大的成功 ...
- weblogic安装使用: Could not Create the Java Virtual Machine
第一次使用weblogic,完全不明白是怎么一回事!找安装包花了大把时间!找到了不知道怎么安装 -- _ --||| 找了一篇安装文档<weblogic 安装部署手册.doc>, 位于:[ ...
- DU 4609 3-idiots FFT
题意还是比较好懂. 给出若干个木棍的长度,问这些木棍构成三角形的可能性. 那么公式很容易知道 就是这些木棍组成三角形的所有情况个数 除以 从n个木棍中取3个木棍的情况数量C(n, 3) 即可 但是很显 ...