2017-09-26 59 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 
相關問題