teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

タイポ修正

2021/02/24 10:56

投稿

mit0223
mit0223

スコア3401

answer CHANGED
@@ -3,7 +3,7 @@
3
3
  具体的には、
4
4
  [https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/default.py#L146](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/default.py#L146)
5
5
  の _dump_results に sort_keys=False を指定すると、属性順序が保存されて出力されました。
6
- さらに調べた結果、 to_nice_yaml フィルタは sort_keys=False を受け付けてくれることがわかり、以下の playbook で解決できました。 to_json は sort_keys=True がデフォルトでした。
6
+ さらに調べた結果、 to_nice_yaml フィルタは sort_keys=False を受け付けてくれることがわかり、以下の playbook で解決できました。 to_json は sort_keys=False がデフォルトでした。
7
7
 
8
8
  playbook
9
9
  ```yaml

1

to_json の仕様について間違っていたので訂正

2021/02/24 10:56

投稿

mit0223
mit0223

スコア3401

answer CHANGED
@@ -3,7 +3,7 @@
3
3
  具体的には、
4
4
  [https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/default.py#L146](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/default.py#L146)
5
5
  の _dump_results に sort_keys=False を指定すると、属性順序が保存されて出力されました。
6
- さらに調べた結果、 to_nice_yaml フィルタは sort_keys=False を受け付けてくれることがわかり、以下の playbook で解決できました。 to_json は sort_keys=False は効かないようでした。(すみません、私は YAML の出力が欲しかったので、調査はここまでとなってます)
6
+ さらに調べた結果、 to_nice_yaml フィルタは sort_keys=False を受け付けてくれることがわかり、以下の playbook で解決できました。 to_json は sort_keys=True がデフォルトでした。
7
7
 
8
8
  playbook
9
9
  ```yaml
@@ -13,12 +13,15 @@
13
13
  d: 1
14
14
  b: 3
15
15
  test_yaml: "{{ test | to_nice_yaml(sort_keys=False) }}"
16
+ test_yaml_default: "{{ test | to_nice_yaml() }}"
16
- test_json: "{{ test | to_json(sort_keys=False) }}"
17
+ test_json: " {{ test | to_json(sort_keys=True) }}"
18
+ test_json_default: " {{ test | to_json() }}"
17
19
  tasks:
18
20
  - debug: var=test
19
21
  - debug: var=test_yaml
22
+ - debug: var=test_yaml_default
20
23
  - debug: var=test_json
21
-
24
+ - debug: var=test_json_default
22
25
  ```
23
26
 
24
27
  結果:
@@ -47,12 +50,19 @@
47
50
 
48
51
  TASK [debug] **********************************************************************************************************************************************************************************************
49
52
  ok: [localhost] => {
50
- "test_json": {
53
+ "test_yaml_default": "b: 3\nd: 1\n"
51
- "b": 3,
52
- "d": 1
53
- }
54
54
  }
55
55
 
56
+ TASK [debug] **********************************************************************************************************************************************************************************************
57
+ ok: [localhost] => {
58
+ "test_json": " {\"b\": 3, \"d\": 1}"
59
+ }
60
+
61
+ TASK [debug] **********************************************************************************************************************************************************************************************
62
+ ok: [localhost] => {
63
+ "test_json_default": " {\"d\": 1, \"b\": 3}"
64
+ }
65
+
56
66
  PLAY RECAP ************************************************************************************************************************************************************************************************
57
- localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
67
+ localhost : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
58
68
  ```