Meet a demand today when using Ansible-playbook after executing a script, according to the content returned by the script to determine whether to continue execution or break execution, query official website found using Register Register can be used to achieve the recording script output, using when+ Fail module to determine whether to proceed or break down, here is a simple example:
Ansible service on 172.16.133.128, first executes a script, the script returns success when a directory/tmp/test is created on 172.16.133.129, and if return failed interrupts execution
First configure a simple script on the 172.16.133.129/tmp/test.sh
#!/bin/bashecho "Failed"
Configuring the Yml file test.yml, configuring the Execute Script section first
----hosts:172.16.133.129 remote_user:root tasks:-Command:/tmp/test.sh register:result
The register register is used here and what is stored, you can use the-v parameter to view the output
Run with Ansible-playbook
[[email protected] ansible]# ansible-playbook -v test.yml play [ 172.16.133.129] ********************************************************* gathering facts ok: [172.16.133.129]task: [ command /tmp/test.sh] ************************************************** changed: [ 172.16.133.129] => {"changed": true, "cmd": ["/tmp/test.sh"], "Delta": " 0:00:00.002602 ", " End ": " 2016-04-11 17:00:57.227517 ", " RC ": 0, " Start ": " 2016-04-11 17:00:57.224915 ", " stderr ": " ", " stdout ": " failed ", " warnings ": []}play recap ******************************************************************** 172.16.133.129 : ok=2 changed=1 &nBsp;unreachable=0 failed=0
Register the information stored in the "+" after the dictionary information, the information is stored in the result variable
You can see that "stdout" is the standard output information for a script, and you can use "when" to determine whether to execute or skip
Modify Test.yml
----hosts:172.16.133.129 remote_user:root tasks:-Command:/tmp/test.sh REGISTER:RESULT-FILE:PA Th=/tmp/test state=directory When:result.stdout = = ' failed '
The Create directory operation is performed when the script output is ' failed ' and the output is not ' failed ', then the execution is skipped
After executing YML, the output is as follows:
[[email protected] ansible]# ansible-playbook -v test.yml play [ 172.16.133.129] ********************************************************* gathering facts ok: [172.16.133.129]task: [ command /tmp/test.sh] ************************************************** changed: [ 172.16.133.129] => {"changed": true, "cmd": ["/tmp/test.sh"], "Delta": " 0:00:00.002618 ", " End ": " 2016-04-11 17:11:07.957097 ", " RC ": 0, " Start ": " 2016-04-11 17:11:07.954479 ", " stderr ": " ", " stdout ": " failed ", " warnings ": []}task: [file path=/tmp/test state=directory] *********************************** changed: [172.16.133.129] => {"changed": true, "GID": 0, "group": " Root ", " mode ": " 0755 ", &nbsP; " Owner ": " root ", " path ": "/tmp/test ", " size ": 4096, " state ": " directory ", "UID": 0}play recap ******************************************************************** 172.16.133.129 : ok=3 changed=2 unreachable=0 failed=0
Modify test.sh, Output ' success ', and then execute TEST.YML
[[email protected] ansible]# ansible-playbook -v test.yml play [ 172.16.133.129] ********************************************************* gathering facts ok: [172.16.133.129]task: [ command /tmp/test.sh] ************************************************** changed: [ 172.16.133.129] => {"changed": true, "cmd": ["/tmp/test.sh"], "Delta": " 0:00:00.002611 ", " End ": " 2016-04-11 17:14:08.456293 ", " RC ": 0, " Start ": " 2016-04-11 17:14:08.453682 ", " stderr ": " ", " stdout ": " success ", " warnings ": []}task: [file path=/tmp/test state=directory] *********************************** skipping: [172.16.133.129]play recap **************************************************** 172.16.133.129 : ok=2 Changed=1 unreachable=0 failed=0
You can see that when STDOUT is success, the operation to create the directory is skipped directly.
Now back to the end of the goal, when the script output as ' failed ', we want to interrupt ansible-playbook execution, rather than skip, it needs to be implemented with the Fail module, and can throw the custom information
Modify test.sh file, Output ' failed '
Modify Test.yml
----hosts:172.16.133.129 remote_user:root tasks:-command:/tmp/test.sh register:result-name:if Stdou T ' failed ', Interrupt execution fail:msg= "Check failed" when:result.stdout = = ' failed '-file:path=/tmp/te St State=directory
If script execution returns ' failed ', interrupt execution, and throw the message "check failed"
[[email protected] ansible]# ansible-playbook -v test.yml play [ 172.16.133.129] ********************************************************* gathering facts ok: [172.16.133.129]task: [ command /tmp/test.sh] ************************************************** changed: [ 172.16.133.129] => {"changed": true, "cmd": ["/tmp/test.sh"], "Delta": " 0:00:00.002632 ", " End ": " 2016-04-11 17:27:47.248996 ", " RC ": 0, " Start ": " 2016-04-11 17:27:47.246364 ", " stderr ": " ", " stdout ": " failed ", " warnings ": []}task: [check ,interrupt execution] ******************************************** failed: [172.16.133.129] => {"Failed": true}msg: check failedfatal: all hosts have already failed -- abortingplay recap ********************************************************** to retry, use: --limit @/root/test.retry172.16.133.129 : ok=2 changed=1 unreachable=0 failed=1
Playbook error when running to the second task and throws Msg
Ansible Official documents: Http://docs.ansible.com/ansible/playbooks_conditionals.html#register-variables
Note: The data in the register can be used, such as "RC", "stderr" and so on, of course, there are many ways, the text of the ' failed ' is strictly match, can also use fuzzy Lookup, such as Result.stdout.find (' failed ')! =- 1 can also achieve the same effect
This article is from the "Wandering Fish" blog, please make sure to keep this source http://faded.blog.51cto.com/6375932/1762688
Ansible-playbook how to judge and interrupt execution