从命令输出中获取变量中的值并在 ansible 的下一个命令中使用该变量
Getting a value in a variable from a command output and using using that variable in next command in ansible
ok: [localhost] => {
"command_output.stdout_lines": [
"Enter SYS, SYSTEM and PDB Admin user password: ",
"Retype SYS, SYSTEM and PDB Admin user password: ",
"{",
" \"jobId\" : \"17d14f12-4403-48f6-a0fe-00d645af86a9\",",
" \"status\" : \"Created\",",
" \"message\" : null,",
" \"reports\" : [ ],",
" \"createTimestamp\" : \"February 02, 2022 12:08:00 PM UTC\",",
" \"resourceList\" : [ ],",
" \"description\" : \"Database service creation with db name: AkashDB4\",",
" \"updatedTime\" : \"February 02, 2022 12:08:00 PM UTC\"",
"}"
]
}
如何将 jobId 的值放入 ansible 的变量中?
然后 reject
filter will allow you to throw out the prompt lines, leaving only JSON in the remaining list[str]
, which join
将组合回 str
并且 from_json
会按照它说的去做
- debug:
msg: >-
{{ (command_output.stdout_lines
| reject("match", ".*user password:.*")
| join("") | from_json).jobId
}}
vars:
command_output:
stdout_lines: [
"Enter SYS, SYSTEM and PDB Admin user password: ",
"Retype SYS, SYSTEM and PDB Admin user password: ",
"{",
" \"jobId\" : \"17d14f12-4403-48f6-a0fe-00d645af86a9\",",
" \"status\" : \"Created\",",
" \"message\" : null,",
" \"reports\" : [ ],",
" \"createTimestamp\" : \"February 02, 2022 12:08:00 PM UTC\",",
" \"resourceList\" : [ ],",
" \"description\" : \"Database service creation with db name: AkashDB4\",",
" \"updatedTime\" : \"February 02, 2022 12:08:00 PM UTC\"",
"}"
]
生产
ok: [localhost] => {
"msg": "17d14f12-4403-48f6-a0fe-00d645af86a9"
}
ok: [localhost] => {
"command_output.stdout_lines": [
"Enter SYS, SYSTEM and PDB Admin user password: ",
"Retype SYS, SYSTEM and PDB Admin user password: ",
"{",
" \"jobId\" : \"17d14f12-4403-48f6-a0fe-00d645af86a9\",",
" \"status\" : \"Created\",",
" \"message\" : null,",
" \"reports\" : [ ],",
" \"createTimestamp\" : \"February 02, 2022 12:08:00 PM UTC\",",
" \"resourceList\" : [ ],",
" \"description\" : \"Database service creation with db name: AkashDB4\",",
" \"updatedTime\" : \"February 02, 2022 12:08:00 PM UTC\"",
"}"
]
}
如何将 jobId 的值放入 ansible 的变量中?
然后 reject
filter will allow you to throw out the prompt lines, leaving only JSON in the remaining list[str]
, which join
将组合回 str
并且 from_json
会按照它说的去做
- debug:
msg: >-
{{ (command_output.stdout_lines
| reject("match", ".*user password:.*")
| join("") | from_json).jobId
}}
vars:
command_output:
stdout_lines: [
"Enter SYS, SYSTEM and PDB Admin user password: ",
"Retype SYS, SYSTEM and PDB Admin user password: ",
"{",
" \"jobId\" : \"17d14f12-4403-48f6-a0fe-00d645af86a9\",",
" \"status\" : \"Created\",",
" \"message\" : null,",
" \"reports\" : [ ],",
" \"createTimestamp\" : \"February 02, 2022 12:08:00 PM UTC\",",
" \"resourceList\" : [ ],",
" \"description\" : \"Database service creation with db name: AkashDB4\",",
" \"updatedTime\" : \"February 02, 2022 12:08:00 PM UTC\"",
"}"
]
生产
ok: [localhost] => {
"msg": "17d14f12-4403-48f6-a0fe-00d645af86a9"
}