当前位置: 首页 > news >正文

Ansible 的脚本 --- playbook 剧本

目录

playbook 剧本

playbooks 本身由以下各部分组成

定义、引用变量

指定远程主机sudo切换用户

when条件判断

迭代

Templates 模块

1.先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量

2.修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量

3.编写 playbook 

tags 模块

用剧本安装lnmp

Roles 模块

roles 的目录结构:

roles 内各目录含义解释

在一个 playbook 中使用 roles 的步骤:

编写httpd模块

编写mysql模块

编写php模块

编写roles示例


playbook 剧本

playbooks 本身由以下各部分组成

(1)Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
(2)Variables:变量
(3)Templates:模板
(4)Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
(5)Roles:角色

示例

vim test1.yaml
---     #yaml文件以---开头,以表明这是一个yaml文件,可省略
- name: first play       #定义一个play的名称,可省略gather_facts: false    #设置不进行facts信息收集,这可以加快执行速度,可省略hosts: webservers      #指定要执行任务的被管理主机组,如多个主机组用冒号分隔remote_user: root      #指定被管理主机上执行任务的用户tasks:                 #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行- name: test connection           #自定义任务名称ping:                           #使用 module: [options] 格式来定义一个任务- name: disable selinuxcommand: '/sbin/setenforce 0'   #command模块和shell模块无需使用key=value格式ignore_errors: True             #如执行命令的返回值不为0,就会报错,tasks停止,可使用ignore_errors忽略失败的任务- name: disable firewalldservice: name=firewalld state=stopped    #使用 module: options 格式来定义任务,option使用key=value格式- name: install httpdyum: name=httpd state=latest- name: install configuration file for httpdcopy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf    #这里需要一个事先准备好的/opt/httpd.conf文件notify: "restart httpd"    #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作- name: start httpd serviceservice: enabled=true name=httpd state=startedhandlers:               #handlers中定义的就是任务,此处handlers中的任务使用的是service模块- name: restart httpd    #notify和handlers中任务的名称必须一致service: name=httpd state=restarted
##Ansible在执行完某个任务之后并不会立即去执行对应的handler,而是在当前play中所有普通任务都执行完后再去执行handler,这样的好处是可以多次触发notify,但最后只执行一次对应的handler,从而避免多次重启。
- name: the first play for install apachegather_facts: falsehosts: dbserversremote_user: roottasks:- name: disable firewalldservice: name=firewalld state=stopped enabled=no- name: disable selinuxcommand: '/usr/sbin/setenforce 0'ignore_errors: true- name: disable selinux foreverreplace: path=/etc/selinux/config  regexp="enforcing"  replace="disabled"- name: mount cdrommount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted- name: copy local yum configuration filecopy: src=/etc/yum.repos.d/repo.bak/local.repo  dest=/etc/yum.repos.d/local.repo- name: install apacheyum: name=httpd state=latest- name: prepare httpd configuration filecopy: src=/etc/ansible/playbook/httpd.conf dest=/etc/httpd/conf/httpd.confnotify: "reload httpd"- name: start apacheservice: name=httpd state=started enabled=yeshandlers: - name: reload httpdservice: name=httpd state=reloaded

运行playbook

ansible-playbook demo1.yaml

 在另一台机器上就会发现apache已经安装,并暴露出端口号

netstat -lntp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      11454/httpd

补充参数
-k(–ask-pass):用来交互输入ssh密码
-K(-ask-become-pass):用来交互输入sudo密码
-u:指定用户
ansible-playbook test1.yaml --syntax-check                         #检查yaml文件的语法是否正确
ansible-playbook test1.yaml --list-task                                  #检查tasks任务
ansible-playbook test1.yaml --list-hosts                                #检查生效的主机
ansible-playbook test1.yaml --start-at-task='install httpd'     #指定从某个task开始运行

定义、引用变量

- name: second playhosts: dbserversremote_user: rootvars:                 #定义变量- groupname: mysql   #格式为 key: value- username: nginxtasks:- name: create groupgroup: name={{groupname}} system=yes gid=306    #使用 {{key}} 引用变量的值- name: create useruser: name={{username}} uid=306 group={{groupname}} - name: copy filecopy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt    #在setup模块中可以获取facts变量信息

示例

- name: second playhosts: dbserversremote_user: rootbecome: yesbecome_user: zxrvars:- username: ggl- groupname: zxr- filename: /opt/ggl.txt- uid: 1234gather_facts: truetasks:- name: create groupgroup: name={{groupname}}  gid=1314- name: create user join groupuser: name={{username}} uid={{uid}} groups={{groupname}}- name: copy filecopy: content="{{ansible_default_ipv4.address}}" dest={{filename}}- name: modify username and groupname of filefile: path={{filename}} owner={{username}}  group={{groupname}}

 在另一个主机上输入id ggl

id ggl
uid=1234(ggl) gid=1234(ggl) 组=1234(ggl),1314(zxr)

在外面传参

ansible-playbook test1.yaml -e "username=nginx"     #在命令行里定义变量

在另一个主机上输入id nginx

id nginx
uid=1235(nginx) gid=1235(nginx) 组=1235(nginx),1314(zxr)

指定远程主机sudo切换用户

---
- hosts: dbserversremote_user: zhangsan            become: yes                     #2.6版本以后的参数,之前是sudo,意思为切换用户运行become_user: root               #指定sudo用户为root
执行playbook时:ansible-playbook test1.yml -k -K 

示例

- name: second playhosts: dbserversremote_user: zxrbecome: yesbecome_user: rootvars:- username: ycx- groupname: zxr- filename: /opt/ggl.txt- uid: 1236gather_facts: truetasks:- name: create groupgroup: name={{groupname}}  gid=1314- name: create user join groupuser: name={{username}} uid={{uid}} groups={{groupname}}- name: copy filecopy: content="{{ansible_default_ipv4.address}}" dest={{filename}}- name: modify username and groupname of filefile: path={{filename}} owner={{username}}  group={{groupname}}
ansible-playbook demo2.yaml -k -K
SSH password:
BECOME password[defaults to SSH password]:PLAY [second play] *************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.70]TASK [create group] ************************************************************
ok: [192.168.110.70]TASK [create user join group] **************************************************
changed: [192.168.110.70]TASK [copy file] ***************************************************************
ok: [192.168.110.70]TASK [modify username and groupname of file] ***********************************
changed: [192.168.110.70]PLAY RECAP *********************************************************************
192.168.110.70             : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

注:在另一台主机上要确保用户加入到/etc/sudoers文件中,确保zxr要有root权限

when条件判断

在Ansible中,提供的唯一一个通用的条件判断是when指令,当when指令的值为true时,则该任务执行,否则不执行该任务。

//when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务
 

vim demo2.yaml
---
- hosts: allremote_user: roottasks:- name: shutdown host command: /sbin/shutdown -r nowwhen: ansible_default_ipv4.address == "192.168.110.70"      #when指令中的变量名不需要手动加上 {{}}
或 when: inventory_hostname == "<主机名>"

示例

- name: third playhosts: allremote_user: roottasks:- name: touch filefile: path=/opt/zzz.txt state=touch#when: ansible_default_ipv4.address != "192.168.110.70"when: inventory_hostname == "192.168.110.70"
ansible-playbook demo3.yamlPLAY [third play] **************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.90]
ok: [192.168.110.70]TASK [touch file] **************************************************************
skipping: [192.168.110.90]
changed: [192.168.110.70]PLAY RECAP *********************************************************************
192.168.110.70             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.110.90             : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

迭代

Ansible提供了很多种循环结构,一般都命名为with_items,作用等同于 loop 循环。

vim test3.yaml
---
- name: play1hosts: dbserversgather_facts: falsetasks: - name: create filefile:path: "{{item}}"state: touchwith_items: [ /opt/a, /opt/b, /opt/c, /opt/d ]- name: play2hosts: dbserversgather_facts: false        vars:test:- /tmp/test1- /tmp/test2- /tmp/test3- /tmp/test4tasks: - name: create directoriesfile:path: "{{item}}"state: directorywith_items: "{{test}}"- name: play3hosts: dbserversgather_facts: falsetasks:- name: add usersuser: name={{item.name}} state=present groups={{item.groups}}with_items:- name: test1groups: wheel- name: test2groups: root
或with_items:- {name: 'test1', groups: 'wheel'}- {name: 'test2', groups: 'root'}

示例

- name: fouth playhosts: dbserversremote_user: rootvars:myfile:- /opt/a- /opt/b- /opt/c- /opt/dtasks:- name: touch directorywith_items: "{{myfile}}"file: path={{item}} state=directory- name: touch filewith_items:- /root/a- /root/b- /root/c- /root/dfile:path: "{{item}}"state: touch
ansible-playbook demo4.yamlPLAY [fouth play] **************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.70]TASK [touch directory] *********************************************************
changed: [192.168.110.70] => (item=/opt/a)
changed: [192.168.110.70] => (item=/opt/b)
changed: [192.168.110.70] => (item=/opt/c)
changed: [192.168.110.70] => (item=/opt/d)TASK [touch file] **************************************************************
changed: [192.168.110.70] => (item=/root/a)
changed: [192.168.110.70] => (item=/root/b)
changed: [192.168.110.70] => (item=/root/c)
changed: [192.168.110.70] => (item=/root/d)PLAY RECAP *********************************************************************
192.168.110.70             : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Templates 模块

Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。

1.先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量

cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2
vim /opt/httpd.conf.j2
Listen {{http_port}}                  #42行,修改
ServerName {{server_name}}            #95行,修改
DocumentRoot "{{root_dir}}"           #119行,修改

2.修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量

vim /etc/ansible/hosts       
[webservers]
192.168.110.90 http_port=192.168.80.11:80 server_name=www.zxr.com:80 root_dir=/var/www/html/zxr[dbservers]
192.168.110.70 http_port=192.168.80.12:80 server_name=www.yyds.com:80 root_dir=/var/www/html/yyds

3.编写 playbook 

- name: sixth playhosts: webserversremote_user: rootvars:- pkg: httpdtasks:- name: install apacheyum: name=httpd state=latest- name: create root dirfile: state=directory path={{item}}with_items:- /var/www/html/zxr- /var/www/html/yyds- name: create index.html in www.zxr.comcopy: content="<h1>this is zxr web</h1>" dest=/var/www/html/zxr/index.htmlwhen: ansible_default_ipv4.address == "192.168.110.90"- name: create index.html in www.yyds.comcopy: content="<h1>this is yyds web</h1>" dest=/var/www/html/yyds/index.htmlwhen: inventory_hostname == "192.168.110.70"- name: prepare configuration filetemplate: src=/opt/playbook/httpd.conf.j2 dest=/etc/httpd/conf/httpd.confnotify: "reload apache"- name: start apacheservice: name={{pkg}} state=started enabled=yeshandlers:- name: reload apacheservice: name={{pkg}} state=reloaded
ansible-playbook demo6.yamlPLAY [sixth play] **************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.90]TASK [install apache] **********************************************************
ok: [192.168.110.90]TASK [create root dir] *********************************************************
ok: [192.168.110.90] => (item=/var/www/html/zxr)
ok: [192.168.110.90] => (item=/var/www/html/yyds)TASK [create index.html in www.zxr.com] ****************************************
ok: [192.168.110.90]TASK [create index.html in www.yyds.com] ***************************************
skipping: [192.168.110.90]TASK [prepare configuration file] **********************************************
changed: [192.168.110.90]TASK [start apache] ************************************************************
ok: [192.168.110.90]RUNNING HANDLER [reload apache] ************************************************
changed: [192.168.110.90]PLAY RECAP *********************************************************************
192.168.110.90             : ok=7    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

tags 模块

可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。

- name: seventh playhosts: dbserversremote_user: roottasks:- name: create abc.txtfile: path=/opt/abc.txt  state=touchtags:- zxr- name: create bac.txtfile: path=/opt/123.txt  state=touchtags:- ycx- name: create cba.txtcopy: content="ggl like dancing"  dest=/opt/ggl.txttags:- ggl
ansible-playbook dbhosts.yaml --tags="zxr"

分别去两台被管理主机上去查看文件创建情况

ansible-playbook demo7.yaml --tags="zxr"PLAY [seventh play] ************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.70]TASK [create abc.txt] **********************************************************
changed: [192.168.110.70]PLAY RECAP *********************************************************************
192.168.110.70             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

示例

用剧本安装lnmp

- name: lnmp playhosts: dbserversremote_user: roottasks:- name: perpare condifurecopy: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo- name: install nginxyum: name=nginx state=latest- name: start nginxservice: name=nginx state=started enabled=yes- name: install mysqlyum: name=mysql57-community-release-el7-10.noarch.rpm state=latest- name: modify filereplace:path: /etc/yum.repos.d/mysql-community.reporegexp: 'gpgcheck=1'replace: 'gpgcheck=0'- name: install mysql-community-serveryum: name=mysql-community-server state=latest- name: start mysqlservice: name=mysqld state=started enabled=yes- name: add yum filecommand: 'wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm -P /etc/yum.repos.d' - name: rpm epelcommand: 'rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm'- name: rpm el7command: 'rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm'- name: install phpcommand: 'yum install -y php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache'- name: start php-fpmservice: name=php-fpm state=started  enabled=yes- name: copy configurecopy: src=/usr/local/nginx/conf/nginx.conf dest=/etc/nginx/conf.d/default.conf- name: restart nginxservice: name=nginx state=started enabled=yes

Roles 模块

roles用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令引入即可。
简单来讲,roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中,并可以便捷的include它们的一种机制。roles一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。主要使用场景代码复用度较高的情况下。

假如我们现在有3个被管理主机,第一个要配置成httpd,第二个要配置成haproxy服务器,第三个要配置成MySQL(mariadb)服务器。我们如何来定义playbook?
第一个play用到第一个主机上,用来构建httpd,第二个play用到第二个主机上,用来构建haproxy。这些个play定义在playbook中比较麻烦,将来也不利于模块化调用,不利于多次调用。比如说后来又加进来一个主机,这第3个主机既是httpd服务器,又是haproxy服务器,我们只能写第3个play,上面写上安装httpd和haproxy。这样playbook中的代码就重复了。
为了避免代码重复,可以定义一个角色叫httpd,第二个角色叫haproxy,并使用roles实现代码重复被调用。

roles 的目录结构:

cd /etc/ansible/
tree roles/
roles/
├── web/    #相当于 playbook 中的 每一个 play 主题
│   ├── files/
│   ├── templates/
│   ├── tasks/
│   ├── handlers/
│   ├── vars/
│   ├── defaults/
│   └── meta/
└── db/├── files/├── templates/├── tasks/├── handlers/├── vars/├── defaults/└── meta/

roles 内各目录含义解释

●files
用来存放由 copy 模块或 script 模块调用的文件。

●templates
用来存放 jinjia2 模板,template 模块会自动在此目录中寻找 jinjia2 模板文件。

●tasks
此目录应当包含一个 main.yml 文件,用于定义此角色的任务列表,此文件可以使用 include 包含其它的位于此目录的 task 文件。

●handlers
此目录应当包含一个 main.yml 文件,用于定义此角色中触发条件时执行的动作。

●vars
此目录应当包含一个 main.yml 文件,用于定义此角色用到的变量。

●defaults
此目录应当包含一个 main.yml 文件,用于为当前角色设定默认变量。 这些变量具有所有可用变量中最低的优先级,并且可以很容易地被任何其他变量覆盖。所以生产中我们一般不在这里定义变量

●meta
此目录应当包含一个 main.yml 文件,用于定义此角色的元数据信息及其依赖关系。


在一个 playbook 中使用 roles 的步骤:

(1)创建以 roles 命名的目录
mkdir /etc/ansible/roles/ -p    #yum装完默认就有

(2)创建全局变量目录(可选)
mkdir /etc/ansible/group_vars/ -p
touch /etc/ansible/group_vars/all     #文件名自己定义,引用的时候注意

(3)在 roles 目录中分别创建以各角色名称命名的目录,如 httpd、mysql
mkdir /etc/ansible/roles/httpd
mkdir /etc/ansible/roles/mysql

(4)在每个角色命名的目录中分别创建files、handlers、tasks、templates、meta、defaults和vars目录,用不到的目录可以创建为空目录,也可以不创建
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta}
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta}

(5)在每个角色的 handlers、tasks、meta、defaults、vars 目录下创建 main.yml 文件,千万不能自定义文件名
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml

(6)修改 site.yml 文件,针对不同主机去调用不同的角色

vim /etc/ansible/site.yml
---
- hosts: webserversremote_user: rootroles:- httpd
- hosts: dbserversremote_user: rootroles:- mysql

(7)运行 ansible-playbook

cd /etc/ansible
ansible-playbook site.yml

示例

mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -ptouch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml

编写httpd模块

写一个简单的tasks/main.yml

vim /etc/ansible/roles/httpd/tasks/main.yml
- name: install apacheyum: name={{pkg}} state=latest
- name: start apacheservice: enabled=true name={{svc}} state=started

定义变量:可以定义在全局变量中,也可以定义在roles角色变量中,一般定义在角色变量中

vim /etc/ansible/roles/httpd/vars/main.yml
pkg: httpd
svc: httpd

编写mysql模块

vim /etc/ansible/roles/mysql/tasks/main.yml
- name: install mysqlyum: name={{pkg}} state=latest
- name: start mysqlservice: enabled=true name={{svc}} state=startedvim /etc/ansible/roles/mysql/vars/main.yml
pkg:- mariadb- mariadb-server
svc: mariadb

编写php模块

vim /etc/ansible/roles/php/tasks/main.yml
- name: install phpyum: name={{pkg}} state=latest
- name: start php-fpmservice: enabled=true name={{svc}} state=started
vim /etc/ansible/roles/php/vars/main.yml
pkg:- php- php-fpm
svc: php-fpm

编写roles示例

vim /etc/ansible/site.yml
---
- hosts: webserversremote_user: rootroles:- httpd- mysql- php
cd /etc/ansible
ansible-playbook site.yml


 

相关文章:

Ansible 的脚本 --- playbook 剧本

目录 playbook 剧本 playbooks 本身由以下各部分组成 定义、引用变量 指定远程主机sudo切换用户 when条件判断 迭代 Templates 模块 1.先准备一个以 .j2 为后缀的 template 模板文件&#xff0c;设置引用的变量 2.修改主机清单文件&#xff0c;使用主机变量定义一个变…...

CSP-J模拟赛 / 买文具

限制条件 时间限制: 1000 ms, 空间限制: 256 MB 输入文件: pen.in, 输出文件&#xff1a;pen.out 题目描述 开学啦&#xff0c;为了准备新学期的课程学习&#xff0c;小贝到商店买文具。小贝买完文具回家&#xff0c;告诉妈妈说她买了钢笔、圆珠笔和铅笔总共x支&#xff0c…...

leecode算法--每日一题1

二分查找 给定一个 n 个元素有序的&#xff08;升序&#xff09;整型数组 nums 和一个目标值 target &#xff0c;写一个函数搜索 nums 中的 target&#xff0c;如果目标值存在返回下标&#xff0c;否则返回 -1。 前提条件必须满足&#xff1a; 目标数组必须是有序数组 所以…...

LViT:语言与视觉Transformer在医学图像分割

论文链接&#xff1a;https://arxiv.org/abs/2206.14718 代码链接&#xff1a;GitHub - HUANGLIZI/LViT: This repo is the official implementation of "LViT: Language meets Vision Transformer in Medical Image Segmentation" (IEEE Transactions on Medical I…...

蓝桥杯上岸每日N题 第五期(山)!!!

蓝桥杯上岸每日N题第五期 ❗️ ❗️ ❗️ 同步收录 &#x1f447; 蓝桥杯Java 省赛B组(初赛)填空题 大家好 我是寸铁&#x1f4aa; 冲刺蓝桥杯省一模板大全来啦 &#x1f525; 蓝桥杯4月8号就要开始了 &#x1f64f; 距离蓝桥杯省赛倒数第3天 ❗️ ❗️ ❗️ 还没背熟模…...

IDEA Writing classes... 比较慢

IDEA配置修改如下&#xff1a; 1、File -> Settings… 2、Build&#xff0c;Execution&#xff0c;Deployment -> Compiler Build process heap size 配置为 20483、Build&#xff0c;Execution&#xff0c;Deployment -> Compiler -> ActionScript & Flex C…...

opencv中轮廓相关属性

一、介绍 findContours() &#xff1a;The function retrieves contours from the binary image。 二、代码 void main() {Mat src imread("match00.bmp", IMREAD_GRAYSCALE);Mat mask;threshold(src, mask, 128, 255, cv::THRESH_BINARY_INV);Mat element cv::g…...

Leetcode 144. 二叉树的前序遍历

题目描述 题目链接&#xff1a;https://leetcode.cn/problems/binary-tree-preorder-traversal/description/ 代码实现 class Solution {List<Integer> l new ArrayList<>();public List<Integer> preorderTraversal(TreeNode root) {preoder(root);re…...

医学影像PACS系统源码:多功能服务器和阅片系统

PACS系统是以最新的IT技术为基础&#xff0c;遵循医疗卫生行业IHE/DICOM3.0和HL7标准&#xff0c;开发的多功能服务器和阅片系统。通过简单高性能的阅片功能&#xff0c;支持繁忙时的影像诊断业务&#xff0c;拥有保存影像的院内Web传输及离线影像等功能&#xff0c;同时具有备…...

php 生成连续递增的Excel列索引 可以控制多少列

今天遇到需要生成对应的下拉&#xff0c;下拉的类 需要PHP 输出一个数组 如 A、B、C、D 到Z 列后 Excel 的列就变成 AA 、AB、 AC 依次类推 查询得知 Excel 最大列数 16384 最大行数 1048576 下面演示3000列或行 <?php$idx [idx > 0];for ($i …...

Openstack等私有云

1 OpenStack 计算&#xff1a;部署管理虚拟机存储&#xff1a;块存储 Cinder 和 对象存储 Swift网路&#xff1a;管理网络身份&#xff1a;管理用户和权限镜像&#xff1a;管理镜像用于快速部署新的虚拟机仪表盘&#xff1a;Web界面 2 RAID 如果使用的软件已经在多个硬件设备…...

MySQL 8.0详细安装配置教程

一. 前言 MySQL是目前最为流行的开源数据库产品&#xff0c;是完全网络化跨平台的关系型数据库系统。它起初是由瑞典MySQLAB公司开发&#xff0c;后来被Oracle公司收购&#xff0c;目前属于Oracle公司。因为开源&#xff0c;所以任何人都能从官网免费下载MySQL软件&#xff0c…...

pytest 入门

1,安装pytest 打开终端或命令提示符窗口,在终端中运行以下命令来安装pytest: pip install pytestpip install -i https://pypi.tuna.tsinghua.edu.cn/simple pytest 确保您的系统上已经安装了Python。您可以在终端中运行以下命令来检查Python的安装情况: pytest --version…...

分布式缓存数据一致性-解决方案

如果是用户维度&#xff0c;并发几率小&#xff08;用户修改订单&#xff09;。不需要考虑一致性问题&#xff0c;缓存数据加上过期时间&#xff0c;每隔一段时间出发读数据&#xff0c;主动更新缓存即可。&#xff08;缓存过期删除数据&#xff0c;触发读请求主动更新&#xf…...

Java设计模式-享元模式

享元模式 1.享元模式含义 享元模式&#xff0c;运用共享技术有效地支持大量细粒度的对象。 其实享元模式很好理解&#xff0c;就是共享元数据的意思。比如一个小狗类对象&#xff0c;里面的属性有头&#xff0c;耳朵&#xff0c;眼睛&#xff0c;毛色这几个属性&#xff0c;…...

idea模块的pom.xml被划横线,不识别的解决办法

目录 问题&#xff1a; 解决办法&#xff1a; 1.打开设置 2. 取消勾选 3.点击确认 4.解决 问题提出&#xff1a; 写shi山的过程中&#xff0c;给模块取错名字了&#xff0c;改名的时候不知道点到了什么&#xff0c;一个模块的pom.xml变成灰色了&#xff0…...

ffmpeg 中 av_log 是怎样工作的?

---------------------------------------- author: hjjdebug date: 2023年 07月 27日 星期四 14:56:38 CST descriptor: ffmpeg 中 av_log 是怎样工作的? ---------------------------------------- av_log 功能其实只是添加了颜色,LOG级别,及log上下文名称,没有添加时间,函…...

HTML+CSS+JavaScript:轮播图自动播放

一、需求 轮播图如下图所示&#xff0c;需求是每隔一秒轮播图自动切换一次 二、代码素材 以下是缺失JS部分的代码&#xff0c;感兴趣的小伙伴可以先自己试着写一写 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /&…...

python 自动化数据提取之正则表达式

>>>> 前 言 我们在做接口自动化的时候&#xff0c;处理接口依赖的相关数据时&#xff0c;通常会使用正则表达式来进行提取相关的数据&#xff0c;今天在这边和大家聊聊如何在python中使用正则表达式。 正则表达式&#xff0c;又称正规表示式、正规表示法、正规…...

分布式事务之本地事务

&#x1f680; 分布式事务 &#x1f680; &#x1f332; AI工具、AI绘图、AI专栏 &#x1f340; &#x1f332; 如果你想学到最前沿、最火爆的技术&#xff0c;赶快加入吧✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;CSDN-Java领域优质创作者&#x1f3c6;&…...

PyTorch 初级教程:构建你的第一个神经网络

PyTorch 是一个在研究领域广泛使用的深度学习框架&#xff0c;提供了大量的灵活性和效率。本文将向你介绍如何使用 PyTorch 构建你的第一个神经网络。 一、安装 PyTorch 首先&#xff0c;我们需要安装 PyTorch。PyTorch 的安装过程很简单&#xff0c;你可以根据你的环境&…...

SpringBoot使用MyBatis Plus + 自动更新数据表

1、Mybatis Plus介绍 Mybatis&#xff0c;用过的都知道&#xff0c;这里不介绍&#xff0c;mybatis plus只是在mybatis原来的基础上做了些改进&#xff0c;增强了些功能&#xff0c;增强的功能主要为增加更多常用接口方法调用&#xff0c;减少xml内sql语句编写&#xff0c;也可…...

【设计模式】简单工厂模式

C语言实现简单的工厂模式 #include <stdio.h> #include <stdlib.h>// 图形类型枚举 typedef enum {CIRCLE,SQUARE,RECTANGLE } ShapeType;// 图形结构体 typedef struct {ShapeType type;float area; } Shape;// 创建圆形 Shape* createCircle() {Shape* circle …...

推荐系统-ALS协同过滤算法实现

从协同过滤的分类来说&#xff0c;ALS&#xff08;Alternating Least Squares&#xff0c;交替最小二乘&#xff09;算法属于User-Item CF&#xff0c;也叫做混合CF&#xff0c;它同时考虑了User和Item两个方面&#xff0c;通过数量相对少的未被观察到的隐藏因子&#xff0c;来…...

QT第三讲

思维导图 蜡笔小新闹钟 需求&#xff1a; 实现 widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include<QTime> //时间类 #include<QTimerEvent> //事件处理类 #include<QtTextToSpeech> //文本转语音类 #include<QMessageBo…...

Linux内核的I2C驱动框架详解------这应该是我目前600多篇博客中耗时最长的一篇博客

目录 1 I2C驱动整体框架图 2 I2C控制器 2.1 I2C控制器设备--I2C控制器在内核中也被看做一个设备 2.2 i2c控制器驱动程序 2.3 platform_driver结构体中的probe函数做了什么 2.3.1 疑问&#xff1a; i2cdev_notifier_call函数哪里来的 2.3.2 疑问&#xff1a;为什么有两…...

【点云处理教程】05-Python 中的点云分割

一、说明 这是我的“点云处理”教程的第 5 篇文章。“点云处理”教程对初学者友好&#xff0c;我们将在其中简单地介绍从数据准备到数据分割和分类的点云处理管道。 在上一教程中&#xff0c;我们看到了如何过滤点云以减少噪声或其密度。在本教程中&#xff0c;我们将应用一些聚…...

代码随想录算法训练营之JAVA|第十七天| 654. 最大二叉树

今天是第17天刷leetcode&#xff0c;立个flag&#xff0c;打卡60天。 算法挑战链接 654. 最大二叉树https://leetcode.cn/problems/maximum-binary-tree/description/ 第一想法 错误的想法&#xff0c;就不说了。 看完代码随想录之后的想法 用递归模拟真实的过程 如果我…...

C++重写函数、隐藏函数、重载函数的区别对比

目录 1.函数重载 1.1定义 1.2函数重载的规则&#xff1a; 1.3函数重载的作用&#xff1a; 2.函数重写&#xff1a; 2.1定义 2.2例子&#xff1a; 3.函数隐藏 3.1定义 3.2举个例子&#xff1a; 1.函数重载 1.1定义 我们在学类和对象的封装特性时学过一个词叫重载&#xff0c…...

15.python设计模式【函数工厂模式】

1.知识讲解 内容&#xff1a;定义一个字典&#xff0c;在python中一切皆对象&#xff0c;将所有的函数进行封装&#xff0c;然后定一个分发函数进行分发&#xff0c;将原来if…else全部干掉。角色&#xff1a; 函数&#xff08;function&#xff09;函数工厂&#xff08;funct…...