个人网站模板代码,微信小程序与公众号的区别,淘宝seo排名优化的方法,路桥做网站的公司有哪些目录
Playbook的组成部分
实例模版
切换用户
指定声明用户
声明和引用变量#xff0c;以及外部传参变量
playbook的条件判断
编辑
习题
编辑
ansible-playbook的循环
item的循环
编辑
list循环
编辑
together的循环#xff08;列表对应的列#xff0…目录
Playbook的组成部分
实例模版
切换用户
指定声明用户
声明和引用变量以及外部传参变量
playbook的条件判断
编辑
习题
编辑
ansible-playbook的循环
item的循环
编辑
list循环
编辑
together的循环列表对应的列数据结合的方式循环
编辑
nested循环
Templates模块
实验httpd
yml文件
实验nginx
tags模块
任务标签的种类
任务标签
自定义标签
实验
Role模块
roles结构
实验 Playbook的组成部分
1、task 任务包含要在目标主机上执行的操作使用模块定义这些操作每个任务都是一个模块的调用
2、variables 变量存储和传递数据变量可以自定义可以在Playbook当中定义为全局变量也可以外部传参
3、Templates 模版用于生成配置文件模版是包含占位符的文件占位符由Ansible在执行时转化为变量值
4、handler 处理器当需要有变更的时候可以执行触发器
5、Roles 角色是一种组织和封装Playbook的允许把相关的任务变量模版和处理器组成一个可复用的单元
实例模版
vim test.yml
#this is our first playbook
- name: first play
#一个name就是一个任务名名字可以不写gather_facts: false
#是否收集目标主机的系统信息false就是不收集hosts: 20.0.0.11
#执行的目标主机remote_user: root
#在目标主机执行的用户tasks:- name: ping testping:- name: close selinuxcommand: /sbin/setenforce 0ignore_errors: True- name: close firewalldservice: namefirewalld statestopped- name: install httpdyum: namehttpd- name: start httpdservice: enabledtrue namehttpd statestarted- name: editon index.htmlshell: echo this is httpd /var/www/html/index.htmlnotify: restart httpdhandlers:- name: restart httpdservice: namehttpd staterestarted#检查yaml文件的语法是否错误
ansible-playbook 文件名 --syntax-check
#查看yml文件里面有几个任务
ansible-playbook 文件名 --list-task
#检查生效的目标主机
ansible-playbook 文件名 --list-hosts
#运行命令
ansible-playbook 文件名ansible-playbook 文件名 --start-at-taskinstall httpd
-k–ask-pass用来交互输入ssh密码
-K-ask-become-pass用来交互输入sudo密码
-u指定用户
切换用户
关闭免密
vim /etc/ansible/ansible.cfg
71行
注释掉编写脚本
hosts: 20.0.0.11
#执行的目标主机
remote_user: xiaobu
become: yes
become_user: root
指定声明用户
在脚本里不声明用户运行时声明用户
ansible-playbook 脚本 -u root -k
声明和引用变量以及外部传参变量
vim test1.yml
#this is second playbook!
#声明和引用变量以及外部传参变量
- hosts: 20.0.0.11remote_user: rootvars:groupname: xiaobu1username: xiaokai
#字典方式: key-value
#vars:
#-
#-
#列表listtasks:- name: create groupgroup:name: {{ groupname }}system: yesgid: 111- name: create useruser:name: {{ username }}uid: 1011group: {{groupname}}shell: /sbin/nologin- name: copy filecopy:content: {{ hostvars[inventory_hostname][ansible_default_ipv4][address] }}
#获取目标主机的IP地址然后复制到目标文件
# 包含所有主机变量的字典
#inventory_hostname 目标主机名
#ansible_default_ipv4 获取目标主机名
#[ansible_default_ipv4][address] 索引dest: /opt/test.txt
外部传参变量
vim test1.yml
#this is second playbook!
#声明和引用变量以及外部传参变量
- hosts: 20.0.0.12remote_user: roottasks:- name: create groupgroup:name: {{ groupname }}system: yesgid: 111- name: create useruser:name: {{ username }}uid: 1011group: {{groupname}}shell: /sbin/nologin- name: copy filecopy:content: {{ hostvars[inventory_hostname][ansible_default_ipv4][address] }}dest: /opt/test.txt外部传参
ansible-playbook test1.yml -e usernamexiaobu2 groupnamexiaokai1playbook的条件判断
when 是一个比较常见的应用长江实现满足条件即执行不满足条件即跳过的任务when是满足条件即执行不满足不执行。
vim test2.yml
#this is when test
- hosts: all
#可以用主机的IP地址也可以是用组名也可以用allremote_user: roottasks:- name: test whendebug:msg: 位置判断
#打印相当于echo msg: 输出的内容debug: 用于脚本的调试在正式脚本中可以去除when: ansible_default_ipv4.address 20.0.0.11
#when: ansible_default_ipv4.address 20.0.0.11或者 when: inventory_hostname ! 20.0.0.11
#ansible_default_ipv4.address ! 20.0.0.11 取反 习题
现在hosts all
条件1 IP 11安装nginx
条件2 IP 12安装httpd
#this is when test
- hosts: allremote_user: roottasks:- name: nginxyum: namenginxwhen: ansible_default_ipv4.address 20.0.0.11- name: nginx infodebug:msg: 安装nginxwhen: ansible_default_ipv4.address 20.0.0.11- name: httpdyum: namehttpdwhen: ansible_default_ipv4.address 20.0.0.12- name: httpd infodebug:msg: 安装httpdwhen: ansible_default_ipv4.address 20.0.0.12 ansible-playbook的循环
ansible有多种循环格式with_items 循环遍历
item的循环
声明变量itemplaybook的内置变量with_items会把item的值遍历列表当中的a,b,c,d
vim test4.yml
- hosts: 20.0.0.12remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_items: - [a,b,c,d]- [1,2,3,4]
#声明变量itemplaybook的内置变量with_items会把item的值遍历列表当中的a,b,c,d
#虽然我声明的列表是两个但是with_items还是把两个列表当成整体进行遍历 list循环
列别分组循环
#分组打印with_list: - [a,b,c,d]- [1,2,3,4]- hosts: 20.0.0.12remote_user: rootgather_facts: falsetasks:- name: create filefile:path: {{ item }}state: touchwith_items:- /opt/a- /opt/b- /opt/c- /opt/d- /opt/1- /opt/2- /opt/3- /opt/4 together的循环列表对应的列数据结合的方式循环
组循环列表当中的值一一对应打印出来
- hosts: 20.0.0.12remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_together:- [a,b,c,d]- [1,2,3,4]- [A,B,C]nested循环
相当于双循环第一层定义循环的次数第二层表示第一层的每一个元素会循环几次
列表里面的元素定义了循环的次数第二层列表相当于内循环
- hosts: 20.0.0.12remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_nested:- [a,b,c,d]- [1,2,3,4] Templates模块
Jinja模版架构通过模版可以实现向模版文件传参Python转义把占位符参数传到配置文件中去
生成一个目标文本文件传递变量到需要配置文件当中
实验httpd
yum -y install httpd
cd /etc/httpd/conf
cp httpd.conf /opt/httpd.conf.j2
httpd.conf.j2 在文件当中配置的是占位符声明的变量
/etc/ansible/hosts 配置了主机的占位符名称和j2文件中的占位符一致定义参数占位符的参数的参数声明好
playbook当中Template模块来吧参数传给目标的主机的配置文件
vim /opt/httpd.conf.j2
42行
Listen {{http_port}}
95行
ServerName {{server_name}}
119行
DocumentRoot {{root_dir}}修改ansible配置文件
20.0.0.11 http_port20.0.0.11:80 server_namewww.xiaobu.com:80 root_dir/etc/httpd/htdocs20.0.0.12 http_port20.0.0.12:80 server_namewww.xiaobu.com:80 root_dir/etc/httpd/htdocs yml文件
- hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpdyum: name{{package}}- name: install config filetemplate: src/opt/httpd.conf.j2 dest/etc/httpd/conf/httpd.confnotify:- restart httpd- name: create root_dirfile:path: /etc/httpd/htdocsstate: directory- name: start httpdservice: name{{service}} enabledtrue statestartedhandlers:- name: restart httpdservice: name{{service}} staterestarted 实验nginx
cp /etc/nginx/nginx.conf /opt/nginx.conf.j2修改nginx.的配置文件server {listen {{nginx_port}};server_name {{servername}};root {{root_dir}};
}修改ansible的配置文件
20.0.0.11 nginx_port8080 servernamewww.xiaobu.com root_dir/etc/nginx/html20.0.0.12 nginx_port8080 servernamewww.xiaobu.com root_dir/etc/nginx/htmlyml文件- hosts: allremote_user: rootvars:- package: nginx- service: nginxtasks:- name: install nginxyum: name{{package}}- name: install config filetemplate: src/opt/nginx.conf.j2 dest/etc/nginx/nginx.confnotify:- restart nginx- name: create root_dirfile:path: /etc/nginx/htmlstate: directory- name: start nginxservice: name{{service}} enabledtrue statestartedhandlers:- name: restart nginxservice: name{{service}} staterestarted tags模块
标签模块可以在playbook当中为任务设定标签tags我们在运行playbook是可以通过指定任务标签来实现只运行设定的标任务
任务标签的种类
always 不管你是否指定了运行标签任务都会执行
never 即使运行了指定标签该任务也不会执行
debug 调试任务
任务标签
- hosts: allremote_user: rootgather_facts: falsetasks:- name: tag debugdebug:msg: this is test1tags:- debug- name: tag setupsetup:tags:- setup- name: tag alwaysdebug:msg: runtags:- always- name: tag neverdebug:msg: never runtags:- never 自定义标签
per_tasks 指定标签之前的任务
post_tasks 运行指定标签之后的任务
- hosts: allremote_user: rootgather_facts: falsetasks:- name: tag alwaysdebug:msg: runtags:- xiaobu- name: tag neverdebug:msg: never runtags:- xiaokai
实验
1、在目标主机上touch xiaobu.txt always
2、在目标主机复制文件opt/xiaobu2.txt 标签never
实验目的
第一运行playbook 不指定标签查看文件生成情况
指定标签为never查看文件生成情况
- hosts: allremote_user: rootgather_facts: falsetasks:- name: touch filefile:path: /opt/xiaobu.txtstate: touchtags:- always- name: copy filecopy:src: /opt/123dest: /opt/123tags:- never
Role模块
Role模块又叫角色
ansible层次化结构化的组织playbook使用了rolse角色
可以根据层次结构自动装载变量文件task以及handler等等 Roles分别把变量 文件 任务 模块以及处理器放在单独的目录当中使用roles模块来一键调用这些文件 roles结构
--- web---总目录角色
files 存放copy和script模块调用的文件
Templates 存放j2的模块文件
tasks 包含任务的目录 ---- mail.yml 角色运行的任务
handlers 包含处理器的目录 ---- main.yml
vars 存放变量的目录 ----main.yml
defaults 包含默认变量的目录 -----main.yml
meta 包含元信息的目录----main.yml
在总目录下site.yml用来调用所有配置文件
实验
三个服务
http mysql php
#安排剧本
cd /etc/ansible/roles/
mkdir httpd mysql php
cd httpd
mkdir files templates tasks handlers vars defaults meta
touch {defaults,vars,tasks,meta,handlers}/main.yml
cd mysql
mkdir files templates tasks handlers vars defaults meta
touch {defaults,vars,tasks,meta,handlers}/main.yml
cd php
mkdir files templates tasks handlers vars defaults meta
touch {defaults,vars,tasks,meta,handlers}/main.yml配置httpd
vim tasks/main.yml
- name: install httpdyum: name{{pkg}}
- name: start httpdservice: enabledtrue name{{svc}} statestartedvim httpd/vars/main.yml
pkg: httpd
svc: httpd配置mysql
vim mysql/tasks/main.yml
- name: isntall mysqlyum: name{{pkg}}
- name: start mysqlservice: enabledtrue name{{svc}} statestartedvim mysql/vars/main.yml
pkg:- mariadb- mariadb-server
svc: mariadb配置php
vim php/tasks/main.yml
- name: install phpyum: name{{pkg}}
- name: start phpservice: enabledtrue name{{svc}} statestartedvim php/vars/main.yml
pkg: - php- php-fpm
svc: php-fpm