How to try one task in Ansible ?

26 views
Skip to first unread message

Ryad karkar

unread,
May 16, 2019, 8:54:13 AM5/16/19
to Ansible Project
Hey all,

I have a lot of task in my repertory task/main.yml among them, this one :

- name: "Execut my script shell"
  script: ./script.sh
  register: result
  delegate_to: localhost

I don't want play this task, i want that when i execut my playbook with command ansible-playbook, ansible try only this task and he tell me there is any error or not.

do you understand ?

Can you help me please community ansible !! ;)

Thanks very much !! ^^

Regards,

Sudheer S

unread,
May 16, 2019, 8:56:44 AM5/16/19
to ansible...@googlegroups.com

Ryad karkar

unread,
May 16, 2019, 11:36:44 AM5/16/19
to Ansible Project
Hey Sudheer,

Thank you very much for your answer, but i think that you are not very well understand my questio...

The goal is that i can verify if my task who execut one script is ok.

The problem is that when i run my playbook i can see state changed on this task same if in this script is false...

Do you understand my question please ?

Sorry for my english, i'm french :)

But thank you very much for your help :)

Regards,

Brian Coca

unread,
May 16, 2019, 1:05:21 PM5/16/19
to Ansible Project
When using things like script/shell/command, Ansible has no way to
know what happened since this is arbitrary code you supply.
That said, script has some helper options like 'creates/removes' that
let you tell ansible ' my code creates this or removes this' and the
module will check these conditions and use that as a reflection of if
the script needs to run again or not with the supposition that it
already ran previously in successful fashion.


--
----------
Brian Coca

Arentas REC

unread,
May 16, 2019, 3:49:29 PM5/16/19
to Ansible Project
Simply you want dry run yourtasks. to se what will be changed? Is that corect? 

Ryad karkar

unread,
May 17, 2019, 3:35:29 AM5/17/19
to Ansible Project
Hey,

I want verify if my script is well execut and all task in my script is ok.

But when i run my script with module command or script or shell, the state is changed but i'm not rassure if all task in my script is execut ...?

Do you understand ?

Thanks very much guy :)

Regards,

Dan Linder

unread,
May 17, 2019, 10:35:58 AM5/17/19
to Ansible Project

Your second question:
But when i run my script with module command or script or shell, the state is changed but i'm not rassure if all task in my script is execut ...?

When you use the `script:` module, anything that script does on your remote system is outside of the control of Ansible.

It is up to you or the developer of the script to ensure that "all task in my script [are] executed".  Does your script have adequate error checking, does it check for dependencies with other tools, is it running as the right account?

Your first question:
I want verify if my script is well execut and all task in my script is ok.

Are you asking how you can setup your playbook so that Ansible can determine if the script is executable on the remote machine?

If you like, you could add some sanity checking to the top of the "script.sh" that only executes with a specific flag you pass to it from the Ansible execution.  For example, if you add a simple "if" condition to it that looks for a command argument `--test_by_ryad`, when that argument is found, the script does whatever checking you need, then exits with a successful exit code (0), or exits with a failure (1) if it is not.  You then setup a two step playbook:

- name: "Verify my script"
  script: ./script.sh --test_by_ryad
  delegate_to: localhost

- name: "Execut my script shell"
  script: ./script.sh
  register: result
  delegate_to: localhost

If the first "Verify my script" will run your script in your "test mode" and if it exits with a failure, the playbook will stop.  If it exits successfully it will continue to execute the script without your test parameter and do whatever your script is designed to do.

Mauricio Tavares

unread,
May 17, 2019, 11:10:38 AM5/17/19
to ansible...@googlegroups.com
I really think he is talking about how to use when to execute a
task and/or include a task list.

> --
> 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-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b906c39d-e237-4d93-b817-1118199818c4%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ryad karkar

unread,
May 20, 2019, 3:19:05 AM5/20/19
to Ansible Project
Hey,

Thank you for your answer guy !! :)

I will try find any solution for resolve this problem because Ansible is not also performly ...

Regards,

Le vendredi 17 mai 2019 17:10:38 UTC+2, Mauricio Tavares a écrit :
On Fri, May 17, 2019 at 10:36 AM Dan Linder <d...@linder.org> wrote:
>
>
> Your second question:
>>
>> But when i run my script with module command or script or shell, the state is changed but i'm not rassure if all task in my script is execut ...?
>
>
> When you use the `script:` module, anything that script does on your remote system is outside of the control of Ansible.
>
> It is up to you or the developer of the script to ensure that "all task in my script [are] executed".  Does your script have adequate error checking, does it check for dependencies with other tools, is it running as the right account?
>
> Your first question:
>>
>> I want verify if my script is well execut and all task in my script is ok.
>
>
> Are you asking how you can setup your playbook so that Ansible can determine if the script is executable on the remote machine?
>
> If you like, you could add some sanity checking to the top of the "script.sh" that only executes with a specific flag you pass to it from the Ansible execution.  For example, if you add a simple "if" condition to it that looks for a command argument `--test_by_ryad`, when that argument is found, the script does whatever checking you need, then exits with a successful exit code (0), or exits with a failure (1) if it is not.  You then setup a two step playbook:
>
> - name: "Verify my script"
>   script: ./script.sh --test_by_ryad
>   delegate_to: localhost
>
> - name: "Execut my script shell"
>   script: ./script.sh
>   register: result
>   delegate_to: localhost
>
> If the first "Verify my script" will run your script in your "test mode" and if it exits with a failure, the playbook will stop.  If it exits successfully it will continue to execute the script without your test parameter and do whatever your script is designed to do.
>
      I really think he is talking about how to use when to execute a
task and/or include a task list.

> --
> 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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages