Both get-service and sc.exe returns textual results. I will have to match
strings to check if service exists and its state and i never like to base
my conditions on pattern matching.
The best solution i could thing of is to go with win_service and define a
failed_when situation along with. At least i will have the results in
proper json objects.
On Thursday, 17 March 2016 15:23:03 UTC+5:30, J Hawkesworth wrote:
>
> You could run a raw powershell command like
>
> Get-Service nameOfService -ErrorAction SilentlyContinue
>
> and then register the result, then check
>
> You would need the '-ErrorAction SilentlyContinue' bit otherwise when the
> service doesn't exist, Get-Service will fail.
>
> also if you prefer you could do something similar with the sc.exe like this
>
> sc.exe query serviceName
>
> Hope this helps.
>
>
> On Thursday, 17 March 2016 05:26:43 UTC, ishan jain wrote:
>>
>> Agreed with your point - we should drag the machine in the state we want.
>> But in my case the number 1 priority is to make the script idempotent,
>> which seems like quite a thing on windows end.
>>
>> I not installing anything on the windows end and my application will run
>> as standalone service, no installation required. I am using NSSM to install
>> the services, so win_package is not going to be of any use to me.
>>
>> To make my script idempotent, i must first check if the service exists,
>> before i invoke NSSM to create the service. I can live with the complicated
>> conditionals i will have in my scripts, but they must be idempotent in the
>> end.
>>
>> The solution where i check the service with win_service works fine except
>> that in the case it is not present, it will give me a lot of red
>> statements. What i am looking for is a better way where i can simply
>> check that if a given service is created or not. I wish NSSM had some API
>> to check for that.
>>
>> On Thursday, 17 March 2016 02:13:52 UTC+5:30, J Hawkesworth wrote:
>>>
>>> I suggest you add a task, before your win_service task that installs the
>>> service if it is not yet installed. Depending on what the service is, you
>>> might be able to use win_package to install it. If it is already
>>> installed, there is not much to loose by running the win_package task a
>>> second time, as it will only install if not installed.
>>>
>>> When provisioning, I like to write playbooks that drag the machine into
>>> the state that I want it in rather than trying to write playbooks that have
>>> to handle lots of different possiblities that might exist on the different
>>> machines.
>>>
>>> Hope this helps,
>>>
>>> Jon
>>>
>>> On Wednesday, 16 March 2016 11:46:50 UTC, ishan jain wrote:
>>>>
>>>> I am trying to provision windows server 2012 R2 machine with ansible
>>>> and for that i need to gather some custom facts about the running
>>>> services.
>>>> I tried getting the information via the win_service module like this:
>>>>
>>>> ---
>>>> - hosts: windows
>>>> tasks:
>>>> - name: check services
>>>> win_service:
>>>> name: test1
>>>> register: result
>>>> ignore_errors: yes
>>>>
>>>> - debug: msg="{{result}}"
>>>>
>>>> - debug: msg="service is running"
>>>> when: result.state == 'running'
>>>>
>>>>
>>>> This works fine if the service exists but in case the service does not
>>>> exist, i get ugly looking failed message in the win_service task and as
>>>> there is no common member name in the registered variable 'result', i am
>>>> not sure how to first check if the service really exist and then proceed
>>>> to
>>>> do something further. Is there a better way to check if service exists and
>>>> if yes, what is its state ?
>>>>
>>>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/38d9de62-bc29-4761-af55-860a60e05014%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.