YAMAGUCHI::weblog

海水パンツとゴーグルで、巨万の富を築きました。カリブの怪物、フリーアルバイター瞳です。

Ansibleでリモートのインストールスクリプトの実行をcurlを使わずに記述する

TL;DR

ansible.builtin.uri でコンテンツを取ってきて ansible.builtin.shellstdin を使って流し込む

サンプル

たとえばrustupのインストールは公式ドキュメントでは次のようなスクリプトを使っている

rustup.rs

curl -sSf https://sh.rustup.rs | sh -s -- -y

これをこのまま ansible.builtin.shell に記述して実行すると warning が出ます。たとえばこういう具合にすると

- name: Install rustup
  ansible.builtin.shell:
    cmd: curl -sSf https://sh.rustup.rs | sh -s -- -y

実行時にこういうふうにwarningがでます。

TASK [common : Run rustup] **********************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'curl'.  If you need to use command
because get_url or uri is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [dev]

指摘のように ansible.builtin.uriansible.builtin.shellstdin を使えばパイプの部分をそのまま表現できるのでそのように書き換えます。

- name: Fetch rustup
  ansible.builtin.uri:
    url: https://sh.rustup.rs
    return_content: yes
  register: rustup_installer

- name: Run rustup installer
  ansible.builtin.shell:
    cmd: sh -s -- -y
    stdin: "{{ rustup_installer.content }}"