Ansible - 使用从 Consul 导入的 json
Ansible - using json imported from Consul
我在 Consul 上有一个 json 内容,我将其导入到我的剧本中。 json 包含帐户名和密码:
{
"account_1": "password_1",
"account_2": "password_2",
"account_3": "password_3"
}
将其注册到变量后,我希望能够使用每个用户名和密码。由于这些 json 可能包含不同数量的条目,我希望我可以为此使用 with_items。有人可以帮我吗?
调试整个 json 没有问题:
TASK [debug] **************************************************************************
ok: [localhost] => {
"accountsDataFromConsul.data.Value": {
"account_1": "password_1",
"account_2": "password_2",
"account_3": "password_3"
}
}
我尝试获取个人条目(用户名和密码)。我试过下面的代码,但它只返回用户名:
- debug:
var: item
with_items: "{{accountsDataFromConsul.data.Value}}"
TASK [debug] ******************************************************************************
ok: [localhost] => (item=account_1) => {
"item": "account_1"
}
ok: [localhost] => (item=account_2) => {
"item": "account_2"
}
ok: [localhost] => (item=account_3) => {
"item": "account_3"
}
地图数据不可通过可靠循环迭代。
看看 dict2items 过滤器。
#!/usr/bin/env ansible-playbook
- name: Lets munge some data
hosts: localhost
gather_facts: false
become: false
vars:
my_dict:
key_one: value_one
key_two: value_two
my_json: '{"key_one": "value_one", "key_two": "value_two"}'
tasks:
- name: Iterate over a dict
debug:
msg: "{{ item.key }} {{ item.value }}"
loop: "{{ my_dict | dict2items }}"
- name: Iterate over json
debug:
msg: "{{ item.key }} {{ item.value }}"
loop: "{{ my_json | from_json | dict2items }}"
PLAY [Lets munge some data] *****************************************************************************************************************************************************************************************************
TASK [Iterate over a dict] ******************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'key_one', 'value': 'value_one'}) => {
"msg": "key_one value_one"
}
ok: [localhost] => (item={'key': 'key_two', 'value': 'value_two'}) => {
"msg": "key_two value_two"
}
TASK [Iterate over json] ********************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'key_one', 'value': 'value_one'}) => {
"msg": "key_one value_one"
}
ok: [localhost] => (item={'key': 'key_two', 'value': 'value_two'}) => {
"msg": "key_two value_two"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
to use every user name and password
下图
vars:
accountsDataFromConsul:
data:
Value:
account_1: password_1
account_2: password_2
account_3: password_3
tasks:
- debug:
msg: "key: {{ item.key }} value: {{ item.value }}"
loop: "{{ accountsDataFromConsul.data.Value|dict2items }}"
给出(删节):
"msg": "key: account_1 value: password_1"
"msg": "key: account_3 value: password_3"
"msg": "key: account_2 value: password_2"
谢谢弗拉基米尔和尼克。这非常有效。我唯一要做的就是在我的 Consul 输出中使用 from_json。
- set_fact:
jsonvar: "{{accountsDataFromConsul.data.Value | from_json}}"
- debug:
msg: "{{ item.key }} {{ item.value }}"
loop: "{{jsonvar | dict2items}}"
我在 Consul 上有一个 json 内容,我将其导入到我的剧本中。 json 包含帐户名和密码:
{
"account_1": "password_1",
"account_2": "password_2",
"account_3": "password_3"
}
将其注册到变量后,我希望能够使用每个用户名和密码。由于这些 json 可能包含不同数量的条目,我希望我可以为此使用 with_items。有人可以帮我吗?
调试整个 json 没有问题:
TASK [debug] **************************************************************************
ok: [localhost] => {
"accountsDataFromConsul.data.Value": {
"account_1": "password_1",
"account_2": "password_2",
"account_3": "password_3"
}
}
我尝试获取个人条目(用户名和密码)。我试过下面的代码,但它只返回用户名:
- debug:
var: item
with_items: "{{accountsDataFromConsul.data.Value}}"
TASK [debug] ******************************************************************************
ok: [localhost] => (item=account_1) => {
"item": "account_1"
}
ok: [localhost] => (item=account_2) => {
"item": "account_2"
}
ok: [localhost] => (item=account_3) => {
"item": "account_3"
}
地图数据不可通过可靠循环迭代。
看看 dict2items 过滤器。
#!/usr/bin/env ansible-playbook
- name: Lets munge some data
hosts: localhost
gather_facts: false
become: false
vars:
my_dict:
key_one: value_one
key_two: value_two
my_json: '{"key_one": "value_one", "key_two": "value_two"}'
tasks:
- name: Iterate over a dict
debug:
msg: "{{ item.key }} {{ item.value }}"
loop: "{{ my_dict | dict2items }}"
- name: Iterate over json
debug:
msg: "{{ item.key }} {{ item.value }}"
loop: "{{ my_json | from_json | dict2items }}"
PLAY [Lets munge some data] *****************************************************************************************************************************************************************************************************
TASK [Iterate over a dict] ******************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'key_one', 'value': 'value_one'}) => {
"msg": "key_one value_one"
}
ok: [localhost] => (item={'key': 'key_two', 'value': 'value_two'}) => {
"msg": "key_two value_two"
}
TASK [Iterate over json] ********************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'key_one', 'value': 'value_one'}) => {
"msg": "key_one value_one"
}
ok: [localhost] => (item={'key': 'key_two', 'value': 'value_two'}) => {
"msg": "key_two value_two"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
to use every user name and password
下图
vars:
accountsDataFromConsul:
data:
Value:
account_1: password_1
account_2: password_2
account_3: password_3
tasks:
- debug:
msg: "key: {{ item.key }} value: {{ item.value }}"
loop: "{{ accountsDataFromConsul.data.Value|dict2items }}"
给出(删节):
"msg": "key: account_1 value: password_1"
"msg": "key: account_3 value: password_3"
"msg": "key: account_2 value: password_2"
谢谢弗拉基米尔和尼克。这非常有效。我唯一要做的就是在我的 Consul 输出中使用 from_json。
- set_fact:
jsonvar: "{{accountsDataFromConsul.data.Value | from_json}}"
- debug:
msg: "{{ item.key }} {{ item.value }}"
loop: "{{jsonvar | dict2items}}"