Ansible的playbook文件格式的基本语法为YAML语法。
注意事项:
YAML编写技巧:
本次练习环境可参考【Ansible】—— 搭建学习环境以及安装ansible
要求如下:
针对主机组dev
1> 安装nginx
2> 提供网页index.html,内容为"Hello nginx."
3> 启动并开机启动nginx
4> 防火墙规则
cd /ansible
vim nginx.yml
/*编写的文档内容为YML_1,在下个代码片*/
ansible-playbook nginx.yml
curl master
YML_1:
---
- hosts: dev
tasks:
- name: 1.Install Nginx Package
yum:
name: nginx
state: present
- name: 2.Provide web content
copy:
content: "Hello nginx.\n"
dest: /usr/share/nginx/html/index.html
- name: 3.Start and Enabled Nginx
service:
name: nginx
state: started
enabled: yes
- name: 4.Firewalld rule
firewalld:
port: 80/tcp
permanent: yes
immediate: yes
state: enabled
要求如下:
针对主机组dev
1> 安装nginx
2> 提供网页index.html,内容为"Hello nginx."
3> 启动并开机启动nginx
4> 防火墙规则
针对主机组test
1> 安装mariadb-server
2> 启动并开机启动mariadb
3> 防火墙规则
cd /ansible
vim multi.yml
/*编写的文档内容为YML_2,在下个代码片*/
ansible-playbook multi.yml
/*测试,切换到主机组中
使用 ansible test -m ping -o 命令可查看test组下的主机是哪台主机
通过查看,可知是ansiblecontrol主机*/
[root@ansiblecontrol ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
/*测试成功*/
YML_2:
---
- name: 1>Build Nginx Environment
hosts: dev
tasks:
- name: 1.Install Nginx Package
yum:
name: nginx
state: present
- name: 2.Provide web content
copy:
content: "Hello nginx.\n"
dest: /usr/share/nginx/html/index.html
- name: 3.Start and Enabled Nginx
service:
name: nginx
state: started
enabled: yes
- name: 4.Firewalld rule
firewalld:
port: 80/tcp
permanent: yes
immediate: yes
state: enabled
- name: 2>Build mariadb Environment
hosts: test
tasks:
- name: 1.Install mariadb-server
yum:
name: mariadb-server
state: present
- name: 2.Start and Enabled mariadb
service:
name: mariadb
state: started
enabled: yes
- name: 3.Firewalld rule
firewalld:
service: mysql
permanent: yes
immediate: yes
state: enabled
1> 将问题1中的nginx 在 playbook 文件内使用 vars,变量为pkg_name
修改nginx.yml文件
原文件不变,将下面代码修改到相应的位置。
---
- hosts: dev
vars:
pkg_name: nginx
tasks:
- name: 1.Install Nginx Package
yum:
name: "{
{ pkg_name }}"
state: present
- name: 2.Provide web content
copy:
content: "Hello {
{ pkg_name }}.\n"
dest: /usr/share/nginx/html/index.html
- name: 3.Start and Enabled Nginx
service:
name: "{
{ pkg_name }}"
state: started
enabled: yes
2> 将问题1中的nginx 在 playbook 文件内使用 vars_files,变量文件为vars.yml,变量为pkg_name
修改nginx.yml文件
原文件不变,将上部分代码替换为下部分代码。
vars:
pkg_name: nginx
------------------------------------
vars_files:
- vars.yml
cd /ansible
echo "pkg_name: nginx" > vars.yml
ansible-playbook nginx.yml
要求如下:
将hostname命令结果注册为变量info,第二个任务的debug模块输出
vim register.yml
/*编写的文档内容为YML_3,在下个代码片*/
ansible-playbook register.yml
YML_3:
---
- hosts: all
tasks:
- name: 1.Register vars
shell: hostname
register: info
- name: 2.Show vars
debug:
msg: hostname is {
{
info.stdout}}
测试结果如下:(只截取修改文件后的测试变化部分)
TASK [2.Show vars] *******************************************************************************************************************************************
ok: [master] => {
"msg": "hostname is master"
}
ok: [ansiblecontrol] => {
"msg": "hostname is ansiblecontrol"
}
ok: [web1] => {
"msg": "hostname is web1"
}
ok: [web2] => {
"msg": "hostname is web2"
}
要求如下:
配置模板文件host.j2 为所有的被管理机器生成文件/opt/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
被管理机器IP地址 主机名
cd /ansible
vim host.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{
{
ansible_default_ipv4.address }} {
{
inventory_hostname }}
//保存退出
vim template.yml
/*编写的文档内容为YML_4,在下个代码片*/
ansible-playbook template.yml
/*测试*/
ansible all -m shell -a "cat /opt/hosts"
YML_4:
---
- hosts: all
tasks:
- name: Test template
template:
src: host.j2
dest: /opt/hosts
测试结果如下:
ansiblecontrol | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.118.11 ansiblecontrol
web1 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.118.12 web1
web2 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.118.13 web2
master | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.118.10 master