jinja模版架构,通过模版可以实现向模版文件传参(python转义),把占位符参数传到配置文件中去。
生产一个目标文件,传递变量到需要的配置文件中。(web开发)
vim /etc/ansible/hosts
192.168.10.202 nginx_port=8080 nginx_server=www.guoqi.com dir=/opt/nginx/html
vim /opt/nginx2.conf.j2
修改nginx的端口、server_name、访问页面的地址
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen [::]:80;
listen {{nginx_port}};
server_name {{nginx_server}};
root {{dir}};
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
vim /opt/date_nginx.yaml
- hosts: 192.168.10.202
remote_user: root
vars:
- package: nginx
- service: nginx
tasks:
- name: install nginx
yum: name={{package}}
- name: install configure
template: src=/opt/nginx2.conf.j2 dest=/etc/nginx/nginx.conf
notify:
- restart nginx
- name: create dir
file:
path: /opt/nginx/html
state: directory
- name: start nginx
service: name={{service}} enabled=true state=started
handlers:
- name: restart nginx
service: name={{service}} state=restarted
标签模块,可以在playbook当中为任务设定标签(tags),我们在运行playbook时可以通过指定任务标签,来实现只运行设定的标签任务。
?
任务标签的种类:
always: 不管你是否指定了运行标签,任务都会执行
never: 只有运行了指定标签,该任务才会执行
debug: 调试任务
setup: 收集主机信息
自定义标签
per_tasks: 指定标签之前的任务
post_tasks: 运行指定标签之后的任务
?????- hosts: all
remote_user: root
tasks:
- name: tag debug
debug:
msg: "test1"
tags:
- debug
- name: tag setup
setup:
tags:
- setup
- name: tag always
debug:
msg: "run"
tags:
- always
- name: tag never
debug:
msg: "never run"
tags:
- never
指定debug,会执行always和debug
指定收集主机信息,会执行always和setup
在目标touch /opt/guoqi.txt always
在目标主机复制分件/opt/wdf.txt#标签never
第一次运行playbook 不指定标签查看文件生成情况#指定为自定义,查看文件生成情况。
- hosts: all
remote_user: root
tasks:
- name: tag file
file:
path: /opt/guoqi.txt
state: touch
tags:
- always
- name: tag copy
copy:
src: /opt/wdf.txt
dest: /opt/wdf.txt
tags:
- never
ansible-playbook date_tags2.yaml --tags="alway"
ansible-playbook date_tags2.yaml --tags="never"
ansible-playbook date_tags2.yaml
角色
ansible是一个层次化的组织playbook,使用了role(角色)
可以根据层次结构,自动装载变量文件,task以及handlers等等
roles:分别把变量 文件 任务 模块以及处理器,放在单独的目录中,使用rolse模块来一键调用这些文件
roles:
-------web 总目录 角色
file: 存放copy和script模块调用的文件
templates 存放j2的模版文件
tasks: 包含任务的目录
-------main.yml 角色运行的任务
handlers 包含处理器的目录
-------main.yml
vars 存放变量的目录
-------main.yml
defaults 包含默认变量的目录
-------main.yml
meta 包含元信息的目录
-------main.yml
site.yml 用来调用所有的配置文件
httpd
cd /etc/ansible/roles/
mkdir httpd
vim httpd/tasks/main.yml
#配置httpd
- name: install httpd
yum: name={{pkg}}
- name: start httpd
service: enabled=true name={{svc}} state=started
vim httpd/vars/main.yml
#配置http服务的名称进行外部传参
pkg: httpd
svc: httpd
mysql
cd /etc/ansible/roles/
mkdir mysql
vim mysql/tasks/main.yml
- name: install mysql
yum: name={{pkg}}
- name: start mysql
service: enabled=true name={{svc}} state=started
vim mysql/vars/main.yml
pkg:
- mariadb
- mariadb-server
svc: mariadb
php
cd /etc/ansible/roles/
mkdir php
vim php/tasks/main.yml
- name: install php
yum: name={{pkg}}
- name: start php-fpm
service: enabled=true name={{svc}} state=started
vim php/vars/main.yml
pkg:
- php
- php-fpm
svc: php-fpm
vim /etc/ansible/roles/sit.yml
#定义总控制
- hosts: 192.168.10.201
remote_user: root
roles:
- httpd
- mysql
- php
运行
ansible-playbook sit.yml