晴耕雨読

working in the fields on fine days and reading books on rainy days

Ansibleで接続先にpythonが存在しないとき

Ansibleで接続先にpythonが存在しないときや、pythonコマンドが実行できない時は、Ansibleのrawモジュールを使用して、直接シェルコマンドを実行することで解決できます。

通常、Ansibleを実行する際に接続先にPythonが存在しないとエラーになります。

fatal: [xxx.xxx.xxx.xxx]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Shared connection to xxx.xxx.xxx.xxx closed.\r\n",
    "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n",
    "msg": "MODULE FAILURE",
    "rc": 0
}

接続先でPythonが使えない場合は、Ansibleのrawモジュールを使用します。 使い方は通常の command や shell モジュールと同じように使えます。

---
- hosts: all
  vars:
    user: tex2e

  tasks:
    - name: Add user
      ansible.builtin.raw: useradd -m {{user}}

    - name: Create file
      ansible.builtin.raw: |-
        echo "Hello, world" >> /tmp/test.log

以上です。

参考文献