目录

一.  本地执行

二.  任务委托 

三.  任务暂停

四.  滚动执行

五.  只执行一次

六.  设置环境变量

七.  交互提示 

一.  本地执行

         我们知道ansible的是操作被控端的,所有执行的动作都是在被控端上完成的,当然在某些特定的时候我们想要有些tasks在本地(控制端)执行,这时我们就需要使用local_action语句。         前面我们提到过lookup的用法就是在控制端执行指令,将返回结果存储到变量中。然而我们的本地执行(local_action)也可以这样做,其实这样的用法也是最多的。

示例1: 通过local_action语句,获取控制端的信息,并存储为变量。

         local_action语句的用法是 connection: local 关键字

[root@clinet include]# cat local_action.yml

- hosts: test

gather_facts: no

tasks:

- name: local action info

shell:

cmd: cat /tmp/1.txt

register: system_info

connection: local

- name: debug info

debug:

msg:

- "{{ system_info.stdout }}"

- "{{ inventory_hostname }}"

[root@clinet include]#

[root@clinet include]#

 执行结果:

[root@clinet 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 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

[root@clinet ansible_2]#

二.  任务委托 

        在有些时候,我们希望运行与选定的主机或主机组相关联的task,但是这个task又不需要在选定的主 机或主机组上执行,而需要在另一台服务器上执行。 这种特性适用于以下场景:

·  在告警系统中启用基于主机的告警

·  向负载均衡器中添加或移除一台主机

·  在dns上添加或修改针对某个主机的解析

·  在存储节点上创建一个存储以用于主机挂载

·  使用一个外部程序来检测主机上的服务是否正常

使用delegate_to语句来在另一台主机上运行task:

‐ name: enable alerts for web servers

hosts: 192.168.194.129

tasks:

‐ name: enable alerts

nagios:

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等待的状态,即等待的文件或端口或者连接状态达到指定的状态时,下一个任务开始执 行。当等的对象为端口时,状态有started,stoped,即端口已经监听或者端口已经关闭;当等 待的对象为文件时,状态有present或者started,absent,即文件已创建或者删除;当等待的对 象为一个连接时,状态有drained,即连接已建立。默认为startedtimeoutwait_for的等待的超时时间,默认为300秒

示例:

#等待8080端口已正常监听,才开始下一个任务,直到超时,默认等待超时时间为300s

‐ wait_for:

port: 8080

state: started

#等待8000端口正常监听,每隔10s检查一次,直至等待超时

‐ wait_for:

port: 8000

delay: 10

#等待8000端口直至有连接建立

‐ wait_for:

host: 0.0.0.0

port: 8000

delay: 10

state: drained

#等待8000端口有连接建立,如果连接来自10.2.1.2或者10.2.1.3,则忽略。

‐ wait_for:

host: 0.0.0.0

port: 8000

state: drained

exclude_hosts: 10.2.1.2,10.2.1.3

# 等待/tmp/foo文件被创建在继续执行

- name: Wait until the file /tmp/foo is present before continuing

ansible.builtin.wait_for:

path: /tmp/foo

# 等待/tmp/foo文件被创建,且文件中包含completed,在继续执行

- name: Wait until the string "completed" is in the file /tmp/foo before continuing

ansible.builtin.wait_for:

path: /tmp/foo

search_regex: completed

# 等待/tmp/foo文件中能匹配到completed

- name: Wait until regex pattern matches in the file /tmp/foo and print the matched group

ansible.builtin.wait_for:

path: /tmp/foo

search_regex: completed (?P\w+)

register: waitfor

- ansible.builtin.debug:

msg: Completed {{ waitfor['match_groupdict']['task'] }}

# 等/var/lock/file.lock被删除

- name: Wait until the lock file is removed

ansible.builtin.wait_for:

path: /var/lock/file.lock

state: absent

# 等进程结束

- name: Wait until the process is finished and pid was destroyed

ansible.builtin.wait_for:

path: /proc/3466/status

state: absent

# 失败时输出自定义的msg信息

- name: Output customized message when failed

ansible.builtin.wait_for:

path: /tmp/foo

state: present

msg: 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 "OpenSSH"

ansible.builtin.wait_for:

port: 22

host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'

search_regex: OpenSSH

delay: 10

connection: 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 "OpenSSH"

ansible.builtin.wait_for:

port: 22

host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'

search_regex: OpenSSH

delay: 10

vars:

ansible_connection: local

四.  滚动执行

        默认情况下,ansible会并行的在所有选定的主机或主机组上执行每一个task,但有的时候,我们会希 望能够逐台运行。最典型的例子就是对负载均衡器后面的应用服务器进行更新时。通常来讲,我们会 将应用服务器逐台从负载均衡器上摘除,更新,然后再添加回去。我们可以在play中使用serial语句 来告诉ansible限制并行执行play的主机数量。

- hosts: all

gather_facts: no

serial: 1

 在上述示例中,serial的值为1,即表示在某一个时间段内,play只在一台主机上执行。如果为2,则 同时有2台主机运行play。

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

- hosts: all

gather_facts: no

serial: 1

max_fail_percentage: 25

 假如负载均衡后面有4台主机,并且有一台主机执行失败,这时ansible还会继续运行,要让Play停止 运行,则必须超过25%,所以如果想一台失败就停止执行,我们可以将max_fail_percentage的值设 为24。如果我们希望只要有执行失败,就放弃执行,我们可以将max_fail_percentage的值设为0。

五.  只执行一次

某些时候,我们希望某个task只执行一次,即使它被绑定到了多个主机上。例如在一个负载均衡器后 面有多台应用服务器,我们希望执行一个数据库迁移,只需要在一个应用服务器上执行操作即可。

可以使用run_once语句来处理:

‐ name: run the database migrateions

command: /opt/run_migrateions

run_once: true

还可以与local_action配合使用,如下:

‐ name: run the task locally, only once

command: /opt/my‐custom‐command

connection: local

run_once: true

 还可以与delegate_to配合使用,让这个只执行一次的任务在指定的机器上运行:

‐ name: run the task locally, only once

command: /opt/my‐custom‐command

run_once: true

delegate_to: app.a1‐61‐105.dev.unp

六.  设置环境变量

        在命令行下执行某些命令的时候,这些命令可能会需要依赖环境变量。比如在安装某些包的时 候,可能需要通过代理才能完成完装。或者某个脚本可能需要调用某个环境变量才能完成运行。 ansible 支持通过 environment 关键字来定义一些环境变量。

在如下场景中可能需要用到环境变量:

·  运行shell的时候,需要设置path变量

·  需要加载一些库,这些库不在系统的标准库路径当中

示例:

‐ hosts: test

tasks:

‐ name: install pip

yum:

name: python‐pip

state: installed

‐ name: install the aws tools

pip:

name: awscli

state: present

‐ name upload file to s3

shell: aws s3 put‐object ‐‐bucket=my‐test‐bucket ‐‐key={{ ansible_hostname }}/fstab ‐‐body=/etc/fstab ‐‐region=eu‐west‐1

environment:

AWS_ACCESS_KEY_ID: xxxxxx

AWS_SECRET_ACCESS_KEY: xxxxxx

 事实上,environment也可以存储在变量当中:

‐ hosts: all

remote_user: root

vars:

proxy_env:

http_proxy: http://proxy.example.com:8080

https_proxy: http://proxy.bos.example.com:8080

tasks:

‐ name: info

apt:

name:cobbler

state:installed

environment: proxy_env

七.  交互提示 

 在少数情况下,ansible任务运行的过程中需要用户输入一些数据,这些数据要么比较秘密不方便, 或者数据是动态的,不同的用户有不同的需求,比如输入用户自己的账户和密码或者输入不同的版本 号会触发不同的后续操作等。ansible的vars_prompt关键字就是用来处理上述这种与用户交互的情况 的。

- hosts: test

vars_prompt:

- name: share_user

prompt: "what is your network username?"

private: yes

- name: share_pass

prompt: "what is your network password"

private: yes

tasks:

- name: debug info1

debug:

var: share_user

- name: debug info2

debug:

var: share_pass

 vars_prompt常用选项说明:

        ·  private: 默认为yes,表示用户输入的值在命令行不可见

        ·  default:定义默认值,当用户未输入时则使用默认值

        ·  confirm:如果设置为yes,则会要求用户输入两次,适合输入密码的情况

 

精彩内容

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。