ansible register when: result | succeeded when: item.rc != 0

ansible register 这个功能非常有用。当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register 。

举个例子:

我们需要判断sda6是否存在,如果存在了就执行一些相应的脚本,则可以为该判断注册一个register变量,并用它来判断是否存在,存在返回 succeeded, 失败就是 failed.

 

ansible执行完命令后的rc=0是什么意思?

命令返回值 returncode

 

tasks:

         -  shell: echo  "I've got '{{ foo }}' and am not afraid to use it!"
           when: foo  is  defined
     
         -  fail: msg = "Bailing out. this play requires 'bar'"
           when: bar  is  not  defined

register关键字可以将任务执行结果保存到一个变量中,该变量可以在模板或者playbooks文件中使用:

1
2
3
4
5
6
7
8
9
10
  -  name: test play
       hosts:  all
     
       tasks:
     
           -  shell: cat  / etc / motd
             register: motd_contents
     
           -  shell: echo  "motd contains the word hi"
             when: motd_contents.stdout.find( 'hi' ) ! =  - 1

 上边中的例子中,通过注册变量访问返回的内容,`stdout`里面保存了命令的标准输出内容。注册变量还可以使用在`with_items`中,如果其保存的内容可以转换为列表,或者内容本身就是个列表。如果命令的输出本身就是列表,可以通过`stdout_lines`访问:

1
2
3
4
5
6
7
8
9
10
11
12
13
  -  name: registered variable usage as a with_items  list
       hosts:  all
     
       tasks:
     
           -  name: retrieve the  list  of home directories
             command: ls  / home
             register: home_dirs
     
           -  name: add home dirs to the backup spooler
             file : path = / mnt / bkspool / {{ item }} src = / home / {{ item }} state = link
             with_items: home_dirs.stdout_lines
             # same as with_items: home_dirs.stdout.split()
 

ansible register 这个功能非常有用。当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register 。

举个例子:

我们需要判断sda6是否存在,如果存在了就执行一些相应的脚本,则可以为该判断注册一个register变量,并用它来判断是否存在,存在返回 succeeded, 失败就是 failed.

- name: Create a register to represent the status if the /dev/sda6 exsited
  shell: df -h | grep sda6
  register: dev_sda6_result ignore_errors: True tags: docker - name: Copy docker-thinpool.sh to all hosts copy: src=docker-thinpool.sh dest=/usr/bin/docker-thinpool mode=0755 when: dev_sda6_result | succeeded tags: docker
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

注意:  1、register变量的命名不能用 -中横线,比如dev-sda6_result,则会被解析成sda6_resultdev会被丢掉,所以不要用- 2、ignore_errors这个关键字很重要,一定要配合设置成True,否则如果命令执行不成功,即 echo $?不为0,则在其语句后面的ansible语句不会被执行,导致程序中止。

 

配合register循环列表

register注册一个变量后,可以配合with_items来遍历变量结果。示例如下:

- shell: echo "{{ item }}"
 with_items:  - one  - two  register: echo - name: Fail if return code is not 0  fail:  msg: "The command ({{ item.cmd }}) did not have a 0 return code"  when: item.rc != 0  with_items: echo.results 
 

转载于:https://www.cnblogs.com/gaoyuechen/p/7776373.html

name: debug ping result vars: ansible_connection: local result: "{{ item.stdout[0] | parse_cli_textfsm('/usr/share/ansible/plugins/textfsm/ping') | json_query('[0]') | json_format }}" commandsH: { command: "ping {{item.item.vrf}} {{item.item.dest}} " } debug: msg: "{{result}} {{ commandsH }}" when: params is defined and item.skipped is not defined and item is succeeded loop: "{{ pingresult4.results | union(pingresult6.results) }}" - name: save ping vars: ansible_connection: local result: "{{ item.stdout[0] | parse_cli_textfsm('/usr/share/ansible/plugins/textfsm/ping') | json_query('[0]') | json_format }}" set_result: "{{ result is string | ternary(default_result, result) }}" isAlive: { isAlive: "{{ set_result.RATE > 0 | ternary(True, False) }}", RATE: "{{ set_result.RATE }}" } commandsH: { command: "ping {{item.item.vrf}} {{item.item.dest}} " } uri: url: "http://localhost:{{runServerPort}}/kafka/PerfRaw" method: POST body_format: json return_content: yes body: # 毫秒级,date命令不能计算毫秒,直接拼接 # 使用!!强制类型转换为int报错,原因尚未明确 taskId: "{{ id }}" extendInfo: "{{ item.item.extendInfo | default({}) | combine(commandsH) }}" ipFrom: "{{ host.ip }}" ip: "{{ item.item.dest }}" taskFinishTime: "{{ lookup('pipe','date +%s000') }}" result: { rping: "{{ set_result | combine(isAlive) | json_format }}" } taskType: monitor workerType: ansible monitorItemType: "{{item.item.monitorItemType}}" monitorItemList: "{{item.item.monitorItemList}}" register: this failed_when: "'\"code\":0' not in this.content" when: params is defined and item.skipped is not defined and item is succeeded loop: "{{ pingresult4.results | union(pingresult6.results) }}"
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值