playbook

发布时间:2023年12月21日

playbook

list? together? nested with_items

Templates 模版

jinja模版架构,通过模版可以实现向模版文件传参(python转义)把占位符参数传到配置文件中去。

生成一个目标文本文件,传递变量到需要配置文件当中(web开发)。

httpd.conf.j2 在文件当中配置的是占位符(声明的变量)

/etc/ansible/hosts 配置了主机的占位符名称和j2文件中的占位符一致(定义参数:占位符的参数的参数声明好)

playbook当中,template模块来把参数传给目标主机的配置文件。

httpd

nginx的

cp httpd.conf /opt/http.conf.j2

vim httpd.conf.j2

42行 Listen {{http_port}}

95行 ServerName? {{server_name}}

119行 DocumentRoot {{root_dir}}

vim /etc/ansible/hosts

[webservers]

20.0.0.180 http_port=20.0.0.180:80 server_name=www.guoqi.com:80? root_dir=/etc/httpd/htdocs

[dbservers]

20.0.0.181?http_port=20.0.0.181:80 server_name=www.wdf.com:80? root_dir=/etc/httpd/htdocs

vim httpd.yml

- hosts: all

? remote_user: root

? vars:

? ? ? -? package:? httpd

? ? ? -? service :? ?httpd

tasks:

? ?-? name:? install? httpd

? ??? yum:? name={{package}}

? ?-? name:? install? configure? file

? ? ? template:? src=/opt/httpd.conf.j2? dest=/etc/httpd/conf/httpd.conf

#使用template的模版

? ? notify:

? ? ? ? -? restart? httpd

? ?-? name? create? root_dir

? ? ? file:

? ? ? ? path:? /etc/httpd/hdocs

? ? ? ? state:? directory

? ?-? name:? start? httpd

? ? ? service:? name={{service}}? enabled=true? state=started

handlers:

? ? -? name:? restart? httpd

? ? ? ?service:? name={{service}}? state=restarted

执行:ansible-playbook httpd.yml

ansible? -a? 192.168.233.20 'cat /etc/httpd/conf/httpd.conf'

到test2查看

vim /etc/httpd/conf/httpd.conf

查看42行:Listen 192.168.233.20:80

查看95行:ServerName? www.guoqi.com:80

查看119行: DocumentRoot? "/etc/httpd/htdocs"

vim /etc/ansible/hosts

yum -y install nginx

题:

vim nginx.html

server {

? ? listen? ? ? ? 80;

? ? #listen? ?8080

? ? server_name? ?;

? ? server_name改为www.guoqi.com

? ? root? ? /usr/share/nginx/html;? ?

? ? #root 改为/opt/nginx/html

}

.j2文件占位?

template传参

host文件定义

解答:vim nginx.conf

cp nginx.conf /opt/nginx.conf.j2

cd /opt/

vim nginx.conf.j2

server {

? ? listen? {{port}};

? ? server_name? {{server_name}};

? ? root? ? ? ? ? ? ? ? ?{{root_dir}};

}

vim /etc/ansible/hosts

tages模块:

模块标签,可以在playbook当中为任务设定标签(tages),我们在运行playbook时,可以通过指定任务标签,来实现只允许设定的标签的任务

-name:

tages:

? debug

--tags debug

--statr-at-task='wdf'

任务标签的种类:

always:不管你是否指定了运行标签,任务都会执行

never:即使运行了指定标签,该任务也不会执行

debug:调试任务

setup:收集主机信息

自定义标签

per_tasks:运行指定标签之前的任务

post_tasks:运行指定标签之后的任务

Roles模块

角色:

ansible层次化,结构化的组件playbook,使用了roles(角色)

可以根据层次的结构,自动装载变量文件,task任务,以及handlers等等

rolse:分别把变量 文件 任务 模块以及处理器,放在单独的目录当中,使用rolse模块来一键调用这些文件。

roles:

? --------web---总目录,角色

? files? ?存放copy和script模块调用的文件

? templates? 存放j2的模版文件

? tasks 包含任务的目录(子目录)

? -----------main.yml? 角色运行的任务

? handlers? 包含处理器的目录

? -----------main.yml

? vars? 存放变量的目录

? -----------main.yml

? defaults? 包含默认变量的目录

? -----------maim.yml

? meta? 包含元信息的目录

? -----------main.yml

site.yml用来统筹调用所有的配置文件。

如何来实现?

三个服务(创建三个角色)

httpd

mysql

php

分别在每个角色里面创建目录

touch {}

mysql

pkg:

? ? mariadb

? ? mariadb-server

svc:? mariadb

- name:? install php

? yum:? name={{pkg}}

- name:? start? php=fpm

? service:

pkg:

svc:

vim site.yml

分配要运行的主机、用户

运行

每个角色都要有自己的目录

技术能力,学习能力,解决问题的能力,抗压能力

文章来源:https://blog.csdn.net/m0_70766460/article/details/135122889
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。