Ansible Script Task on Control Machine with Variable Passing - How to Execute After Remote Hosts
{"title":"Ansible Script Task on Control Machine with Variable Passing - How to Execute After Remote Hosts", "description":"Learn how to define a script task in Ansible to execute on the control machine, passing a variable to the script. This guide also covers how to ensure the script runs after operations on all remote hosts.", "keywords":"ansible, script task, control machine, delegate_to, run_once, variable passing, playbook, localhost, remote hosts", "content":"In Ansible, you can use the script module to define a script task that executes on the control machine and pass variables to the script. You can also use the delegate_to parameter to delegate the task to a specific remote host, ensuring it executes after operations on all remote machines.\n\nHere is an example playbook demonstrating how to define a script task, pass a variable, and execute after all remote hosts are finished:\n\nyaml\n- name: Execute script on control machine\n hosts: localhost\n gather_facts: false\n\n tasks:\n - name: Run script on control machine\n script:\n src: /path/to/script.sh\n executable: /bin/bash\n delegate_to: localhost\n vars:\n my_variable: \"Hello World\"\n run_once: true\n\n\nIn the example, we define a task called Run script on control machine using the script module to execute the /path/to/script.sh script, specifying /bin/bash as the executable. We delegate the task to localhost to ensure execution on the control machine. We pass a variable my_variable with the value Hello World using the vars parameter. Finally, we use run_once to limit the task to run only once, guaranteeing execution after all remote host operations.\n\nNote that the combination of delegate_to and run_once ensures the task runs only once on the control machine and after all remote hosts are finished. "}
原文地址: https://www.cveoy.top/t/topic/pWYx 著作权归作者所有。请勿转载和采集!