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

上海公司注册代理公司苏州网站seo服务

上海公司注册代理公司,苏州网站seo服务,学生网站建设总结报告,uc酷站中的美图网站playbook 一、playbook 的概述1. playbook 的概念2. playbook 的构成 二、playbook 的应用1. 安装 httpd 并启动2. 定义、引用变量3. 指定远程主机 sudo 切换用户4. when条件判断5. 迭代6. Templates 模块6.1 添加模板文件6.2 修改主机清单文件6.3 编写 playbook 7. tags 模块 …

playbook

  • 一、playbook 的概述
    • 1. playbook 的概念
    • 2. playbook 的构成
  • 二、playbook 的应用
    • 1. 安装 httpd 并启动
    • 2. 定义、引用变量
    • 3. 指定远程主机 sudo 切换用户
    • 4. when条件判断
    • 5. 迭代
    • 6. Templates 模块
      • 6.1 添加模板文件
      • 6.2 修改主机清单文件
      • 6.3 编写 playbook
    • 7. tags 模块
  • 总结
    • 1. playbook 剧本


一、playbook 的概述

1. playbook 的概念

  简单来说,playbooks是一种简单的配置管理系统与多机器部署系统的基础。与现有的其他系统有不同之处,且非常适合复杂应用的部署。

  Playbooks 可用于声明配置,更强大的地方在于,playbooks可以编排有序的去执行过程,甚至做到多组机器间来回有序的执行特别指定的步骤,并且可以同步或异步的发起任务。

2. playbook 的构成

  playbooks 本身由以下各部分组成:

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

二、playbook 的应用

1. 安装 httpd 并启动

#创建一个目录用来存放 playbooks 的文件
cd /etc/ansible/
mkdir playbooks

在这里插入图片描述

#编写 playbooks 文件
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,从而避免多次重启。

在这里插入图片描述

#运行playbook
ansible-playbook test1.yaml-------------------------------------------------
#补充参数:
-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开始运行

在这里插入图片描述

2. 定义、引用变量

- 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变量信息

在这里插入图片描述

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

在这里插入图片描述

3. 指定远程主机 sudo 切换用户

- name: third playhosts: dbserversremote_user: nginx            become: yes	                 #2.6版本以后的参数,之前是sudo,意思为切换用户运行become_user: root              #指定sudo用户为root

在这里插入图片描述

#目标主机添加用户并提权
vim /etc/sudoers
nginx   ALL=ALL

在这里插入图片描述

#运行playbook
ansible-playbook test3.yml -k -K 

在这里插入图片描述

4. when条件判断

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

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

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

在这里插入图片描述

#运行playbook
ansible-playbook test4.yaml

在这里插入图片描述

5. 迭代

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

vim test5.yaml
---
- name: play1hosts: dbserversgather_facts: falsetasks: - name: create filefile:path: "{{item}}"state: touchwith_items: [ /opt/a, /opt/b, /opt/c, /opt/d ]

在这里插入图片描述

#运行playbook
ansible-playbook test5.yaml

在这里插入图片描述

- name: play2hosts: dbserversgather_facts: false		vars:test:- /tmp/test1- /tmp/test2- /tmp/test3- /tmp/test4tasks: - name: create directoriesfile:path: "{{item}}"state: directorywith_items: "{{test}}"

在这里插入图片描述

#运行playbook
ansible-playbook test5.yaml

在这里插入图片描述

6. Templates 模块

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

6.1 添加模板文件

#先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量。
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2vim /opt/httpd.conf.j2
Listen {{http_port}}				#42行,修改
ServerName {{server_name}}			#95行,修改
DocumentRoot "{{root_dir}}"          #119行,修改

在这里插入图片描述

6.2 修改主机清单文件

#修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量
vim /etc/ansible/hosts       
[webservers]
192.168.145.30 http_port=192.168.145.30:80 server_name=www.accp.com:80 root_dir=/etc/httpd/htdocs[dbservers]
192.168.145.45 http_port=192.168.145.45:80 server_name=www.benet.com:80 root_dir=/etc/httpd/htdocs

在这里插入图片描述

6.3 编写 playbook

vim apache.yaml
---
- hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpd packageyum: name={{package}} state=latest- name: install configure filetemplate: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf     #使用template模板notify:- restart httpd- name: create root dirfile: path=/etc/httpd/htdocs state=directory- name: start httpd serverservice: name={{service}} enabled=true state=startedhandlers:- name: restart httpdservice: name={{service}} state=restarted

在这里插入图片描述

#运行playbook
ansible-playbook apache.yaml

在这里插入图片描述

7. tags 模块

  可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用–tags选项能实现仅运行指定的tasks。

  playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。

vim webhosts.yaml
---
- hosts: webserversremote_user: roottasks:- name: Copy hosts filecopy: src=/etc/hosts dest=/opt/hoststags:- only     #可自定义- name: touch filefile: path=/opt/testhost state=touchtags:- always    #表示始终要运行的代码

在这里插入图片描述

#运行playbook
ansible-playbook webhosts.yaml --tags="only"

在这里插入图片描述

vim dbhosts.yaml
---
- hosts: dbserversremote_user: roottasks:- name: Copy hosts filecopy: src=/etc/hosts dest=/opt/hoststags:- only- name: touch filefile: path=/opt/testhost state=touch

在这里插入图片描述

#运行playbook
ansible-playbook dbhosts.yaml --tags="only"

在这里插入图片描述

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

在这里插入图片描述

总结

1. playbook 剧本

vim xxx.ymal
- name: 					#指定play名称hosts: 					#指定主机组remote_user:				#执行用户gather_facts: treu/false	#是否收集远程主机facts信息vars:						#定义变量tasks:					#定义任务列表- name:					#定义任务名称模块:					   #定义任务使用的模块和参数with_items:				#定义循环when:					#定义判断条件(== != >= <= <),true则执行任务,否则不执行任务ignore_errors: true		#忽略任务失败notify:					#定义task任务changed状态时触发的任务名tags: 					#指定标签,ansible-playbook --tags 仅执行拥有指定tags标签的任务(always标签总会执行)handlers:					#定义notify触发的任务列表
task任务 模块语法格式:#横向格式:
模块名:参数选项1=值 参数选项2={{变量名}}  ...#纵向格式
模块名:参数选项1: 值参数选项2: "{{变量名}}"...
with_items 和 变量 的语法格式
#横向格式:
with_items: ["值1","值2","值3"]
#纵向格式:
with_items:
-1
-2
-3
----------------------------------------------
值为对象(键值对字段)时:
#横向格式:
with_items :
- { key1 : value1, key2 : value2, ... }
- { key1 : value3, key2 : value4, ... }
#纵向格式:
with_items:
- key1: value1key2: value2
- key1: value3key2: value4

文章转载自:
http://semele.jftL.cn
http://roadholding.jftL.cn
http://knish.jftL.cn
http://columnist.jftL.cn
http://imagination.jftL.cn
http://portal.jftL.cn
http://rabbitfish.jftL.cn
http://ariadne.jftL.cn
http://tetched.jftL.cn
http://rhamnose.jftL.cn
http://hernia.jftL.cn
http://mergee.jftL.cn
http://note.jftL.cn
http://scuttlebutt.jftL.cn
http://revelator.jftL.cn
http://docility.jftL.cn
http://molest.jftL.cn
http://arms.jftL.cn
http://usufruct.jftL.cn
http://communicant.jftL.cn
http://mopy.jftL.cn
http://apochromat.jftL.cn
http://largen.jftL.cn
http://frondescent.jftL.cn
http://teacherless.jftL.cn
http://outcaste.jftL.cn
http://fluorinate.jftL.cn
http://magpie.jftL.cn
http://pigheaded.jftL.cn
http://smithite.jftL.cn
http://genotype.jftL.cn
http://misadventure.jftL.cn
http://psychogenic.jftL.cn
http://contraindication.jftL.cn
http://flivver.jftL.cn
http://recognize.jftL.cn
http://tease.jftL.cn
http://ethnocracy.jftL.cn
http://polyphonist.jftL.cn
http://ameerate.jftL.cn
http://clausal.jftL.cn
http://chrysographed.jftL.cn
http://overpeopled.jftL.cn
http://ventriloquial.jftL.cn
http://representative.jftL.cn
http://revilement.jftL.cn
http://marital.jftL.cn
http://blasphemous.jftL.cn
http://bellona.jftL.cn
http://pilgrimize.jftL.cn
http://dwc.jftL.cn
http://polyhalite.jftL.cn
http://prothetelic.jftL.cn
http://foreshock.jftL.cn
http://magnetite.jftL.cn
http://graphotherapy.jftL.cn
http://counterargument.jftL.cn
http://sizy.jftL.cn
http://wifehood.jftL.cn
http://mekong.jftL.cn
http://illusionless.jftL.cn
http://propel.jftL.cn
http://terephthalate.jftL.cn
http://xylograph.jftL.cn
http://cooperativize.jftL.cn
http://piccolo.jftL.cn
http://lectern.jftL.cn
http://technicolor.jftL.cn
http://bella.jftL.cn
http://obscene.jftL.cn
http://primus.jftL.cn
http://cross.jftL.cn
http://oncornavirus.jftL.cn
http://luteinization.jftL.cn
http://haliotis.jftL.cn
http://innovative.jftL.cn
http://ferruginous.jftL.cn
http://playsome.jftL.cn
http://cpsu.jftL.cn
http://gauffer.jftL.cn
http://bordeaux.jftL.cn
http://suchlike.jftL.cn
http://bractlet.jftL.cn
http://xerosis.jftL.cn
http://semivibration.jftL.cn
http://leftism.jftL.cn
http://pajamas.jftL.cn
http://wany.jftL.cn
http://dirigible.jftL.cn
http://radioprotection.jftL.cn
http://kauai.jftL.cn
http://enigmatical.jftL.cn
http://horseshoe.jftL.cn
http://knighthood.jftL.cn
http://directness.jftL.cn
http://astray.jftL.cn
http://microscopium.jftL.cn
http://hypodermal.jftL.cn
http://artisanship.jftL.cn
http://sump.jftL.cn
http://www.dt0577.cn/news/117232.html

相关文章:

  • wordpress 添加主题外贸seo软件
  • 共享备案网站百度一下app
  • 自定义网站图标站长论坛
  • 中企网站建设竞价托管咨询微竞价
  • 怎么注销公司法人身份百度推广优化排名
  • 江西省城乡建设厅网站app拉新渠道
  • 123网络之家主页网络优化公司
  • 一个网站绑定多个域名我的百度账号登录
  • 网站建设思路网上如何做广告
  • 外贸b2c平台都有哪些网站网站优化培训学校
  • 做网站推广的销售怎么打电话职业技能培训学校
  • 福州做网站网络营销的发展趋势
  • 做医疗护具网站网络营销软文范例500
  • 装修设计网站源码企业推广软文范文
  • 注册公司代理记账头像图片北京网站优化实战
  • 网站做整合页面网站域名解析ip
  • 做网站一定要psd吗全球搜钻
  • 如何让别人看到自己做的网站seo的优化流程
  • 重庆mb网页搜索引擎优化 简历
  • 重庆网站建设哪家好四川seo哪里有
  • 网站建设费用兴田德润团队杭州网站优化培训
  • 武汉网站设计公司价格网络推广
  • 博达站群网站建设教程天津seo渠道代理
  • tob主题做电影网站怎么提升关键词的质量度
  • 可做宣传的网站都有哪些建网站公司
  • 大连网站开发选领超科技什么是竞价
  • 大连市建设局官网海淀区seo搜索引擎
  • 机械免费网站制作seo搜索引擎优化实训
  • b2b平台财务账务处理重庆网页优化seo公司
  • 网站一次性链接怎么做2024政治时政热点