小编Kon*_*kov的帖子

查询后如何计算元素数?

我有一个疑问:

.modules[].resources | select (.[]!=null)
Run Code Online (Sandbox Code Playgroud)

之后我得到了:

{ somestuff } { somestuff } { somestuff }
Run Code Online (Sandbox Code Playgroud)

毕竟,当我添加长腿时:

.modules[].resources | select (.[]!=null) | length
Run Code Online (Sandbox Code Playgroud)

我有:

1 1 1
Run Code Online (Sandbox Code Playgroud)

但我需要计算元素,所以我需要 3 个输出。我该如何实施?

事实上,从第一个查询输出创建一个数组以进一步操作它是非常有用的

[ { somestuff } , { somestuff } , { somestuff } ]
Run Code Online (Sandbox Code Playgroud)

jq

9
推荐指数
1
解决办法
5051
查看次数

为什么用"envsubst <file> file"重写文件会将其留空?

我需要将一些环境变量值放到文件中.

cat file
# $VAR

echo $VAR
# text
Run Code Online (Sandbox Code Playgroud)

当我做envsubst '$VAR $VAR' < file > file文件变空.要解决此问题,我使用envsubst '$VAR $VAR' < file | tee file 2>&1 >/dev/null.但有时它不起作用,我的意思是文件变空.第一个问题是为什么有时文件变空了,第二个问题是什么是在Linux和*BSD和MacOS中将env变量文件放入文件的真正的Unix方法?

unix bash environment-variables

5
推荐指数
1
解决办法
3988
查看次数

是否可以在带有 lineinfile ansible 模块的路径中使用变量?

我有一本剧本

---
- hosts: 127.0.0.1
  connection: local

  vars:
    WORK_DIR: /somefolder

  tasks:

  - debug:
      msg: "{{ WORK_DIR }}"

  - lineinfile:
      path: /somefolder/some.file
      regexp: '"display_name":'
      line: '  "display_name": "another_name",'
Run Code Online (Sandbox Code Playgroud)

工作正常,我有一个调试

ok: [127.0.0.1] => {
    "msg": "/somefolder"
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在路径中使用变量时


- hosts: 127.0.0.1
  connection: local

  vars:
    WORK_DIR: /somefolder

  tasks:

  - debug:
      msg: "{{ WORK_DIR }}"

  - lineinfile:
      path: "{{ WORK_DIR }}"/some.file
      regexp: '"display_name":'
      line: '  "display_name": "another_name",'
Run Code Online (Sandbox Code Playgroud)

有错误

- lineinfile:
    path: "{{ WORK_DIR }}"/some.file
                          ^ here
Run Code Online (Sandbox Code Playgroud)

问题是为什么?这是错误或功能还是其他什么?

ansible

2
推荐指数
1
解决办法
8420
查看次数

如何在 Ansible playbook 中正确使用 regex_findall?

我在 ansible playbook 中使用 regex_findall 从字符串中提取 ipv4 地址时遇到问题。这是 ansible 有问题的行似乎是:

- debug:
    msg: "{{ item.stdout | regex_findall('\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b') }}"
                                                         ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

with_items:
  - {{ foo }}

Should be written as:

with_items:
  - "{{ foo }}"

exception type: <class 'yaml.scanner.ScannerError'>
exception: while parsing a quoted scalar
in "<unicode string>", line 29, column 14 …
Run Code Online (Sandbox Code Playgroud)

regex ansible

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

ansible ×2

bash ×1

environment-variables ×1

jq ×1

regex ×1

unix ×1