ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

【Ansible】raw モジュールについて

Ansible で raw モジュールを使用することがあったので、その時のメモをここに残します。

raw

このモジュールは ansible-core の一部であり、すべての Ansible インストールに含まれています。ほとんどの場合、 collections: キーワードを指定しなくても、短いモジュール名 raw を使用できます。

概要

  • モジュール サブシステムを経由せずSSH コマンドを実行
  • raw に与えられた引数は設定されたリモート シェルを通じて直接実行
  • 利用可能な場合は、標準出力、エラー出力、および戻りコードが返される

サンプル

- name: Bootstrap a host without python2 installed
  raw: dnf install -y python2 python2-dnf libselinux-python

- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
  raw: cat < /tmp/*txt
  args:
    executable: /bin/bash

- name: Safely use templated variables. Always use quote filter to avoid injection issues.
  raw: "{{ package_mgr|quote }} {{ pkg_flags|quote }} install {{ python|quote }}"

- name: List user accounts on a Windows system
  raw: Get-WmiObject -Class Win32_UserAccount