Exchange Server 2016 Update: Database Mount Error - exchange-server

I was trying to upgrade my Exchange Server 2016 CU 20 to Exchange Server 2016 CU23 and on the
Step 7 of 11: Mailbox role: Mailbox service.
The upgrade is failing with the below error.
We repaired the affected database also, and the repair was successful.
Still seeing the error, nonetheless.
We tried to reboot the server, and that also did not fix the issue.
The database in question, is in a healthy state.
Error:
The following error was generated when "$error.Clear();
$name = [Microsoft.Exchange.Management.RecipientTasks.EnableMailbox]::DiscoveryMailboxUniqueName;
$dispname = [Microsoft.Exchange.Management.RecipientTasks.EnableMailbox]::DiscoveryMailboxDisplayName;
$dismbx = get-mailbox -Filter {name -eq $name} -IgnoreDefaultScope -resultSize 1;
if( $dismbx -ne $null)
{
$srvname = $dismbx.ServerName;
if( $dismbx.Database -ne $null -and $RoleFqdnOrName -like "$srvname.*" )
{
Write-ExchangeSetupLog -info "Setup DiscoverySearchMailbox Permission.";
$mountedMdb = get-mailboxdatabase $dismbx.Database -status | where { $_.Mounted -eq $true };
if( $mountedMdb -eq $null )
{
Write-ExchangeSetupLog -info "Mounting database before stamp DiscoverySearchMailbox Permission...";
mount-database $dismbx.Database;
}
$mountedMdb = get-mailboxdatabase $dismbx.Database -status | where { $_.Mounted -eq $true };
if( $mountedMdb -ne $null )
{
$dmRoleGroupGuid = [Microsoft.Exchange.Data.Directory.Management.RoleGroup]::DiscoveryManagement_InitInfo.WellKnownGuid;
$dmRoleGroup = Get-RoleGroup -Identity $dmRoleGroupGuid -DomainController $RoleDomainController -ErrorAction:SilentlyContinue;
if( $dmRoleGroup -ne $null )
{
trap [Exception]
{
Add-MailboxPermission $dismbx -User $dmRoleGroup.Name -AccessRights FullAccess -DomainController $RoleDomainController -ErrorAction SilentlyContinue;
continue;
}
Add-MailboxPermission $dismbx -User $dmRoleGroup.Identity -AccessRights FullAccess -DomainController $RoleDomainController -ErrorAction Stop -WarningAction SilentlyContinue;
}
}
}
}
" was run: "System.InvalidOperationException: Failed to mount database "env3-ex16-AdminDB". Error: An Active Manager operation failed. Error: The database action failed. Error: Operation failed with message: MapiExceptionDatabaseError: Unable to mount database. (hr=0x80004005, ec=1108)
Diagnostic context:
Lid: 65256
Lid: 10722 StoreEc: 0x454
Lid: 1494 ---- Remote Context Beg ----
Lid: 1238 Remote Context Overflow
Lid: 34760 StoreEc: 0xFFFFFDF0
Lid: 41344 Guid: fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 35200 dwParam: 0x1700
Lid: 59596 dwParam: 0x2A034686 Msg: JI20
Lid: 43212 dwParam: 0x2A034696 Msg: JT05
Lid: 43212 dwParam: 0x2A034696 Msg: JT08
Lid: 59596 dwParam: 0x2A034696 Msg: WM19
Lid: 59596 dwParam: 0x2A034696 Msg: WM20
Lid: 59596 dwParam: 0x2A034696 Msg: WM21
Lid: 54472 StoreEc: 0x980
Lid: 42184 StoreEc: 0x454
Lid: 10786 dwParam: 0x0 Msg: 15.01.2507.006:ENV3-EX16:fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 51578 Guid: fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 1750 ---- Remote Context End ----
Lid: 1047 StoreEc: 0x454 [Database: env3-ex16-AdminDB, Server: ENV3-EX16.sptestindia.com] ---> Microsoft.Exchange.Cluster.Replay.AmDbActionWrapperException: An Active Manager operation failed. Error: The database action failed. Error: Operation failed with message: MapiExceptionDatabaseError: Unable to mount database. (hr=0x80004005, ec=1108)
Diagnostic context:
Lid: 65256
Lid: 10722 StoreEc: 0x454
Lid: 1494 ---- Remote Context Beg ----
Lid: 1238 Remote Context Overflow
Lid: 34760 StoreEc: 0xFFFFFDF0
Lid: 41344 Guid: fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 35200 dwParam: 0x1700
Lid: 59596 dwParam: 0x2A034686 Msg: JI20
Lid: 43212 dwParam: 0x2A034696 Msg: JT05
Lid: 43212 dwParam: 0x2A034696 Msg: JT08
Lid: 59596 dwParam: 0x2A034696 Msg: WM19
Lid: 59596 dwParam: 0x2A034696 Msg: WM20
Lid: 59596 dwParam: 0x2A034696 Msg: WM21
Lid: 54472 StoreEc: 0x980
Lid: 42184 StoreEc: 0x454
Lid: 10786 dwParam: 0x0 Msg: 15.01.2507.006:ENV3-EX16:fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 51578 Guid: fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 1750 ---- Remote Context End ----
Lid: 1047 StoreEc: 0x454 [Database: env3-ex16-AdminDB, Server: ENV3-EX16.sptestindia.com] ---> Microsoft.Exchange.Data.Storage.AmOperationFailedException: An Active Manager operation failed. Error: Operation failed with message: MapiExceptionDatabaseError: Unable to mount database. (hr=0x80004005, ec=1108)
Diagnostic context:
Lid: 65256
Lid: 10722 StoreEc: 0x454
Lid: 1494 ---- Remote Context Beg ----
Lid: 1238 Remote Context Overflow
Lid: 34760 StoreEc: 0xFFFFFDF0
Lid: 41344 Guid: fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 35200 dwParam: 0x1700
Lid: 59596 dwParam: 0x2A034686 Msg: JI20
Lid: 43212 dwParam: 0x2A034696 Msg: JT05
Lid: 43212 dwParam: 0x2A034696 Msg: JT08
Lid: 59596 dwParam: 0x2A034696 Msg: WM19
Lid: 59596 dwParam: 0x2A034696 Msg: WM20
Lid: 59596 dwParam: 0x2A034696 Msg: WM21
Lid: 54472 StoreEc: 0x980
Lid: 42184 StoreEc: 0x454
Lid: 10786 dwParam: 0x0 Msg: 15.01.2507.006:ENV3-EX16:fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 51578 Guid: fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 1750 ---- Remote Context End ----
Lid: 1047 StoreEc: 0x454 [Server: ENV3-EX16.sptestindia.com] ---> Microsoft.Mapi.MapiExceptionDatabaseError: MapiExceptionDatabaseError: Unable to mount database. (hr=0x80004005, ec=1108)
Diagnostic context:
Lid: 65256
Lid: 10722 StoreEc: 0x454
Lid: 1494 ---- Remote Context Beg ----
Lid: 1238 Remote Context Overflow
Lid: 34760 StoreEc: 0xFFFFFDF0
Lid: 41344 Guid: fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 35200 dwParam: 0x1700
Lid: 59596 dwParam: 0x2A034686 Msg: JI20
Lid: 43212 dwParam: 0x2A034696 Msg: JT05
Lid: 43212 dwParam: 0x2A034696 Msg: JT08
Lid: 59596 dwParam: 0x2A034696 Msg: WM19
Lid: 59596 dwParam: 0x2A034696 Msg: WM20
Lid: 59596 dwParam: 0x2A034696 Msg: WM21
Lid: 54472 StoreEc: 0x980
Lid: 42184 StoreEc: 0x454
Lid: 10786 dwParam: 0x0 Msg: 15.01.2507.006:ENV3-EX16:fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 51578 Guid: fb24818a-4b46-415b-bea4-6a2009cd595c
Lid: 1750 ---- Remote Context End ----
Lid: 1047 StoreEc: 0x454
at Microsoft.Mapi.MapiExceptionHelper.InternalThrowIfErrorOrWarning(String message, Int32 hresult, Boolean allowWarnings, Int32 ec, DiagnosticContext diagCtx, Exception innerException)
at Microsoft.Mapi.MapiExceptionHelper.ThrowIfError(String message, Int32 hresult, IExInterface iUnknown, Exception innerException)
at Microsoft.Mapi.ExRpcAdmin.MountDatabase(Guid guidStorageGroup, Guid guidMdb, Int32 ulFlags)
at Microsoft.Exchange.Cluster.Replay.StoreRpcController.<>c__DisplayClass19_0.<MountDatabase>b__0()
at Microsoft.Exchange.Cluster.Shared.SafeRefCountedTimeoutWrapper.<>c__DisplayClass21_0.<ProtectedCallWithTimeout>b__0()
at Microsoft.Exchange.Data.HA.InvokeWithTimeout.Invoke(Action invokableAction, Action foregroundAction, TimeSpan invokeTimeout, Boolean sendWatsonReportNoThrow, Object cancelEvent)
at Microsoft.Exchange.Cluster.Shared.SafeRefCountedTimeoutWrapper.ProtectedCallWithTimeout(String operationName, TimeSpan timeout, Action operation)
at Microsoft.Exchange.Cluster.ActiveManagerServer.AmStoreHelper.Mount(Guid mdbGuid, MountFlags flags)
at Microsoft.Exchange.Cluster.ActiveManagerServer.ActiveManagerCore.<>c__DisplayClass45_2.<MountDatabaseDirect>b__4()
at Microsoft.Exchange.Cluster.Replay.FailoverPerformanceTrackerBase`1.RunTimedOperation(TOpCode opCode, Action operation)
at Microsoft.Exchange.Cluster.ActiveManagerServer.ActiveManagerCore.MountDatabaseDirect(Guid mdbGuid, MountFlags storeFlags, AmMountFlags amMountFlags, AmDbActionCode actionCode)
at Microsoft.Exchange.Cluster.ActiveManagerServer.AmRpcServer.<>c__DisplayClass19_0.<MountDatabaseDirect>b__0()
at Microsoft.Exchange.Data.Storage.Cluster.HaRpcExceptionWrapperBase`2.RunRpcServerOperation(String databaseName, RpcServerOperation rpcOperation)
--- End of inner exception stack trace ---
at Microsoft.Exchange.Data.Storage.Cluster.HaRpcExceptionWrapperBase`2.ClientRethrowIfFailed(String databaseName, String serverName, RpcErrorExceptionInfo errorInfo)
at Microsoft.Exchange.Data.Storage.ActiveManager.AmRpcClientHelper.RunRpcOperationWithAuth(AmRpcOperationHint rpcOperationHint, String serverName, String databaseName, NetworkCredential networkCredential, Nullable`1 timeoutMs, InternalRpcOperation rpcOperation)
at Microsoft.Exchange.Data.Storage.ActiveManager.AmRpcClientHelper.MountDatabaseDirectEx(String serverToRpc, Guid dbGuid, AmMountArg mountArg)
at Microsoft.Exchange.Cluster.ActiveManagerServer.AmRpcClientWrapper.MountDatabaseDirectEx(String serverToRpc, Guid dbGuid, AmMountArg mountArg)
at Microsoft.Exchange.Cluster.ActiveManagerServer.AmDbAction.MountDatabaseDirect(AmServerName serverName, AmServerName lastMountedServerName, Guid dbGuid, MountFlags storeFlags, AmMountFlags amFlags, AmDbActionCode actionCode)
at Microsoft.Exchange.Cluster.ActiveManagerServer.AmDbStandaloneAction.<>c__DisplayClass1_0.<MountInternal>b__0(Object <p0>, EventArgs <p1>)
at Microsoft.Exchange.Cluster.ActiveManagerServer.AmHelper.HandleKnownExceptions(EventHandler ev)
--- End of inner exception stack trace ---
at Microsoft.Exchange.Data.Storage.Cluster.HaRpcExceptionWrapperBase`2.ClientRethrowIfFailed(String databaseName, String serverName, RpcErrorExceptionInfo errorInfo)
at Microsoft.Exchange.Data.Storage.ActiveManager.AmRpcClientHelper.RunDatabaseRpcWithReferral(AmRpcOperationHint rpcOperationHint, IADDatabase database, String targetServer, InternalRpcOperation rpcOperation)
at Microsoft.Exchange.Management.SystemConfigurationTasks.MountDatabase.RequestMount(MountFlags storeMountFlags)
at Microsoft.Exchange.Management.SystemConfigurationTasks.MountDatabase.InternalProcessRecord()
--- End of inner exception stack trace ---
at Microsoft.Exchange.Configuration.Tasks.Task.ThrowError(Exception exception, ErrorCategory errorCategory, Object target, String helpUrl)
at Microsoft.Exchange.Configuration.Tasks.Task.WriteError(Exception exception, ErrorCategory category, Object target)
at Microsoft.Exchange.Management.SystemConfigurationTasks.MountDatabase.InternalProcessRecord()
at Microsoft.Exchange.Configuration.Tasks.Task.<ProcessRecord>b__91_1()
at Microsoft.Exchange.Configuration.Tasks.Task.InvokeRetryableFunc(String funcName, Action func, Boolean terminatePipelineIfFailed)".

Related

Ansible Collect task list and print if any task has failed

In ansible, inside a inner loop I am performing below activities and registering results
- name: Check the nslookup status
shell: nslookup mydomain.com
register: nslookup_result
ignore_errors: yes
- name: Check the Time date status
shell: timedatectl status
register: timedatectl_result
ignore_errors: yes
- name: Check Date
shell: date
register: date_result
ignore_errors: yes
I am trying to print summary of the any of the task failed.
- name: Summary of Jobs
set_facts:
failed_tests: "{{ failed_tests| default([]) + 'The server has been failed with errors [failed_result.stdout]' }}"
fail:
msg: The server has been failed with errors "{{ failed_result.stdout }}"
when: failed_result.failed is true
loop:
- nslookup_result
- timedatectl_result
- date_result
loop_control:
loop_var: failed_result
- name: Check any test job failed
fail:
msg: Some job failed
when: failed_tests.length() >= 1
- name: Print the list of the failed tests
var=unsuccessful_tasks
loop: failed_tests
loop_control:
loop_var: unsuccessful_tasks
Can someone help on this as this task has been failed.
First the failed jobs print the failure message to different output attributes, sometimes in stderr and sometimes in stdout.
Example:
ok: [test-001] => {
"rc": 1,
"stderr": "",
"stderr_lines": [],
"stdout": "Server:\t\tx.x.x.x\nAddress:\tx.x.x.x#53\n\n** server can't find do.yes: NXDOMAIN",
"stdout_lines": [
"Server:\t\tx.x.x.x",
"Address:\tx.x.x.x#53",
"",
"** server can't find do.yes: NXDOMAIN"
]
}
}
ok: [test-001] => {
"rc": 1,
"stderr": "cat: /tmp/t: No such file or directory",
"stderr_lines": [
"cat: /tmp/t: No such file or directory"
],
"stdout": "",
"stdout_lines": []
}
}
you should start with stderr, and when it's empty print stdout.
Plus you're using the loop and condition a wrong way.
You can use the return value rc to check the failed task.
Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on), this field contains ‘return code’ of these utilities.
- name: Summary of Jobs
debug:
msg: "The server has been failed with errors {{ item.stderr_lines if item.stderr_lines | length > 0 else item.stdout_lines }}"
loop:
- "{{ nslookup_result }}"
- "{{ timedatectl_result }}"
- "{{ date_result }}"
when: item.rc != 0
TASK [Summary of Jobs] **************************************
ok: [localhost] => (item={'changed': True, 'stdout': "Server:\t\tx.x.x.x\nAddress:\tx.x.x.x#53\n\n** server can
't find do.yes: NXDOMAIN", 'stderr': '', 'rc': 1, 'cmd': 'nslookup do.yes', 'start': '2022-10-16 00:26:40.297689', 'end
': '2022-10-16 00:26:40.381663', 'delta': '0:00:00.083974', 'failed': True, 'msg': 'non-zero return code', 'stdout_lines
': ['Server:\t\tx.x.x.x', 'Address:\tx.x.x.x#53', '', "** server can't find do.yes: NXDOMAIN"], 'stderr_lines'
: []}) => {
"msg": "The server has been failed with errors ['Server:\\t\\tx.x.x.x', 'Address:\\tx.x.x.x#53', '', \"** server can't find do.yes: NXDOMAIN\"]"
}
ok: [localhost] => (item={'changed': True, 'stdout': '', 'stderr': 'cat: /tmp/t: No such file or directory', 'rc': 1, '
cmd': 'cat /tmp/t', 'start': '2022-10-16 00:26:40.580334', 'end': '2022-10-16 00:26:40.584115', 'delta': '0:00:00.00378
1', 'failed': True, 'msg': 'non-zero return code', 'stdout_lines': [], 'stderr_lines': ['cat: /tmp/t: No such file or d
irectory']}) => {
"msg": "The server has been failed with errors ['cat: /tmp/t: No such file or directory']"
}
skipping: [localhost] => (item={'changed': True, 'stdout': 'Sun Oct 16 00:26:40 CEST 2022', 'stderr': '', 'rc': 0, 'cmd
': 'date', 'start': '2022-10-16 00:26:40.779088', 'end': '2022-10-16 00:26:40.782367', 'delta': '0:00:00.003279', 'msg'
: '', 'stdout_lines': ['Sun Oct 16 00:26:40 CEST 2022'], 'stderr_lines': [], 'failed': False})

Register variables with dynamic names from with_items

I would like to register variables from lookup results.
My inventory:
rrules:
- name: r1
start_date: '2022-01-01 13:00:00'
- name: r2
start_date: '2022-02-02 12:00:00'
Task looks like:
- name: Create a string for a schedule
debug:
msg: "{{ query('awx.awx.schedule_rrule', 'minute', start_date=item.start_date) }}"
register: "{{ item.name }}"
with_items:
- "{{ rrules }}"
When I run the playbook, I get the error:
fatal: [127.0.0.1]: FAILED! => {"msg": "Invalid variable name in 'register' specified: '{{ item.name }}'"}
Using the register of a debug task is a terrible idea.
Do not do that, instead, use the proper module to register variables, which is the set_fact module.
With it, you can register variable with a dynamic name, as you intended it:
- set_fact:
"{{ item.name }}": >-
{{ query(
'awx.awx.schedule_rrule',
'minute',
start_date=item.start_date
) }}
loop: "{{ rrules }}"
Given those tasks:
- set_fact:
"{{ item.name }}": >-
{{ query(
'awx.awx.schedule_rrule',
'minute',
start_date=item.start_date
) }}
loop: "{{ rrules }}"
vars:
rrules:
- name: r1
start_date: '2022-01-01 13:00:00'
- name: r2
start_date: '2022-02-02 12:00:00'
- debug:
var: r1
- debug:
var: r2
It will yield:
TASK [set_fact] ***************************************************************
ok: [localhost] => (item={'name': 'r1', 'start_date': '2022-01-01 13:00:00'})
ok: [localhost] => (item={'name': 'r2', 'start_date': '2022-02-02 12:00:00'})
TASK [debug] ******************************************************************
ok: [localhost] =>
r1: DTSTART;TZID=Europe/Paris:20220101T130000 RRULE:FREQ=MINUTELY;INTERVAL=1
TASK [debug] ******************************************************************
ok: [localhost] =>
r2: DTSTART;TZID=Europe/Paris:20220202T120000 RRULE:FREQ=MINUTELY;INTERVAL=1

run a task using playbook on successful output of before task in windows

I'm executing the below YAML(ansible) file using Jenkins. I want to execute task3 only after the successful execution/output of task2 and task2 only after task1 , My YAML file is as below. How do I add the dependency for this?
For example application.yml
-name: task1
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname"
register: executeapp1
-name: task2
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname1"
register: executeapp2
-name: task3
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname1"
register: executeapp3
There is an example with handlers (i don't know if you need the registers on the next steps so I let them here.
tasks:
- name: task1
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname"
notify: task2
register: executeapp1
handlers:
- name: task2
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname1"
notify: task3
register: executeapp2
- name: task3
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname1"
register: executeapp3
Notify will only work when the task had an exit status of "changed" so it will only be executing the notify when task 1 works.
You can also do it in a worse way using when:
-name: task1
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname"
register: executeapp1
-name: task2
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname1"
register: executeapp2
when: task1 is changed
-name: task3
win_command: "C:\Windows\Microsoft.NET\FRAMEWORK\V3.9.3013\MSBuild.exe" "E:\ApplicationFolder\App.xml" -target:Execute /p:ApplicationName="appname1"
register: executeapp3
when: task2 is changed

ansible varibals do not pass to include_role

i have this ansible structure its part of large code base :
it seems that when running the main playbook the defaults/main.yml app_type:game
don't pass on to the current_version.yml and give error
why it doesn't use the default var in defaults/main.yml
also when i pass the same variable name from --extra-vars it gives me the same error
when i print the variable from tasks/main.yml
it prints the right value
what is wrong with the include_role?
C:.
├───defaults
│ main.yml
│
└───tasks
main.yml
current_version.yml
legacy_version.yml
main.yml
---
app_type: game
tasks/main.yml
#- name: set lagacy_package true
# set_fact:
# lagacy_package: true
- name: select legacy
include_role:
tasks_from: legacy_version
name: package_install
public: true
vars:
app_type: "{{ app_type }}"
when: (lagacy_package is defined) and (lagacy_package == true)
- name: select Current
include_role:
tasks_from: current_version
name: package_install
public: true
vars:
app_type: "{{ app_type }}"
when: lagacy_package is not defined
current_version.yml
- debug:
msg: "System 2 ################ {{ app_type }} ######################################## "
error from current_version.yml
2020-07-01 17:04:09,118 p=11546 u=ec2-user n=ansible | fatal: [10.0.5.71]: FAILED! =>
msg: 'An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.AnsibleError''>, original message: An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.AnsibleError''>, original message: An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.AnsibleError''>, original message: An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.AnsibleError''>, original message: An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.An
Problem is in the below code of "select legacy" task which is going to a loop for assigning app_type with itself.
app_type: "{{ app_type }}"
Changing the name of either of the variables should work like:
app_type: "{{ other_app_type }}"

Extract exact value from stdout_lines from Ansible debug

I got my playbook from which I get value what I need through debug method, However I am not able to get exact value of child of my debug var
Below is my playbook
- hosts: db
tasks:
- name: Checking if For Page Life Expectancy.
win_command: sqlcmd -q "SELECT [object_name],[counter_name],[cntr_value] FROM sys.dm_os_performance_counters WHERE [object_name] LIKE '%Manager%'AND [counter_name] = 'Page life expectancy'"
register: win_command_result
- debug:
var: win_command_result.stdout_lines.object_name
And We get output like this
TASK [debug]
ok: [db1.local] => {
"win_command_result": {
"changed": true,
"cmd": "sqlcmd -q \"SELECT [object_name],[counter_name],
[cntr_value] FROM sys.dm_os_performance_counters WHERE [object_name]
LIKE '%Manager%'AND [counter_name] = 'Page life expectancy'\"",
"delta": "0:00:01.099974",
"end": "2018-09-11 05:08:36.022907",
"failed": false,
"rc": 0,
"start": "2018-09-11 05:08:34.922933",
"stderr": "",
"stderr_lines": [],
"stdout": "object_name
counter_name
cntr_value \r\n---------------------------
----------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- --------------------\r\nSQLServer:Buffer Manager
Page life expectancy
238579\r\n\r\n(1 rows affected)\r\n",
"stdout_lines": [
"object_name
counter_name
cntr_value ",
"---------------------------------------------------------
----------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- --------------------",
"SQLServer:Buffer Manager
Page life expectancy
238579",
"",
"(1 rows affected)"
]
}
}
We only need value of cntr_value which is 238579
If i set as I though that cntr_value is child of stdout_lines
- debug:
var: win_command_result.stdout_lines.cntr_value
It says
ok: [db1.local] => {
"win_command_result.stdout_lines.cntr_value": "VARIABLE IS NOT DEFINED!"
}
How do I extract exact value of cntr_value
Your output final variable is:
myvar.win_command_result.stdout
So if you want to extract the number, do a regex:
- name: Debug
debug:
msg: "{{ myvar.win_command_result.stdout | regex_search('\\d{6}')}}"
Another option:
- name: Fact
set_fact:
mylist: "{{ myvar.win_command_result.stdout_lines | list }}"
- name: Debug
debug:
msg: "{{ item | regex_search('\\d+') }}"
with_items: "{{ mylist[2] }}"

Resources