例如,当 Ansible 收集有关主机的事实时,它会获取主机的所有挂载:
"ansible_mounts": [
{
"block_available": 7800291,
"block_size": 4096,
"block_total": 8225358,
"block_used": 425067,
"device": "/dev/mapper/foobar",
"fstype": "xfs",
"inode_available": 16403366,
"inode_total": 16458752,
"inode_used": 55386,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 31949991936,
"size_total": 33691066368,
"uuid": "2ebc82cb-5bc2-4db9-9914-33d65ba350b8"
},
{
"block_available": 44648,
"block_size": 4096,
"block_total": 127145,
"block_used": 82497,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 255595,
"inode_total": 256000,
"inode_used": 405,
"mount": "/boot",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 182878208,
"size_total": 520785920,
"uuid": "c5f7eaf2-5b70-4f74-8189-a63bb4bee5f8"
},
Run Code Online (Sandbox Code Playgroud)
等等。所以我想做的是:在模板中,我想遍历数组中的所有对象并输出每个“mount”键的值。
我像这样尝试:
(% for mounts in {{ ansible_mounts }} %)
Mountpoint: {{ ansible_mounts.mount }}
(% …
Run Code Online (Sandbox Code Playgroud) 我想以机器可读的形式获取服务状态详细信息(已加载、已启用、活动、正在运行、因为、主 PID),并且我知道 systemd 工具有--output=json
选项,但如果我这样做:
systemctl status servicename --output=json --plain
Run Code Online (Sandbox Code Playgroud)
我看到类似的东西:
? snapd.service - Snappy daemon
Loaded: loaded (/lib/systemd/system/snapd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2018-04-16 11:20:07 MSK; 4h 45min ago
Main PID: 738 (snapd)
Tasks: 10 (limit: 4915)
CGroup: /system.slice/snapd.service
??738 /usr/lib/snapd/snapd
{ "__CURSOR" : "s=461ecb6a4b814913acd57572cd1e1c82;...
Run Code Online (Sandbox Code Playgroud)
日志记录采用 JSON 格式。但是如果可能的话,如何在 JSON 中获取服务状态?
我试图弄清楚如何在 nginx 中处理对特定目录发出的所有请求并返回一个没有重定向的 json 字符串。
例子:
curl -i http://example.com/api/call1/
Run Code Online (Sandbox Code Playgroud)
预期结果:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Type: application/json
Date: Fri, 13 Apr 2012 23:48:21 GMT
Last-Modified: Fri, 13 Apr 2012 22:58:56 GMT
Server: nginx
X-UA-Compatible: IE=Edge,chrome=1
Content-Length: 38
Connection: keep-alive
{"logout": true}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我在 nginx conf 中的内容:
location ~ ^/api/(.*)$ {
index /api_logout.json;
alias /path/to/file/api_logout.json;
types { }
default_type "application/json; charset=utf-8";
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试发出请求时, Content-Type 不会坚持:
$ curl -i http://example.com/api/call1/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Type: application/octet-stream
Date: Fri, 13 Apr 2012 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Sulley 模糊测试框架对服务器进行模糊测试。
我在 Wireshark 中观察到以下流。该错误涉及 JSON 解析问题,但是,当我使用 Google Chrome 的 Postman 扩展尝试相同的 HTTP POST 请求时,它成功了。
谁能解释一下这个 HTTP POST 请求可能有什么问题?JSON 似乎有效。
POST /restconf/config HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
Content-Type: application/yang.data+json
{ "toaster:toaster" : { "toaster:toasterManufacturer" : "Geqq", "toaster:toasterModelNumber" : "asaxc", "toaster:toasterStatus" : "_." }}
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: */*
Transfer-Encoding: chunked
Date: Sat, 07 Jun 2014 05:26:35 GMT
Connection: close
152
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<errors xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf">
<error>
<error-type>protocol</error-type>
<error-tag>malformed-message</error-tag>
<error-message>Error parsing input: Root element of Json …
Run Code Online (Sandbox Code Playgroud) 我有一个 JSON 文件,我需要更新一个特定的值。
{
"Comment": "comment",
"test": {
"enabled": true
},
"enabled": true,
"otherStuff": blah,
"otherStuff2": blah,
"otherStuff3": blah,
}
Run Code Online (Sandbox Code Playgroud)
我想将第二个“启用”的值更改为 false。使用 JQ Parser,我可以使用jq '.enabled '轻松检索它,但我不确定操作 JSON 的最佳方法是什么。
JSON 是我从 API 获得的响应,将来可能会发生变化,我不能依赖之前/之后的行或值。
我希望 rsyslog 以 JSON 格式编写日志消息,这需要在字符串周围使用双引号 (")。
问题是有时值本身包含双引号,并且需要转义这些值 - 但我不知道如何做到这一点。
目前我的 rsyslog.conf 包含我使用的这种格式(有点简化):
$template JsonFormat,"{\"msg\":\"%msg%\",\"app-name\":\"%app-name%\"}\n",sql
Run Code Online (Sandbox Code Playgroud)
但是当一个包含双引号的 msg 到达时,JSON 就会被破坏,例如:
user pid=21214 uid=0 auid=4294967295 msg='PAM setcred:
user="oracle" exe="/bin/su" (hostname=?, addr=?, terminal=?
result=Success)'
Run Code Online (Sandbox Code Playgroud)
变成:
{"msg":"user pid=21214 uid=0 auid=4294967295 msg='PAM setcred:
user="oracle" exe="/bin/su" (hostname=?, addr=?, terminal=?
result=Success)'","app-name":"user"}
Run Code Online (Sandbox Code Playgroud)
但我需要它成为:
{"msg":"user pid=21214 uid=0 auid=4294967295 msg='PAM setcred:
user=\"oracle\" exe=\"/bin/su\" (hostname=?, addr=?, terminal=?
result=Success)'","app-name":"user"}
Run Code Online (Sandbox Code Playgroud) 运行 aws ec2 describe-instances 将返回类似于以下内容的 json 文本:
{
"Reservations": [
{
"Instances": [
"PublicDnsName": "ec2..."
"VpcId": "vpc-...",
...
"Instances": [
Run Code Online (Sandbox Code Playgroud)
我知道对于每个“实例”我可以提取单个字段的内容,例如 PublicDnsName,使用 jq 如下:
jq '.Reservations[].Instances[].PublicDnsName'
它将列出我实例的 dns 名称但是我如何提取两个或多个字段并用空格分隔它们或逗号什么的?我希望为每个实例并排列出 PublicDnsName 和 VpcId。
具体来说,我正在寻找的是 VpcId 为空、未定义或不存在的实例列表。换句话说,我想要一个我的经典实例列表,我需要通过 api 来处理结果。