📜  Ansible Shell(1)

📅  最后修改于: 2023-12-03 14:39:15.090000             🧑  作者: Mango

Ansible Shell

Introduction

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.

Features

Here are some of the features of Ansible Shell:

  • Execute shell commands on remote machines
  • Redirect command output to a file or set of files
  • Retrieve output from previous commands in the same playbook
  • Set environment variables for shell commands
  • Execute command with specific user and group permissions
  • Execute command and ignore errors
  • Execute commands with a timeout
Syntax

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 execute
  • warn: Boolean value that specifies whether to ignore non-zero return codes
  • executable: Command to run the script, using the /bin/sh shell by default
  • chdir: Directory to change into and run the command
  • creates: Path to a file that should not exist, and that will be created before running the command
  • removes: Path to a file that should exist, and that will be removed after running the command
  • stdin: Provide input to the command; can be a string, list of strings, or newline-separated set of strings
  • stdin_add_newline: Add a newline to the end of the STDIN data
  • stdin_once: Send all input data at once over STDIN
  • strip_empty_ends: Strip empty characters from the end of the command output
  • umask: Sets the file-modes for any files created by the command
  • env: Dictionary of environment variables to set before running the command
  • timeout: How long to wait for the command to complete before timing out
  • use_unsafe_shell: Uses the /bin/sh shell for running the command, which can be a security risk.
Example

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.

Conclusion

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.