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

黄石网站建设哪家好头条广告入口

黄石网站建设哪家好,头条广告入口,品牌营销,网站备案行业目录 一、Ansible 的脚本 playbook 剧本1.1playbooks的组成 二、剧本编写实验2.1定义、引用变量2.2使用远程主机sudo切换用户2.3whenn条件判断2.4迭代 三、Templates 模板四、Tags模板 一、Ansible 的脚本 playbook 剧本 1.1playbooks的组成 (1)Tasks&…

目录

  • 一、Ansible 的脚本 playbook 剧本
    • 1.1playbooks的组成
  • 二、剧本编写实验
    • 2.1定义、引用变量
    • 2.2使用远程主机sudo切换用户
    • 2.3whenn条件判断
    • 2.4迭代
  • 三、Templates 模板
  • 四、Tags模板

一、Ansible 的脚本 playbook 剧本

1.1playbooks的组成

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

在这里插入图片描述
运行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.1定义、引用变量

- 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"     #在命令行里定义变量

在这里插入图片描述
在这里插入图片描述

2.2使用远程主机sudo切换用户

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

2.3whenn条件判断

- name: three playhosts: allremote_user: roottasks:- name: create filefile: path=/opt/abc.txt state=touchwhen: ansible_default_ipv4.address=="192.168.243.103" #通过facts收集的信息过滤出匹配的主机,when中的变量名字,不需要手动加{{}}或 when: inventory_hostname == "<主机名>"ansible-playbook test2.yaml

在这里插入图片描述

2.4迭代

  • Ansible提供了很多种循环结构,一般都命名为with_items,作用等同于 loop 循环。
- 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/htdocs

在这里插入图片描述
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=restartedansible-playbook apache.yaml

在这里插入图片描述
在这里插入图片描述

四、Tags模板

  • 可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用–tags选项能实现仅运行指定的tasks。
    playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
- name: serve playremote_user: roothosts: webserverstasks:- name: touch ddd,txtfile: path=/opt/ddd.txt state=touchtags:  #标签为ddd当命令tags标签为ddd,或者没有标签才执行- ddd- name: touch 123.txtfile: path=/opt/123.txt state=touchtags:- always #alwats表示都执行- name: touch abc.txtfile: path=/opt/abc.txt state=touchtags:- aaa #同上

在这里插入图片描述

http://www.yayakq.cn/news/99462/

相关文章:

  • 百度有网站建设吗做网站美工需要会什么软件
  • 静态网站提交表单怎么做电子商务后悔死了
  • 网站制作 太原计算机速成班
  • 广州市城乡建设网站百度快照是啥
  • 百度做个网站要多少钱企业简介
  • 天津网站建设工具wordpress 插件player
  • 电商网站购物流程外国旅游网站建设现状
  • 找代理做网站网站域名归属谁es网站开发
  • 网站建设网站免费东莞市建设
  • wp 企业网站模板seo推广外包企业
  • 网站设计包括什么网站开发计入什么科目
  • 株洲电商网站建设阳江网上办事大厅
  • 营口建网站网络营销电子版教材
  • 网站图片怎么换大连搜狗推广
  • 网站制作备案上线流程网站移动端
  • 云南建设项目招标公告发布网站高端网站开发企业
  • seo建站优化中国海洋大学做英语作业的网站
  • 网站改版升级总结余姚建设公司网站
  • 河南住房和城乡建设部网站阿里巴巴外贸订单网站
  • 贵阳建站公司模板qq做兼职给网站给你
  • 旅游网页设计模板网站免费中山网站建设文化策划书
  • 邮箱注册网站昆明市建设厅网站
  • 惠州有做网站的吗iis新建网站不能访问
  • 手机网站开发怎么收费给个网站你们会感谢我的
  • python创建网站常德做网站建设的公司
  • 毕业设计网站最容易做什莫类型开发网站如何选需要注意什么
  • 一般找素材都是做哪几个网站呢安阳电话区号
  • 齐齐哈尔企业网站排名优化中国建设招投标网
  • 合肥网站关键词排名个人如何开发手机app
  • 合肥网站外包辽宁省建设厅网站升级何时结束