当前位置: 首页 > news >正文 企聚网站建设wordpress本地转服务器 news 2025/11/3 8:13:53 企聚网站建设,wordpress本地转服务器,网上商城制作哪家好,网站源代码怎么生成网页目录 实施任务控制 一#xff0c;循环#xff08;迭代#xff09;--- loop 1#xff0c;利用loop----item循环迭代任务 2#xff0c;item---loop循环案例 1#xff0c;定义item循环列表 2#xff0c;通过变量应用列表格式 3#xff0c;字典列表#xff08;迭代嵌套子… 目录 实施任务控制 一循环迭代--- loop 1利用loop----item循环迭代任务 2item---loop循环案例 1定义item循环列表 2通过变量应用列表格式 3字典列表迭代嵌套子变量 4register变量与loop 二when条件任务语法 1 when条件测试案例 2when条件表达语法 3 多条件表达and or语法 4组合循环和有条件判断任务 三实施处理程序 1Handlers和notify触发条件 2 tags标签定义 四处理任务失败 1ignore_errors 忽略错误继续任务 2 force_handlers: yes 3 failed_when 4 change_when 满足条件输出change状态 5block、rescue、always块和错误处理 实施任务控制 一循环迭代--- loop 1利用loop----item循环迭代任务 通过循环管理员不需要编写多个使用同一模块的任务。例他们不需要编写五个任务来确保存在五个用户而是只需要编写一个任务来对含有五个用户的列表迭代从而确保他们都在。 ansible支持loop关键字对一组项目迭代任务可以配置循环以利用各个项目、列表中各个文件的内容、生成数字序列化或更为复杂的结构来重复任务。 2item---loop循环案例 loop关键字添加到任务中将应对其迭代任务的项目列表取为值。 循环item保存每个迭代过程中使用的值。 确定多个服务运行 1定义item循环列表 1查看node1主机上的httpd与named服务是否启动发现没启动 [rootnode1 ~]# systemctl status httpd [rootnode1 ~]# systemctl status named 2查看node2主机上的httpd与named服务是否启动发现没启动 [rootnode2 ~]# systemctl status httpd [rootnode2 ~]# systemctl status named 3编辑playbook4.yml文件 --- - name: play1 hosts: test tasks: - name: task1 service: name: {{ item }} state: started loop: - httpd //定义item循环列表 - named bind的服务名位named 4执行脚本文件playbook4.yml使其启动httpd服务与bind服务 [rootserver var_test]# ansible-playbook playbook4.yml 5查看node1主机上的httpd与named服务是否启动发现此时已经启动 [rootnode1 ~]# systemctl status httpd [rootnode1 ~]# systemctl status named 6查看node2主机上的httpd与named服务是否启动发现此时已经启动 [rootnode2 ~]# systemctl status httpd [rootnode2 ~]# systemctl status named 2通过变量应用列表格式 1此时node1上的httpd与named服务是关闭的 [rootnode1 ~]# systemctl status httpd [rootnode1 ~]# systemctl status named 2此时node2上的httpd与named服务是关闭的 [rootnode2 ~]# systemctl status httpd [rootnode2 ~]# systemctl status named 3编辑playbook4.yml文件 --- - name: play1 hosts: test vars: pkname: - httpd - named tasks: - name: task1 service: name: {{ item }} state: started loop: {{ pkname }} //通过变量应用列表格式 4执行playbook4.yml剧本文件 [rootserver var_test]# ansible-playbook playbook4.yml 5查看node1主机上的httpd与named服务是否启动发现此时已经启动 [rootnode1 ~]# systemctl status httpd [rootnode1 ~]# systemctl status named 6查看node2主机上的httpd与named服务是否启动发现此时已经启动 [rootnode2 ~]# systemctl status httpd [rootnode2 ~]# systemctl status named 3字典列表迭代嵌套子变量 1编辑剧本playbook4.yml --- - name: play1 hosts: test tasks: - name: task1 user: name: {{ item.name }} state: present groups: {{ item.groups }} loop: - { name: jane,groups: wheel} - { name: joe, groups: root } 2执行剧本playbook4.yml [rootserver var_test]# ansible-playbook playbook4.yml 3在node1上可以查看到playbook4中创建的用户以及附加组 [rootnode1 ~]# id jane [rootnode1 ~]# id joe 4在node2上可以查看到playbook4中创建的用户以及附加组 [rootnode2 ~]# id jane [rootnode2 ~]# id joe 4register变量与loop 1编写剧本文件playbook4.yml - name: play1 gather_facts: no hosts: localhost tasks: - name: task1 shell: echo this is my item: {{ item }} loop: - one - two register: echo_results //变量注册 - name: task2 debug: var: echo_results //变量内容显示在屏幕上 2执行剧本文件 [rootserver var_test]# ansible-playbook playbook4.yml 二when条件任务语法 when语句用于有条件运行任务。它取要测试的条件为值。如果条件满足则运行任务。如果条件不满足则跳过任务。 1 when条件测试案例 案例一可以测试当run_my_task 为true的时候执行否则不执行 1此时的node1主机上没有安装bind [rootnode1 ~]# rpm -q bind 2编辑playbool4.yml文件使得当my_task为true在node1安装bind --- - name: play1 hosts: node1 vars: my_test: true tasks: - name: task1 yum: name: bind when: my_test 3执行剧本playbool4.yml [rootserver var_test]# ansible-playbook playbook4.yml 4此时在node1主机上查看bind发现已经被安装 [rootnode1 ~]# rpm -q bind 案例2 判断my_service是否有值若有值则用作安装的软件包名称若没有定义变量则跳过任务不显示错误 1此时的node1主机上没有vsftpd包 [rootnode1 ~]# rpm -q vsftpd 2编辑playbook4.yml文件在node1上安装vsftpd包 --- - name: play1 hosts: node1 vars: my_package: vsftpd tasks: - name: task1 yum: name: {{ my_package }} when: my_package is defined 3执行剧本playbook4.yml [rootserver var_test]# ansible-playbook playbook4.yml 4此时在node1主机上查看vsftpd发现已经被安装 [rootnode1 ~]# rpm -q vsftpd 2when条件表达语法 操作 示例 等于值为字符串 ansible_machine x86_64 等于值为数字 max_memory 512 小于(小于等于) Min_memory 128 min_memory 512 大于 大于等于 Min_memory 256 max_memory 512 不等于 Min_memory ! 512 变量存在 Min_memory is defined 变量不存在 Min_memory is not defined 布尔值变量是true. 1、True 或yes 的求值为true 变量Memory_available值存在为true则执行 Memory_available 布尔值变量是false. 0、 False 或no的求值为false 变量Memory_available值存在为true则执行 Not memory_available 第一个变量的值存在在第二个变量的列表中条件满足 事实变量在变量列表中满足要求 Ansible_distribution in supported_distros - name Demonstrate the inkeyword hosts: all gather_facts: yes Vars: supperted_distros: - Redhat - Fedora Tasks: - name: Install httpd using yum, where supported yum: name: http state: present when: ansible_distribution in supperted_distros 注 When 语句的缩进由于when语句不是模块变量他必须通过缩进到任务的最高级别放置在模块的外面。 任务是YAML散列/字典when语句只是任务重的又一个键就如任务的名称以及它所使用的模块一样。通常的惯例是将可能存在的任何when关键字放在任务名称和模块的后面。 3 多条件表达and or语法 语法一 whenansible_distribution Redhat or ansible_distribution Fedora When: ansible_distribution_version 7.5 and ansible_kernel 3.10.0-327.el7.x86_64 语法二 使用and语句时则必须要所有条件为真时才执行相应任务 when: - ansible_distribution_version 7.5 - ansible_kernel 3.10.0-327.el7.x86_64 使用括号分组条件可以更好的表达复制的条件语句 When: ( ansible_distribution_version redhat and ansible_distribution_major_version 7 ) or ( ansible_distribution_version fedora and ansible_distribution_major_version 28 ) 4组合循环和有条件判断任务 对某类任务结合使用when和loop时将对每项检查when语句 yum安装软件包只要/上挂载的文件系统有超过300M的可用空间。 Ansible_mounts 事实是一组字典各自代表一个已挂在文件系统的相关事实。循环迭代每个字典只要找到了代表两个条件都为真的已挂载文件系统的字典时条件语句才得到满足。 - name: install mariadb-server if enough space on root yum: name: mariadb-server state: latest loop: { ansible_mounts } //字典循环 when: item.mount / and item.size_available 300000000 //当两个条件都满足则执行安装 三实施处理程序 1Handlers和notify触发条件 notify 通知此action可用于每个play最后被触发这样避免多次发生改变时都执行指定操作仅在所有变化发生完成后一次性的执行操作。在notify中列出的操作作为handler及notify中调用handlers中定义的操作。notify判断是否有发生改变一旦发生改变触发handlers执行指定操作handlers 操作者是task列表这些task和前述的task没有本质区别用于当关注资源发生变化时才会采取一定的操作。 案例实现只有修改了配置文件才会重启服务 1将/etc/httpd/conf/httpd.conf 复制到/目录下 [rootserver var_test]# cp /etc/httpd/conf/httpd.conf / 2编辑文件/httpd.conf将端口改为10000 #Listen 80 Listen 10000 3在受控主机node1与node2上关闭防火墙并设置selinux为permissive [rootnode1 ~]# systemctl stop firewalld [rootnode1 ~]# setenforce 0 [rootnode2 ~]# systemctl stop firewalld [rootnode2 ~]# setenforce 0 4创建playbook5.yml文件 - name: handlers test hosts: all tasks: - name: install httpd service yum: name: httpd state: installed - name: copy config file copy: src: /httpd.conf dest: /etc/httpd/conf/httpd.conf notify: - restart service - name: start httpd service service: name: httpd state: started handlers: - name: restart service service: name: httpd state: restarted 5执行剧本playbook5.yml 因为控制端的端口号为10000发生了修改所以会重启服务 [rootserver var_test]# ansible-playbook playbook5.yml 6再次执行剧本文件playbook5.yml此时的配置文件并未发生任何改变所以服务并未重新启动 [rootserver var_test]# ansible-playbook playbook5.yml 7此时再将配置文件的端口号由10000改为80 Listen 80 8再次执行剧本playbook5.yml发现又会重启服务因为/etc/httpd文件发生改变 [rootserver var_test]# ansible-playbook playbook5.yml 注意handlers写在notify模块之后 2 tags标签定义 tags标签: 通过此标签来指定playbook文件执行哪条命令 执行 -t 标签名 案例仅执行playbook5.yml中的安装httpd命令 1编辑playbook5.yml文件 - name: handlers test hosts: all tasks: - name: install httpd service yum: name: httpd state: installed tags: yum_httpd - name: copy config file copy: src: /httpd.conf dest: /etc/httpd/conf/httpd.conf notify: - restart service - name: start httpd service service: name: httpd state: started handlers: - name: restart service service: name: httpd state: restarted 2此时执行剧本文件playbook.yml会发现只执行了装包命令 [rootserver var_test]# ansible-playbook -t yum_httpd playbook5.yml 四处理任务失败 1ignore_errors 忽略错误继续任务 默认情况下任务失败时play会终止。可以通过ingnore乎略失败其他任务可以继续执行。 案例 1编辑剧本文件playbook6.yml - name: restart httpd is postfix is running hosts: all tasks: - name: get postfix server status command: /usr/bin/systemctl is-active postfix # ignore_errors: yes register: result - name: restart apache httpd based on postfix status service: name: httpd state: restarted when: result.rc 0 2此时受控端主机并未安装postfix所以任务执行会失败 [rootserver var_test]# ansible-playbook playbook6.yml 3编辑剧本文件playbook6.yml - name: restart httpd is postfix is running hosts: all tasks: - name: get postfix server status command: /usr/bin/systemctl is-active postfix ignore_errors: yes register: result - name: restart apache httpd based on postfix status service: name: httpd state: restarted when: result.rc 3 4此时受控端主机虽然并未安装postfix但是会跳过失败继续执行下面的任务 [rootserver var_test]# ansible-playbook playbook6.yml 2 force_handlers: yes 如果任务失败并且play在该主机上终止则收到play中早前任务通知的处理程序将不会运行。 1此时的/etc/http配置文件并未做任何改变所以服务httpd不会自启 [rootserver var_test]# ansible-playbook playbook5.yml 2将/etc/httpd配置文件进行修改notify触发成功但是后面的服务名错误force_handlers: yes 关键字还是会执行handlers中的命令 Listen 1004 3编辑playbook5.yml文件 - name: handlers test force_handlers: yes hosts: all tasks: - name: install httpd service yum: name: httpd state: installed - name: copy config file copy: src: /httpd.conf dest: /etc/httpd/conf/httpd.conf notify: - restart service - name: start httpd service service: name: http state: started handlers: - name: restart service service: name: httpd state: restarted 4此时执行剧本文件playbook5.yml即使后面的服务名写错了也能执行handlers [rootserver var_test]# ansible-playbook playbook5.yml 3 failed_when failed_when其实是ansible的一种错误处理机制是由fail模块使用了when条件语句的组合效果。 我们也可以直接通过fail模块和when条件语句写成如下 - name: run user shell: /usr/local/bin/create_users.sh register: result ignore_errors: yes - name: fail the play if the previous command did not succeed fail: msg: please create user when: not in result.stdout 4 change_when 满足条件输出change状态 当我们控制一些远程主机执行某些任务时当任务在远程主机上成功执行状态发生更改时会返回changed状态响应状态未发生更改时会返回OK状态响应当任务被跳过时会返回skipped状态响应。我们可以通过changed_when来手动更改changed响应状态。 1Playbook5.yml案例中如果配置文件/httpd.conf并未发生改变执行显示ok [rootserver var_test]# ansible-playbook playbook5.yml 2编辑playbook5.yml文件 - name: handlers test hosts: all tasks: - name: install httpd service yum: name: httpd state: installed - name: copy config file copy: src: /httpd.conf dest: /etc/httpd/conf/httpd.conf register: cmd_v1 changed_when: not cmd_v1.changed notify: - restart service - name: start httpd service service: name: httpd state: started handlers: - name: restart service service: name: httpd state: restarted 3修改后此时来执行剧本文件playbook5.yml已经由ok状态变为changed状态 [rootserver var_test]# ansible-playbook playbook5.yml 5block、rescue、always块和错误处理 通过块也可结合rescue和always语句来处理错误如果块中的任何任务失败则执行其rescue块中的任务来进行恢复。在block子句中的任务以及rescue子句中的任务如果出现故障运行之后always子句中的任务运行。 总结 block: 定义要运行的主要的任务。rescue: 定义要在block子句中定义的任务失败时运行的任务。always:定义始终都独立运行的任务不论block和rescue子句中定义的任务是否成功还是失败。block有错才会执行rescuealways怎么都会执行 案例 在playbook中实施块即使block子句中定义的任务失败rescue和always子句中定义的任务也会执行。 创建目录文件和文本文件注意如果默认路径下存在同名文件需要进一步处理才能创建该文件。 1编辑playbook7.yml文件 --- - name: hosts: localhost tasks: - name: block module block: - name: create directory file: name: /aaa state: directory rescue: - name: backup filenmae shell: mv /aaa /aaa.backup - name: create directory force file: name: /aaa state: directory force: yes always: - name: create file file: name: /newfile state: touch 2执行剧本playbook7.yml文件 [rootserver var_test]# ansible-playbook playbook7.yml 3目录/aaa与文件/newfile已经被创建 [rootserver var_test]# ls -l /aaa [rootserver var_test]# ls -l /newfile [rootserver var_test]# cd /aaa 文章转载自: http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.khpx.cn.gov.cn.khpx.cn http://www.morning.cfynn.cn.gov.cn.cfynn.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.bwjws.cn.gov.cn.bwjws.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.fhddr.cn.gov.cn.fhddr.cn http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.lwxsy.cn.gov.cn.lwxsy.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.qlsbz.cn.gov.cn.qlsbz.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.qwzpd.cn.gov.cn.qwzpd.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.nbfkk.cn.gov.cn.nbfkk.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.bwttp.cn.gov.cn.bwttp.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.nmnhs.cn.gov.cn.nmnhs.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.hhnhb.cn.gov.cn.hhnhb.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.qqxmj.cn.gov.cn.qqxmj.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn 查看全文 http://www.tj-hxxt.cn/news/273212.html 相关文章: 公司网站建设需要哪些方面网站建设定制开发 网站销售需要注册公司吗上海电子门户网站建设数据 最新网站源码下载万网网站建设购买过程 建设公司网站报价适合个人网站 网站建设和管理专业好不好电子科技产品东莞网站建设 网站制作 杭州公司建设企业网站所遵循的一般原则 网站备案需要准备什么材料wordpress 瑜伽课程 浙江建设厅网站施工员报名济南哪家公司做网站 山东省两学一做网站做网站服务费税率 企业网站访问对象有哪些c2c交易平台官网 网页制作与网站建设ppt合肥市房产信息网官网 插画师个人网站是怎么做的通化网站制作 门头沟网站建设公司成都最新热门事件 云南网站开发公司介绍郑州网络营销公司有哪些 做的网站搜不到学电子商务后悔死了 免费学校网站系统三个字公司名字聚财 杭州做网站的公司哪些比较好cento安装wordpress 网站和网店区别做网站维护要学些什么 哪个网站可以做笔译兼职排名优化网站 百度下载安装如何做网站性能优化 顺昌网站建设产品的网络推广要点 网站建设 应该考虑什么住房与建设管理局 青岛提供网站建设哪家便宜郑州网站建设三牛 漯河企业网站建设wordpress 英文转中文 wap网站建设管理制度做外贸如何建立网站 导航网站帝国cms模版湖州童装网站 天津网站建设论坛关于网站建设的申请 怎么快速优化网站排名购物平台取名字 关于茶文化网站建设的背景福州做企业网站 可信网站认证价格wordpress 数组