📅  最后修改于: 2023-12-03 14:39:15.090000             🧑  作者: Mango
Ansible Shell is a powerful tool designed for system administrators and developers to automate tasks and processes on remote machines. It enables you to run shell commands on remote machines using Ansible.
The Ansible Shell module allows you to execute a command on a remote machine, and it is especially useful when you need to execute a command that is not supported by other built-in Ansible modules.
Here are some of the features of Ansible Shell:
The syntax for using Ansible Shell is as follows:
- name: Execute shell command
shell:
cmd: "command"
args:
warn: bool
executable: command
chdir: directory
creates: file
removes: file
stdin: ""
stdin_add_newline: bool
stdin_once: bool
strip_empty_ends: bool
umask: int
env: dict
timeout: int
use_unsafe_shell: bool
register: variable
cmd
: Required field that specifies the command to executewarn
: Boolean value that specifies whether to ignore non-zero return codesexecutable
: Command to run the script, using the /bin/sh shell by defaultchdir
: Directory to change into and run the commandcreates
: Path to a file that should not exist, and that will be created before running the commandremoves
: Path to a file that should exist, and that will be removed after running the commandstdin
: Provide input to the command; can be a string, list of strings, or newline-separated set of stringsstdin_add_newline
: Add a newline to the end of the STDIN datastdin_once
: Send all input data at once over STDINstrip_empty_ends
: Strip empty characters from the end of the command outputumask
: Sets the file-modes for any files created by the commandenv
: Dictionary of environment variables to set before running the commandtimeout
: How long to wait for the command to complete before timing outuse_unsafe_shell
: Uses the /bin/sh
shell for running the command, which can be a security risk.Here is an example Ansible playbook that uses the Ansible Shell module to run a command on multiple remote hosts:
- name: Copy files
hosts: all
become: yes
tasks:
- name: Create directory for files
file:
path: /tmp/myfiles
state: directory
register: dir_created
- name: Download files
shell: wget -P /tmp/myfiles http://example.com/file1.txt http://example.com/file2.txt
args:
chdir: /tmp/myfiles
register: download_output
when: dir_created.changed
- name: Display download output
debug:
var: download_output.stdout_lines
In this example, the playbook creates a directory for files on all the remote machines, downloads the files using wget, and then displays the output of the download command.
The Ansible Shell module is a powerful tool that enables system administrators and developers to execute shell commands on remote machines. With its ability to set environment variables and execute commands with specific user and group permissions, Ansible Shell can be used to automate complex tasks and processes on remote machines.