【问题标题】:ansible start multiple service and truncate name serviceansible 启动多个服务并截断名称服务
【发布时间】:2021-01-07 23:20:08
【问题描述】:

我想在 playbook 上启动多个服务,但我想删除服务结束字符串,例如: a. 服务 => 一个 b.service => b

因为我不能使用service a.service start,但我必须使用service a start

因为变量 item 像这样存储在 default/main.yml 角色手册中:

name_services:
  - a.service
  - b.service                                                                                                                                                                                 

在 main.yml 上

- name: start multiple service
  service:
    name: "{{ item }}"
    state: started
  with_items: "{{ name_services }}"

【问题讨论】:

    标签: ansible roles


    【解决方案1】:

    使用过滤器splitext

    - name: start multiple service
      service:
        name: "{{ item | splitext | first }}"
        state: started
      with_items: "{{ name_services }}"
    

    【讨论】:

      【解决方案2】:

      这是一种实现方式,因为服务的字符串值中只有一个点 (.)。您根据点拆分并获取第一个块(点之前的字符串)。

        - debug:
            var: item.split('.')[0]
          loop: "{{ name_services }}"
      

      另一种方法是从字符串中删除.service 部分:

        - debug:
            var: item.replace('.service','')
          loop: "{{ name_services }}"
      

      干杯

      【讨论】:

        猜你喜欢
        • 2021-01-08
        • 2011-02-20
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-05
        • 2019-06-06
        • 1970-01-01
        相关资源
        最近更新 更多