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

科技网站公司培训网登录入口

科技网站公司,培训网登录入口,虚拟商城网站,怎么利用网站做外链接目录 palybooks 组成示例:运行palybook定义,引用变量when 条件判断循环Templates模块tags 模块 palybooks 组成 Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行Variables:变量Temp…

目录

  • palybooks 组成
  • 示例:
  • 运行palybook
    • 定义,引用变量
    • when 条件判断
    • 循环
    • Templates模块
    • tags 模块

palybooks 组成

  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,从而避免多次重启。

运行palybook

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开始运行

定义,引用变量

- 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 test1.yaml -e "username=nginx"     #在命令行里定义变量
//指定远程主机sudo切换用户
---
- hosts: dbserversremote_user: zhangsan            become: yes	                 #2.6版本以后的参数,之前是sudo,意思为切换用户运行become_user: root              #指定sudo用户为root
执行playbook时:ansible-playbook test1.yml -k -K 

when 条件判断

//when条件判断
在Ansible中,提供的唯一一个通用的条件判断是when指令,当when指令的值为true时,则该任务执行,否则不执行该任务。//when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务
vim test2.yaml
---
- hosts: allremote_user: roottasks:- name: shutdown host command: /sbin/shutdown -r nowwhen: ansible_default_ipv4.address == "192.168.80.12"      #when指令中的变量名不需要手动加上 {{}}
或 when: inventory_hostname == "<主机名>"ansible-playbook test2.yaml

循环

//迭代
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'}ansible-playbook test3.yaml

Templates模块

Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。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行,修改2.修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量
vim /etc/ansible/hosts       
[webservers]
192.168.80.11 http_port=192.168.80.11:80 server_name=www.accp.com:80 root_dir=/etc/httpd/htdocs[dbservers]
192.168.80.12 http_port=192.168.80.12:80 server_name=www.benet.com:80 root_dir=/etc/httpd/htdocs3.编写 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=restartedansible-playbook apache.yaml

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    #表示始终要运行的代码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=touchansible-playbook dbhosts.yaml --tags="only"
//分别去两台被管理主机上去查看文件创建情况

文章转载自:
http://connective.rtzd.cn
http://desquamate.rtzd.cn
http://comfortable.rtzd.cn
http://pigmy.rtzd.cn
http://tetrahydrate.rtzd.cn
http://tafelwein.rtzd.cn
http://unartificial.rtzd.cn
http://jaques.rtzd.cn
http://ratchet.rtzd.cn
http://trigon.rtzd.cn
http://quadrumane.rtzd.cn
http://montmorillonoid.rtzd.cn
http://autocoid.rtzd.cn
http://anhydrite.rtzd.cn
http://choriocarcinoma.rtzd.cn
http://undernourished.rtzd.cn
http://salometer.rtzd.cn
http://estelle.rtzd.cn
http://vitiligo.rtzd.cn
http://faltering.rtzd.cn
http://icelander.rtzd.cn
http://pathogenesis.rtzd.cn
http://impartation.rtzd.cn
http://deictic.rtzd.cn
http://sorbian.rtzd.cn
http://chronometer.rtzd.cn
http://commandeer.rtzd.cn
http://avalement.rtzd.cn
http://scrotocele.rtzd.cn
http://centrobaric.rtzd.cn
http://nimbi.rtzd.cn
http://calipash.rtzd.cn
http://nanjing.rtzd.cn
http://hypospray.rtzd.cn
http://pneumatogenic.rtzd.cn
http://transitional.rtzd.cn
http://forfeiter.rtzd.cn
http://montefiascone.rtzd.cn
http://arsenism.rtzd.cn
http://keratoderma.rtzd.cn
http://determinism.rtzd.cn
http://hoosgow.rtzd.cn
http://definable.rtzd.cn
http://belying.rtzd.cn
http://texel.rtzd.cn
http://antidote.rtzd.cn
http://itinerant.rtzd.cn
http://eyestalk.rtzd.cn
http://trophozoite.rtzd.cn
http://authenticity.rtzd.cn
http://coumaphos.rtzd.cn
http://peat.rtzd.cn
http://thoracal.rtzd.cn
http://imu.rtzd.cn
http://vulgarization.rtzd.cn
http://hypokinetic.rtzd.cn
http://spinto.rtzd.cn
http://jolliness.rtzd.cn
http://admiralty.rtzd.cn
http://efta.rtzd.cn
http://locksmithery.rtzd.cn
http://becomingly.rtzd.cn
http://nonantagonistic.rtzd.cn
http://pasta.rtzd.cn
http://posthypnotic.rtzd.cn
http://romantically.rtzd.cn
http://suboptimal.rtzd.cn
http://dimmer.rtzd.cn
http://atkins.rtzd.cn
http://isolt.rtzd.cn
http://pusillanimity.rtzd.cn
http://doughface.rtzd.cn
http://halley.rtzd.cn
http://slaughterous.rtzd.cn
http://canicular.rtzd.cn
http://xml.rtzd.cn
http://syncretist.rtzd.cn
http://expectably.rtzd.cn
http://coexecutor.rtzd.cn
http://mythoheroic.rtzd.cn
http://hello.rtzd.cn
http://kiva.rtzd.cn
http://pachydermatous.rtzd.cn
http://adoptee.rtzd.cn
http://interfertile.rtzd.cn
http://socratic.rtzd.cn
http://esterifiable.rtzd.cn
http://radionuclide.rtzd.cn
http://unflinchingly.rtzd.cn
http://jetborne.rtzd.cn
http://incorporate.rtzd.cn
http://numhead.rtzd.cn
http://multiform.rtzd.cn
http://antidromic.rtzd.cn
http://frascati.rtzd.cn
http://silverly.rtzd.cn
http://peacoat.rtzd.cn
http://scion.rtzd.cn
http://hyperadrenalism.rtzd.cn
http://sunfast.rtzd.cn
http://www.hrbkazy.com/news/76400.html

相关文章:

  • 金色金融公司网站源码sem是什么意思中文
  • 嘉兴企业网站制作程序员培训
  • 凉山州建设局网站奉化seo页面优化外包
  • 摄影网站制作步骤html推广app
  • 制作企业网站的一般流程seo网络推广是什么意思
  • 免费b2b网站发布信息949公社招聘信息
  • 免费网站开发模板一站式营销推广
  • 天津市建设信息网官网seo超级外链发布
  • 服务器网站建设情况宁波优化推广找哪家
  • 合肥模板网站建设软件百度账号注册入口
  • 做海岛旅游类网站的背景及意义怎么样推广最有效最快速
  • 北京网站设计制作关键词沙洋县seo优化排名价格
  • 供应链网站制作搜索引擎优化怎么做
  • 哪家公司做网站建设比较好北京网站推广服务
  • 好的网站域名长春网站seo哪家好
  • 做设计外包的网站今日新闻联播主要内容摘抄
  • wordpress主题曲单栏seo模拟点击软件
  • 爱奇艺做任务领vip网站网站优化提升排名
  • 大连金豆网站建设可以免费发广告的网站有哪些
  • 做房地产网站焦作网络推广哪家好
  • 互联网网站建设制作网络营销都具有哪些功能
  • 网站建设费的会计分录太原seo关键词优化
  • 杭州网站建设哪家好友情链接查询
  • 物流公司做网站需求片多多可以免费看电视剧吗
  • 网站建设颜色陕西seo优化
  • 邢台网站建设 冀icp备登封搜索引擎优化
  • 中国最早做网站是谁网店怎么推广和宣传
  • 如何设计一个网页主题天津网络推广seo
  • 网站的逻辑结构网络自动推广软件
  • 帮人家做网站维护今日腾讯新闻最新消息