Re: [ansible-project] Error- When Condition

2022-08-19 Thread farrukh ahmed
Thank you, gentlemen, for your precious time and efforts, in clearing all 
the minor doubts.

Both the solutions worked for me, that Walter and Lewis presented. But 
Lewis optimized this more.

It was a productive discussion though.

Thanks & regards,

FARRUKH AHMED



On Tuesday, August 16, 2022 at 4:58:31 PM UTC+5 walte...@nist.gov wrote:

> Yes, that works also, and is simpler than my example. Nice!
>
> +++
> ---
> - name: testing when
>   hosts: localhost
>   become: no
>   gather_facts: no
>   vars:
> my_lines:
>   - "idle-queue:idle-queue_00 RUNNING   pid 32292, uptime 
> 1:21:01"
>
>   - "idle-queue:idle-queue_01 RUNNING   pid 32293, uptime 
> 1:21:01"
>   - "idle-queue:idle-queue_02 RUNNING   pid 32291, uptime 
> 1:21:01"
>   tasks:
>
> - debug: msg="running found"
>
> *  when: my_lines is search('RUNNING') *+++
>
> ... yields ...
>
>
> +++
> % ansible-playbook -i localhost, foo.yml
>
> PLAY [testing when] 
> 
>
> TASK [debug] 
> ***
> ok: [localhost] => {
>
> *"msg": "running found" *}
>
> PLAY RECAP 
> *
> localhost  : ok=1changed=0unreachable=0  
>   failed=0skipped=0rescued=0ignored=0   
> +++
>
> Walter 
> --
> Walter Rowe, Chief
> Infrastructure Services
> Office of Information Systems Management
> National Institute of Standards and Technology
> United States Department of Commerce
>

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/695c4a74-eb5b-42bc-8e22-e74dff316a48n%40googlegroups.com.


Re: [ansible-project] Error- When Condition

2022-08-16 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
Yes, that works also, and is simpler than my example. Nice!

+++
---
- name: testing when
  hosts: localhost
  become: no
  gather_facts: no
  vars:
my_lines:
  - "idle-queue:idle-queue_00 RUNNING   pid 32292, uptime 1:21:01"
  - "idle-queue:idle-queue_01 RUNNING   pid 32293, uptime 1:21:01"
  - "idle-queue:idle-queue_02 RUNNING   pid 32291, uptime 1:21:01"
  tasks:

- debug: msg="running found"
  when: my_lines is search('RUNNING')
+++

... yields ...

+++
% ansible-playbook -i localhost, foo.yml

PLAY [testing when] 


TASK [debug] 
***
ok: [localhost] => {
"msg": "running found"
}

PLAY RECAP 
*
localhost  : ok=1changed=0unreachable=0failed=0
skipped=0rescued=0ignored=0
+++

Walter
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3F9B2FC8-8FFD-403C-BCF3-96F9D2DFF709%40nist.gov.


Re: [ansible-project] Error- When Condition

2022-08-16 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
Example:

+++
---
- name: testing when
  hosts: localhost
  become: no
  gather_facts: no
  vars:
my_lines:
  - "idle-queue:idle-queue_00 RUNNING   pid 32292, uptime 1:21:01"
  - "RUNNING"
  - "idle-queue:idle-queue_01 RUNNING   pid 32293, uptime 1:21:01"
  - "idle-queue:idle-queue_02 RUNNING   pid 32291, uptime 1:21:01"
  tasks:

- debug: msg="running found"
  when: '"RUNNING" in my_lines'
+++


+++
% ansible-playbook -i localhost, foo.yml

PLAY [testing when] 


TASK [debug] 
***
ok: [localhost] => {
"msg": "running found"
}

PLAY RECAP 
*
localhost  : ok=1changed=0unreachable=0failed=0
skipped=0rescued=0ignored=0
+++

Walter
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/FB38EBE7-6319-4232-BD5A-F078CD1F6FB5%40nist.gov.


Re: [ansible-project] Error- When Condition

2022-08-16 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
I stand corrected. The syntax does work. I think I had a type of my own causing 
the error I experienced with it. Thank you for correcting me.

It still does not produce the desired result. My prior example does, and for 
the same reason I stated previously.

This "when" clause does not do an effective "grep" of each line in the list.

  when: '"RUNNING" in my_lines'

It does not find the word "RUNNING" in any of the list of strings. It will only 
be true of any of the lines is exactly "RUNNING" and nothing else.

This playbook:

+++
---
- name: testing when
  hosts: localhost
  become: no
  gather_facts: no
  vars:
my_lines:
  - "idle-queue:idle-queue_00 RUNNING   pid 32292, uptime 1:21:01"
  - "idle-queue:idle-queue_01 RUNNING   pid 32293, uptime 1:21:01"
  - "idle-queue:idle-queue_02 RUNNING   pid 32291, uptime 1:21:01"
  tasks:

- debug: msg="running found"
  when: '"RUNNING" in my_lines'
+++

yields this output:

+++
% ansible-playbook -i localhost, foo.yml

PLAY [testing when] 


TASK [debug] 
***
skipping: [localhost]

PLAY RECAP 
*
localhost  : ok=0changed=0unreachable=0failed=0
skipped=1rescued=0ignored=0
+++

Walter
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

On Aug 15, 2022, at 4:53 PM, jbor...@gmail.com 
mailto:jborea...@gmail.com>> wrote:

That's not true, I do it all the time. The raw yaml value needs to be quoted 
but you still need to quote the inner string value to make sure it's 
interpreted as a string, e.g.

when: '"RUNNING" in job_check.stdout_lines'

On Tuesday, August 16, 2022 at 6:20:02 AM UTC+10 
walte...@nist.gov wrote:
I agree, and placing it in quotes doesn't fix it. I tested your suggestion and 
it also failed. It doesn't like starting a 'when' clause with a quote in any 
form.
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

On Monday, August 15, 2022 at 3:44:26 PM UTC-4 Felix Fontein wrote:
Hi,

> You do not need double quote. A 'when' condition does not need to be
> quoted.

this has nothing to do with 'when' conditions, but with YAML parsing.

A statement such as

> when: "RUNNING" in job_check.stdout_lines

will result in a YAML parsing error. Actually the error output from
Ansible is pretty helpful here:

> This one looks easy to fix. It seems that there is a value started
> with a quote, and the YAML parser is expecting to see the line ended
> with the same kind of quote. For instance:
>
> when: "ok" in result.stdout
>
> Could be written as:
>
> when: '"ok" in result.stdout'
>
> Or equivalently:
>
> when: "'ok' in result.stdout"

Cheers,
Felix

--
You received this message because you are subscribed to a topic in the Google 
Groups "Ansible Project" group.
To unsubscribe from this topic, visit 
https://groups.google.com/d/topic/ansible-project/daKrxfpknXo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to 
ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/88bf67d4-23fb-4b6f-8387-2cba80db70ean%40googlegroups.com.

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/56C36584-4B52-4883-9AB8-ED8757CF84A4%40nist.gov.


Re: [ansible-project] Error- When Condition

2022-08-13 Thread Vladimir Botka
On Sat, 13 Aug 2022 17:58:02 -0400
Nico Kadel-Garcia  wrote:

> On Fri, Aug 12, 2022 at 2:48 AM Vladimir Botka  wrote:
> >
> > On Fri, 12 Aug 2022 11:01:01 +0500
> > farrukh ahmed  wrote:
> >  
> > > *What I'm trying to achieve is that;*
> > > *1) Check if any job process is running.*
> > > *2) If the job process found running then; first stop the process and then
> > > remove the process.*  
> >
> > Use *pkill*
> > https://www.commandlinux.com/man-page/man1/pkill.1.html
> 
> pkill is a useful tool, and a dangerous one if other processes may
> have matching names. Use it cautiously.

Right. Probably it would be good to add that cautious usage here
includes running *pgrep* first and see whether the selected processes
are what you really want. This is the reason why *pgrep/pkill* come
together.

FWIW. See the playbook for testing asynchronous scripts and signals
https://gist.github.com/vbotka/755c3c4fea3d7afb54406412325a3f12


-- 
Vladimir Botka

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20220814014019.7ee02329%40gmail.com.


pgp2DwgwJ2c6j.pgp
Description: OpenPGP digital signature


Re: [ansible-project] Error- When Condition

2022-08-13 Thread Nico Kadel-Garcia
On Fri, Aug 12, 2022 at 2:48 AM Vladimir Botka  wrote:
>
> On Fri, 12 Aug 2022 11:01:01 +0500
> farrukh ahmed  wrote:
>
> > *What I'm trying to achieve is that;*
> > *1) Check if any job process is running.*
> > *2) If the job process found running then; first stop the process and then
> > remove the process.*
>
> Use *pkill*
> https://www.commandlinux.com/man-page/man1/pkill.1.html
>
> --
> Vladimir Botka

pkill is a useful tool, and a dangerous one if other processes may
have matching names. Use it cautiously.

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAOCN9rzSpW0ReL6dOv1E%3DjpMuH4%3D240UEnw7HbQSUa6kz5ZRbg%40mail.gmail.com.


Re: [ansible-project] Error- When Condition

2022-08-12 Thread Vladimir Botka
On Fri, 12 Aug 2022 11:01:01 +0500
farrukh ahmed  wrote:

> *What I'm trying to achieve is that;*
> *1) Check if any job process is running.*
> *2) If the job process found running then; first stop the process and then
> remove the process.*

Use *pkill*
https://www.commandlinux.com/man-page/man1/pkill.1.html

-- 
Vladimir Botka

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20220812084835.2c61b36e%40gmail.com.


pgpoPmq4HgynN.pgp
Description: OpenPGP digital signature


[ansible-project] Error- When Condition

2022-08-12 Thread farrukh ahmed
Hello Team,

Hope you all are doing well.

Here is my case:

Tasks > remove.yml

- name: "Check if any job is running"  shell: |
supervisorctl status "{{ job_name }}:*"  register: job_check
- debug:msg: "{{ job_check.stdout_lines }}"  notify:
- stop job
- remove job  when: RUNNING in job_check.stdout_lines


Output:

TASK [supervisord : Check if any job is running]


changed: [34.204.229.0] => {
"changed": true,
"cmd": "supervisorctl status \"idle-queue:*\"\n",
"delta": "0:00:00.137281",
"end": "2022-08-12 05:46:48.807981",
"invocation": {
"module_args": {
"_raw_params": "supervisorctl status \"idle-queue:*\"\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"rc": 0,
"start": "2022-08-12 05:46:48.670700",
"stderr": "",
"stderr_lines": [],
"stdout": "idle-queue:idle-queue_00 RUNNING   pid 32292,
uptime 1:21:01\nidle-queue:idle-queue_01 RUNNING   pid 32293,
uptime 1:21:01\nidle-queue:idle-queue_02 RUNNING   pid 32291,
uptime 1:21:01",
"stdout_lines": [
"idle-queue:idle-queue_00 RUNNING   pid 32292, uptime 1:21:01",
"idle-queue:idle-queue_01 RUNNING   pid 32293, uptime 1:21:01",
"idle-queue:idle-queue_02 RUNNING   pid 32291, uptime 1:21:01"
]}


Error:

TASK [supervisord : debug]
**
fatal: [34.204.229.0]: FAILED! => {
"msg": "The conditional check 'RUNNING in job_check.stdout_lines'
failed. The error was: error while evaluating conditional (RUNNING in
job_check.stdout_lines): 'RUNNING' is undefined\n\nThe error appears
to be in 
'/home/farrukh/Documents/work/devrim/repositories/ansible-playbooks/roles/supervisord/tasks/remove_job.yml':
line 8, column 3, but may\nbe elsewhere in the file depending on the
exact syntax problem.\n\nThe offending line appears to be:\n\n\n-
debug:\n  ^ here\n"}


*What I'm trying to achieve is that;*
*1) Check if any job process is running.*
*2) If the job process found running then; first stop the process and then
remove the process.*

PS: using handlers.

Handlers > main.yml

---
# handlers file for supervisord
- name: "{{ action }} job"
  shell: |
supervisorctl {{ action }}
  when: action == "update" or action == "reread"
- name: "{{ action }} job"
  shell: |
supervisorctl {{ action }} "{{ job_name }}:*"
  when: action != "update" or action != "reread"

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAL6Xq1d4Cn-HVn%2Bc0rJEhtOOhGTD5eAKb_JCB%3DbJvp5%2BjgB5dg%40mail.gmail.com.