How to try one task in Ansible ?

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 !! :wink:

Thanks very much !! ^^

Regards,

Use tags:

https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html

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 :slight_smile:

But thank you very much for your help :slight_smile:

Regards,

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.

Simply you want dry run yourtasks. to se what will be changed? Is that corect?

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 :slight_smile:

Regards,

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.

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.

Hey,

Thank you for your answer guy !! :slight_smile:

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

Regards,