在 Bash 中我通常这样安装 Composer:
curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
我尝试阅读Ansible Composer 模块文档 只是为了一个提示关于使用 Ansible 安装 Composer 的推荐方法是什么,但我没有找到。
我只是想确保每次安装 Composer 时,除了 Ansible 通过 SSH 从我的机器上不断升级之外,它还会通过最新的安装命令进行安装。
我怎样才能以 Ansible“ state: latest
” 的方式做到这一点(与 Ansible 的模块一样apt
)?
答案1
答案2
这是另一种方法,这里解释了一切:https://www.devopsaurus.com/install-composer-with-ansible/
---
- name: Validate Composer checksum
get_url:
checksum: "sha384:795f976fe0ebd8b75f26a6dd68f78fd3453ce79f32ecb33e7fd087d39bfeb978342fb73ac986cd4f54edd0dc902601dc"
dest: /usr/src/
url: https://getcomposer.org/installer
become: yes
- name: Download and install Composer
shell: curl -sS https://getcomposer.org/installer | php
args:
chdir: /usr/src/
creates: /usr/local/bin/composer
warn: false
become: yes
- name: Add Composer to global path
copy:
dest: /usr/local/bin/composer
group: root
mode: '0755'
owner: root
src: /usr/src/composer.phar
remote_src: yes
become: yes
答案3
用于
get_url
输出https://getcomposer.org/installer
到文件composer-setup.php
,最好是在某个目录中,比方说/tmp
使用
command
模块运行php /tmp/composer-setup.php ...
步骤
或者搜索 Ansible Galaxy,看看是否有其他人做过。
答案4
读取校验和,将其存储在变量中,并将 PHAR 文件下载到目录中/usr/local/bin
。校验和任务是可选的。但是,需要 PHP 才能读取该版本。
---
- name: "Read Checksum"
shell: curl https://getcomposer.org/download/latest-stable/composer.phar.sha256
register: composer_checksum
- name: "Download PHAR file"
ansible.builtin.get_url:
url: https://getcomposer.org/download/latest-stable/composer.phar
dest: /usr/local/bin/composer
force: yes
checksum: "sha256:{{ composer_checksum.stdout }}"
- name: "Read composer version"
shell: composer -V
register: composer_version
- debug:
var: composer_version.stdout
预期输出
ok: [127.0.0.1] => { "composer_version.stdout": "Composer 版本 2.1.9 2021-10-05 09:47:38" }