ansible可以将表达式的值定义为变量吗?

时间:2017-09-26 10:15:47

标签: ansible

我有一个如下所示的剧本。

   - 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可以将表达式的值定义为变量吗?

1 个答案:

答案 0 :(得分: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