Ansible Module command

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

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.

- name: A command example
  command: some-command arg1 arg2
  become: true
  become_user: root
  args:
    chdir: /somedir/

"become" and "become_user" are optional. Also see:

Ansible Privilege Escalation

Registering Variables as Results of Command Execution

This sets a variable:

- name: Read requested Java version
  command: "cat {{ansible_env.HOME}}/config.txt | grep \"^JAVA_VERSION\" | sed -e 's/.*=//'"
  register: java_version