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.
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.
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.
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.