2017-09-26 73 views
0

我有一个像下面这样的剧本。可以将一个表达式的值作为一个变量来确定吗?

- name: ensure crm service is exist 
    shell: crm resource status {{item.service}} 
    register: is_service_exist 
    with_items: "{{crm_services}}" 
    run_once: true 
    delegate_to: "{{controller_master}}" 
    when: crm_services is defined 
    ignore_errors: True 

    - name: msg 
    vars: 
     service_is_exist: var[{{is_service_exist.results[0].stderr.find('not found')}}==-1] 
    debug: msg={{service_is_exist}}] 

    - name: stop crm service 
    shell: crm resource stop {{item.service}} 
    with_items: "{{crm_services}}" 
    run_once: true 
    delegate_to: "{{controller_master}}" 
    when: crm_services is defined and is_service_exist.results[0].stderr.find('not found')==-1 

    - name: uninstall current rpm packages 
    shell: rpm -e --nodeps {{item.package}} 
    with_items: "{{packages}}" 
    ignore_errors: True 

我想知道crm_service是否存在,如果crm_service存在,请停止服务并卸载当前的rpm包。 我认为is_service_exist.results[0].stderr.find('not found')==-1不容易阅读,所以我想将表达式设置为一个变量,可以做到吗?

我试图var[{{is_service_exist.results[0].stderr.find('not found')}}==-1],但输出是这样的 “msg” 中: “VAR [-1 == - 1]”

所以,可以ansible定义为变量的表达式的值?

回答

0

我通常在每个register之后使用set_fact来摆脱垃圾数据。

- name: ensure crm service is exist 
    shell: crm resource status {{item.service}} 
    register: is_service_exist 
    with_items: "{{crm_services}}" 
    run_once: true 
    delegate_to: "{{controller_master}}" 
    when: crm_services is defined 
    ignore_errors: True 

- set_fact: 
    is_service_exists: "{{is_service_exist.results[0].stderr.find('not found') == -1}}" 

- name: stop crm service 
    shell: crm resource stop {{item.service}} 
    with_items: "{{crm_services}}" 
    run_once: true 
    delegate_to: "{{controller_master}}" 
    when: crm_services is defined and is_service_exist 
相关问题