建设悦生活网站中国正规现货交易平台

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

建设悦生活网站,中国正规现货交易平台,最新热搜新闻事件,峰峰做网站目录 一. 本地执行 二. 任务委托
三. 任务暂停 四. 滚动执行 五. 只执行一次 六. 设置环境变量 七. 交互提示 一. 本地执行 我们知道ansible的是操作被控端的#xff0c;所有执行的动作都是在被控端上完成的#xff0c;当然在某些特定的时候我们想要有些tas…目录 一.  本地执行 二.  任务委托  三.  任务暂停 四.  滚动执行 五.  只执行一次 六.  设置环境变量 七.  交互提示  一.  本地执行 我们知道ansible的是操作被控端的所有执行的动作都是在被控端上完成的当然在某些特定的时候我们想要有些tasks在本地控制端执行这时我们就需要使用local_action语句。         前面我们提到过lookup的用法就是在控制端执行指令将返回结果存储到变量中。然而我们的本地执行local_action也可以这样做其实这样的用法也是最多的。 示例1 通过local_action语句获取控制端的信息并存储为变量。 local_action语句的用法是 connection: local 关键字 [rootclinet include]# cat local_action.yml

  • hosts: testgather_facts: no tasks:- name: local action infoshell: cmd: cat /tmp/1.txtregister: system_info connection: local- name: debug info debug:msg: - {{ system_info.stdout }}- {{ inventory_hostname }} [rootclinet include]# [rootclinet include]# 执行结果 [rootclinet ansible_2]# ansible-playbook yum_file/include/local_action.yml PLAY [test] ********************************************************************************************************************************************************************************************TASK [local action info] ******************************************************************************************************************************************************************************** changed: [192.168.194.129]TASK [debug info] ************************************************************************************************************************************************************************************** ok: [192.168.194.129] {msg: [client, 192.168.194.129] }PLAY RECAP ********************************************************************************************************************************************************************************************** 192.168.194.129 : ok2 changed1 unreachable0 failed0 skipped0 rescued0 ignored0 [rootclinet ansible_2]#
    二.  任务委托  在有些时候我们希望运行与选定的主机或主机组相关联的task但是这个task又不需要在选定的主 机或主机组上执行而需要在另一台服务器上执行。 这种特性适用于以下场景 ·  在告警系统中启用基于主机的告警 ·  向负载均衡器中添加或移除一台主机 ·  在dns上添加或修改针对某个主机的解析 ·  在存储节点上创建一个存储以用于主机挂载 ·  使用一个外部程序来检测主机上的服务是否正常 使用delegate_to语句来在另一台主机上运行task ‐ name: enable alerts for web servershosts: 192.168.194.129tasks:‐ name: enable alertsnagios: action:enable_alerts service:web host:{{ inventory_hostname }}delegate_to: 192.168.194.130 如果delegate_to: 127.0.0.1的时候等价于local_action 三.  任务暂停 有些情况下一些任务的运行需要等待一些状态的恢复比如某一台主机或者应用刚刚重启我们需 要需要等待它上面的某个端口开启此时就需要将正在运行的任务暂停直到其状态满足要求。 Ansible提供了wait_for模块以实现任务暂停的需求。 参数说明connect_timeout在下一个任务执行之前等待连接的超时时间delay等待一个端口或者文件或者连接到指定的状态时默认超时时间为300秒在这等待的 300s的时间里wait_for模块会一直轮询指定的对象是否到达指定的状态delay即为多长时间 轮询一次状态。hostwait_for模块等待的主机的地址默认为127.0.0.1portwait_for模块待待的主机的端口path文件路径只有当这个文件存在时下一任务才开始执行即等待该文件创建完成state等待的状态即等待的文件或端口或者连接状态达到指定的状态时下一个任务开始执 行。当等的对象为端口时状态有startedstoped即端口已经监听或者端口已经关闭当等 待的对象为文件时状态有present或者startedabsent即文件已创建或者删除当等待的对 象为一个连接时状态有drained即连接已建立。默认为startedtimeoutwait_for的等待的超时时间,默认为300秒 示例 #等待8080端口已正常监听才开始下一个任务直到超时默认等待超时时间为300s ‐ wait_for:port: 8080state: started#等待8000端口正常监听每隔10s检查一次直至等待超时 ‐ wait_for:port: 8000delay: 10#等待8000端口直至有连接建立 ‐ wait_for:host: 0.0.0.0port: 8000delay: 10state: drained#等待8000端口有连接建立如果连接来自10.2.1.2或者10.2.1.3则忽略。 ‐ wait_for:host: 0.0.0.0port: 8000state: drainedexclude_hosts: 10.2.1.2,10.2.1.3# 等待/tmp/foo文件被创建在继续执行

  • name: Wait until the file /tmp/foo is present before continuingansible.builtin.wait_for:path: /tmp/foo# 等待/tmp/foo文件被创建且文件中包含completed在继续执行

  • name: Wait until the string completed is in the file /tmp/foo before continuingansible.builtin.wait_for:path: /tmp/foosearch_regex: completed# 等待/tmp/foo文件中能匹配到completed

  • name: Wait until regex pattern matches in the file /tmp/foo and print the matched groupansible.builtin.wait_for:path: /tmp/foosearch_regex: completed (?Ptask\w)register: waitfor

  • ansible.builtin.debug:msg: Completed {{ waitfor[match_groupdict][task] }}# 等/var/lock/file.lock被删除

  • name: Wait until the lock file is removedansible.builtin.wait_for:path: /var/lock/file.lockstate: absent# 等进程结束

  • name: Wait until the process is finished and pid was destroyedansible.builtin.wait_for:path: /proc/3466/statusstate: absent# 失败时输出自定义的msg信息

  • name: Output customized message when failedansible.builtin.wait_for:path: /tmp/foostate: presentmsg: Timeout to find file /tmp/foo#在本地执行等host中的主机的openssh启动每10s检查一次300s超时

    Do not assume the inventory_hostname is resolvable and delay 10 seconds at start

  • name: Wait 300 seconds for port 22 to become open and contain OpenSSHansible.builtin.wait_for:port: 22host: {{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}search_regex: OpenSSHdelay: 10connection: local# Same as above but you normally have ansible_connection set in inventory, which overrides connection

  • name: Wait 300 seconds for port 22 to become open and contain OpenSSHansible.builtin.wait_for:port: 22host: {{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}search_regex: OpenSSHdelay: 10vars:ansible_connection: local 四.  滚动执行 默认情况下ansible会并行的在所有选定的主机或主机组上执行每一个task但有的时候我们会希 望能够逐台运行。最典型的例子就是对负载均衡器后面的应用服务器进行更新时。通常来讲我们会 将应用服务器逐台从负载均衡器上摘除更新然后再添加回去。我们可以在play中使用serial语句 来告诉ansible限制并行执行play的主机数量。

  • hosts: allgather_facts: noserial: 1 在上述示例中serial的值为1即表示在某一个时间段内play只在一台主机上执行。如果为2则 同时有2台主机运行play。 一般来讲当task失败时ansible会停止执行失败的那台主机上的任务但是继续对其他 主机执 行。在负载均衡的场景中我们会更希望ansible在所有主机执行失败之前就让play停止否则很可能 会面临所有主机都从负载均衡器上摘除并且都执行失败导致服务不可用的场景。这个时候我们可以 使用serial语句配合max_fail_percentage语句使用。 max_fail_percentage 表示当最大失败主机的比 例达到多少时ansible就让整个play失败。示例如下

  • hosts: allgather_facts: noserial: 1max_fail_percentage: 25 假如负载均衡后面有4台主机并且有一台主机执行失败这时ansible还会继续运行要让Play停止 运行则必须超过25%所以如果想一台失败就停止执行我们可以将max_fail_percentage的值设 为24。如果我们希望只要有执行失败就放弃执行我们可以将max_fail_percentage的值设为0。 五.  只执行一次 某些时候我们希望某个task只执行一次即使它被绑定到了多个主机上。例如在一个负载均衡器后 面有多台应用服务器我们希望执行一个数据库迁移只需要在一个应用服务器上执行操作即可。 可以使用run_once语句来处理 ‐ name: run the database migrateionscommand: /opt/run_migrateionsrun_once: true还可以与local_action配合使用如下 ‐ name: run the task locally, only oncecommand: /opt/my‐custom‐commandconnection: localrun_once: true 还可以与delegate_to配合使用让这个只执行一次的任务在指定的机器上运行 ‐ name: run the task locally, only oncecommand: /opt/my‐custom‐commandrun_once: truedelegate_to: app.a1‐61‐105.dev.unp 六.  设置环境变量 在命令行下执行某些命令的时候这些命令可能会需要依赖环境变量。比如在安装某些包的时 候可能需要通过代理才能完成完装。或者某个脚本可能需要调用某个环境变量才能完成运行。 ansible 支持通过 environment 关键字来定义一些环境变量。 在如下场景中可能需要用到环境变量 ·  运行shell的时候需要设置path变量 ·  需要加载一些库这些库不在系统的标准库路径当中 示例 ‐ hosts: testtasks:‐ name: install pipyum:name: python‐pipstate: installed‐ name: install the aws toolspip:name: awsclistate: present‐ name upload file to s3shell: aws s3 put‐object ‐‐bucketmy‐test‐bucket ‐‐key{{ ansible_hostname }}/fstab ‐‐body/etc/fstab ‐‐regioneu‐west‐1environment:AWS_ACCESS_KEY_ID: xxxxxxAWS_SECRET_ACCESS_KEY: xxxxxx事实上environment也可以存储在变量当中 ‐ hosts: allremote_user: rootvars:proxy_env:http_proxy: http://proxy.example.com:8080https_proxy: http://proxy.bos.example.com:8080tasks:‐ name: infoapt: name:cobbler state:installedenvironment: proxy_env 七.  交互提示  在少数情况下ansible任务运行的过程中需要用户输入一些数据这些数据要么比较秘密不方便 或者数据是动态的不同的用户有不同的需求比如输入用户自己的账户和密码或者输入不同的版本 号会触发不同的后续操作等。ansible的vars_prompt关键字就是用来处理上述这种与用户交互的情况 的。

  • hosts: testvars_prompt:- name: share_userprompt: what is your network username?private: yes- name: share_passprompt: what is your network passwordprivate: yestasks:- name: debug info1debug:var: share_user- name: debug info2debug:var: share_pass vars_prompt常用选项说明         ·  private: 默认为yes表示用户输入的值在命令行不可见         ·  default定义默认值当用户未输入时则使用默认值         ·  confirm如果设置为yes则会要求用户输入两次适合输入密码的情况