在Ansible中添加列表周围的单引号

发布于 2025-01-22 22:12:54 字数 730 浏览 0 评论 0原文

我有一个项目服务列表,每个项目都需要用单个引号包裹以配置某些参数。

我看到的最简单的解决方案是:

"{{services | match('quote') | join(' OR ')}}"

这仅在单个报价中包裹了列表中的第一个元素,而不是剩下的。

我还尝试了Match Regex的一些变体。

最后,我尝试在数据源中手动添加单个报价,然后加入。第一个元素保留了单个报价,但随后被剥夺了吗?这里发生了什么?

现在,它们从库存中静止不动 IE:

---
inventory:
  hosts:
    host1:
      procs:
      - splunkd.*
      services:
      - 'some service name'
      - 'another service name'
      - 'SplunkForwarder'

我需要最终结果,

"Name='some service name' OR Name='another service name'"

目前是在变量中引用的单一引用的服务。引号被剥离或忽略。

结果

Name=some service or Name=another service

I have a list of items services and each needs to be wrapped in single quotes to configure some parameters.

The simplest solution I saw posted was:

"{{services | match('quote') | join(' OR ')}}"

This only wrapped the first element in the list in a single quote but not the remaining.

I also tried some variations of match regex.

Finally I tried adding the single quote manually in the data source, then joining. The first element retained the single quotes but subsequent were stripped off? What is going on here?

Right now they are static from the inventory
i.e.:

---
inventory:
  hosts:
    host1:
      procs:
      - splunkd.*
      services:
      - 'some service name'
      - 'another service name'
      - 'SplunkForwarder'

I need the end result to be

"Name='some service name' OR Name='another service name'"

Currently with the services single quoted in variable the quotes are stripped or ignored.

Result

Name=some service or Name=another service

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2025-01-29 22:12:54

您可以使用循环来切除问题:

  tasks:
    - name: set var
      set_fact:
        result: "{{ result | d('') + _i + _o }}"
      loop: "{{ services }}"
      loop_control:
        extended: yes
      vars:
        _i: "Name='{{ item }}'"
        _o: "{{ '' if  ansible_loop.last else ' OR ' }}"

    - name: display result
      debug:
        var: result

结果:

ok: [localhost] => {
    "result": "Name='some service name' OR Name='another service name' OR Name='SplunkForwarder'"
}

与您的VAR:

- name: Join services   
  set_fact:     
    joined_services: "{{ joined_services | d('') + service + serviceAppend }}"     
  loop: "{{ services }}"     
  loop_control:       
    extended: yes     
  vars:
    service: "'{{ item }}'"
    serviceAppend: "{{ '' if  ansible_loop.last else ' OR ' }}"

you could cut your problem by using loop:

  tasks:
    - name: set var
      set_fact:
        result: "{{ result | d('') + _i + _o }}"
      loop: "{{ services }}"
      loop_control:
        extended: yes
      vars:
        _i: "Name='{{ item }}'"
        _o: "{{ '' if  ansible_loop.last else ' OR ' }}"

    - name: display result
      debug:
        var: result

result:

ok: [localhost] => {
    "result": "Name='some service name' OR Name='another service name' OR Name='SplunkForwarder'"
}

with your vars:

- name: Join services   
  set_fact:     
    joined_services: "{{ joined_services | d('') + service + serviceAppend }}"     
  loop: "{{ services }}"     
  loop_control:       
    extended: yes     
  vars:
    service: "'{{ item }}'"
    serviceAppend: "{{ '' if  ansible_loop.last else ' OR ' }}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文