0

ansible で ec2.py 動的インベントリ スクリプトを使用して、ec2 ホストとそのタグ名のリストを抽出します。以下のような JSON のリストが返されます。

  "tag_aws_autoscaling_groupName_asg_test": [
    "aa.b.bb.55",
    "1b.b.c.d"
  ],

  "tag_aws_autoscaling_groupName_asg_unknown": [
    "aa.b.bb.55",
    "1b.b.c.e"
  ],

この出力の解析に jq を使用しています。

  1. これらの両方の ASG に共通するフィールドのみを抽出するにはどうすればよいですか?
  2. これらの ASG の両方に固有のフィールドのみを抽出するにはどうすればよいですか?
4

2 に答える 2

3

差/2

jq の「-」演算子が配列で定義される方法のuniqueため、「一意の」回答を生成するには を 1 回呼び出すだけで十分です。

def difference($a; $b): ($a | unique) - $b;

同様に、対称差の場合、「一意の」値を生成するには、1 回の並べ替え操作で十分です。

def sdiff($a; $b): (($a-$b) + ($b-$a)) | unique;

交差/2

intersect/2これは、すべてのバージョンの jq で動作group_byするのより高速なバージョンsortです。

def intersect(x;y):
  ( (x|unique) + (y|unique) | sort) as $sorted
  | reduce range(1; $sorted|length) as $i
      ([];
       if $sorted[$i] == $sorted[$i-1] then . + [$sorted[$i]] else . end) ;

交差点/2

jq 1.5 を使用している場合は、同様ですが、かなり高速な set-intersection 関数を次に示します。これは、2 つの配列の set-intersection にある要素のストリームを生成します。

def intersection(x;y):
  (x|unique) as $x | (y|unique) as $y
  | ($x|length) as $m
  | ($y|length) as $n
  | if $m == 0 or $n == 0 then empty
    else { i:-1, j:-1, ans:false }
    | while(  .i < $m and .j < $n;
        $x[.i+1] as $nextx
        | if $nextx == $y[.j+1] then {i:(.i+1), j:(.j+1), ans: true, value: $nextx}
          elif  $nextx < $y[.j+1] then .i += 1 | .ans = false
          else  .j += 1 | .ans = false
          end )
    end
  | if .ans then .value else empty end ;
于 2016-12-16T23:30:16.133 に答える