Ansible Module command: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 35: Line 35:
   command: "sh -c \"grep ^JAVA_VERSION {{ansible_env.HOME}}/config.txt | sed -e 's/.*=//'\""
   command: "sh -c \"grep ^JAVA_VERSION {{ansible_env.HOME}}/config.txt | sed -e 's/.*=//'\""
   register: java_version
   register: java_version
  changed_when: false
</syntaxhighlight>
<syntaxhighlight lang='yaml'>
- name: Get Homebrew installation directory
  command: "sh -c \"brew --prefix\""
  register: brew_prefix_cmd
   changed_when: false
   changed_when: false
</syntaxhighlight>
</syntaxhighlight>

Revision as of 06:35, 4 July 2021

External

Internal

Overview

The command module takes the command name followed by a list of space-delimited arguments. A command will NOT be interpreted by a shell, so variables like $HOME and operations like "<", ">", "|", ";" and "&" will not work. Use the shelll module for that.

For a discussion on the interaction between command, cmd, argv, etc. see command, argv and cmd below.

- name: A command example
  command: some-command arg1 arg2 # the command can be specified here, or with 'cmd', or with 'argv'
    argv: ... # Passes the command as a list rather than a string
    cmd: ... # The command to run
    chdir: ... # Change into this directory before running the command
    stdin: ... 
    stdin_add_newline: ...
    warn: ...
    creates: ...
  become: true
  become_user: root
  args:
    chdir: /somedir/ # alternative way of specifying "command" configuration
  register: variable_that_contains_results_of_execution

A variable that contains the result of the command execution can be set as usual for a task using the task's register configuration elements.

Examples

- name: Read requested Java version
  command: "sh -c \"grep ^JAVA_VERSION {{ansible_env.HOME}}/config.txt | sed -e 's/.*=//'\""
  register: java_version
  changed_when: false
- name: Get Homebrew installation directory
  command: "sh -c \"brew --prefix\""
  register: brew_prefix_cmd
  changed_when: false

command, argv and cmd

The command followed by the space-separated list of arguments can be specified immediately after the command: keyword:

- name: some name
  command: ls -l /tmp

Alternatively, the command provided as a list with argv. Note that either command or argv can be used, but not both at the same time:

- name: some name
  command:
    argv:
      - ls
      - -l
      - /tmp

The full command can be specified as a string with cmd. Note that either command or cmd can be used, but not both at the same time:

- name: some name
  command:
    cmd: ls -l /tmp

Privilege Escalation

See:

Ansible Privilege Escalation

Setting Command Output as a Fact

Set the command output as a fact (see set_fact):

- name: Get Homebrew installation directory
  command: "sh -c \"brew --prefix\""
  register: brew_prefix_cmd
  changed_when: false

- name: Set Homebrew installation directory
  set_fact:
    pkg_manager_install_dir: "{{ brew_prefix_cmd.stdout }}"