英文:
Accessing AWS RDS Arn from aws rds describe-db-instances
问题
以下是您要翻译的部分:
我正在使用Ansible访问我的帐户中的一些RDS集群。我已经使用以下命令成功运行它。
这是输入数据。
"stdout_lines": [
"{",
" \"DBInstances\": [",
" {",
" \"PubliclyAccessible\": false, ",
" \"MasterUsername\": \"xxxxx\", ",
" \"MonitoringInterval\": 0, ",
" \"LicenseModel\": \"general-public-license\", ",
" \"VpcSecurityGroups\": [",
" {",
" \"Status\": \"active\", ",
" \"VpcSecurityGroupId\": \"xxxx\"",
" }",
" ], ",
" \"InstanceCreateTime\": \"xxxxxx\", ",
" \"CopyTagsToSnapshot\": false, ",
" \"OptionGroupMemberships\": [",
" {",
" \"Status\": \"in-sync\", ",
" \"OptionGroupName\": \"xxxxx\"",
" }",
" ], ",
" \"DBInstanceArn\": \"arn:aws:rds:region:xxxx:db:xxxx\", ",
" ......."
" }",
" ], ",
" \"DBInstanceArn\": \"arn:aws:rds:region:xxxx:db:xxxx\", ",
" ......."
当运行以下命令以定位数组中的[0]项时,我收到以下输出。
"{{ (registered.stdout_lines | from_json)['DBInstances'][0]['DBInstanceArn'] }}"
好的,这是输出:
ok: [localhost] => {
"msg": "arn:aws:rds:region:xxxx:db:xxxx"
}
我可以通过像这样指定它来获取我需要的项。我需要包含数组ID,因为有3个数组。我尝试以这种方式运行它。但我获得了所有数据。
- name: Show
set_fact:
var: "item.db_instance_arn"
with_items: "{{rds.instances}}"
register: variable
我也获得了我想要的事实。我只是无法输出那个单独的事实。
是否有一种方法可以访问数组中的所有项并输出我需要的一个变量?
提前感谢。新年快乐!
英文:
I am using Ansible to access some RDS clusters in my account. I have the command working with this.
This is the input data.
"stdout_lines": [
"{",
" \"DBInstances\": [",
" {",
" \"PubliclyAccessible\": false, ",
" \"MasterUsername\": \"xxxxx\", ",
" \"MonitoringInterval\": 0, ",
" \"LicenseModel\": \"general-public-license\", ",
" \"VpcSecurityGroups\": [",
" {",
" \"Status\": \"active\", ",
" \"VpcSecurityGroupId\": \"xxxx\"",
" }",
" ], ",
" \"InstanceCreateTime\": \"xxxxxx\", ",
" \"CopyTagsToSnapshot\": false, ",
" \"OptionGroupMemberships\": [",
" {",
" \"Status\": \"in-sync\", ",
" \"OptionGroupName\": \"xxxxx\"",
........
" \"DBInstanceArn\": \"arn:aws:rds:region:xxxx:db:xxxx\", ",
.......
When running the command below targeting the [0[ item in the array I receive this output.
>"{{ (registered.stdout_lines | from_json)['DBInstances'][0]['DBInstanceArn'] }}"
ok: [localhost] => {
"msg": "arn:aws:rds:region :xxxx:db:xxxx"
}
I can get down to the item I need by specifying it like this. I need the array ID in there as there are 3 of them. I have tried to run it this way. But I get all the data.
- name: Show
set_fact:
var: "item.db_instance_arn"
with_items: "{{rds.instances}}"
register: variable
I also get the fact that I want. I just cant output that individual fact.
Is there a way to hit all the items in the array and output the one variable I need?
Thanks in advance. Happy New Year!
答案1
得分: 0
You will want (registered.stdout | from_json)
,not (registered.stdout_lines | from_json) }}
,because stdout_lines
is a list
of str
, but from_json
wants a str
-- I'm surprised your question didn't include the error message that you experienced, because it for sure produces one.
Then, once you have straightened out the JSON parsing, you will want to use the map
filter,because as you observed, DBInstances
is a list
, meaning you need to effectively iterate over its objects like so:
- debug:
msg: my DB ARNs are {{
(registered.stdout | from_json).DBInstances
| map(attribute="DBInstanceArn")
| list }}
If you prefer, you can use the json_query
filter which uses JMESPath syntax, although most of the time I find it more opaque than helpful:
- debug:
msg: my DB ARNs are {{ (registered.stdout | from_json)
| json_query('DBInstances[*].DBInstanceArn') }}
英文:
You will want (registered.stdout | from_json)
, not (registered.stdout_lines | from_json) }}
, because stdout_lines
is a list
of str
, but from_json
wants a str
-- I'm surprised your question didn't include the error message that you experienced, because it for sure produces one
Then, once you have straightened out the JSON parsing, you will want to use the map
filter, because as you observed, DBInstances
is a list
, meaning you need to effectively iterate over its objects like so:
- debug:
msg: my DB ARNs are {{
(registered.stdout | from_json).DBInstances
| map(attribute="DBInstanceArn")
| list }}
If you prefer, you can use the json_query
filter which uses JMESPath syntax, although most of the time I find it more opaque than helpful:
- debug:
msg: my DB ARNs are {{ (registered.stdout | from_json)
| json_query('DBInstances[*].DBInstanceArn') }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论