Ansible Module command: Difference between revisions
Jump to navigation
Jump to search
Line 33: | Line 33: | ||
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> | |||
Set the command output as a fact: | |||
<syntaxhighlight lang='yaml'> | |||
- 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 }}" | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 21:57, 19 January 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.
- 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:
Registering Variables as Results of Command Execution
This sets a variable:
- 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
Set the command output as a 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 }}"