我想编写剧本来安装我的开发环境。大多数步骤都顺利,但我很难通过 安装 Ruby asdf
。
剧本被拆分并从中调用main.yaml
。安装asdf
工作正常:
---
- hosts: localhost
connection: local
gather_facts: yes
tasks:
- name: Detects previous installations of asdf
command: asdf version > /dev/null
register: asdf_check
ignore_errors: true
- name: Download and install asdf
block:
- name: Clone asdf repository into ~/.asdf
ansible.builtin.git:
name: https://github.com/asdf-vm/asdf.git
dest: ~/.asdf
single_branch: yes
version: v0.14.0
- name: Insert lines to ~/.bashrc to source asdf
ansible.builtin.blockinfile:
path: "/home/{{ unix_username }}/.bashrc"
block: |
. "$HOME/.asdf/asdf.sh"
. "$HOME/.asdf/completions/asdf.bash"
# Note for superuser post:
# I'm not sure to keep this, I added it to try a specific approach described below
- name: Set execution permissions to asdf.sh (reset later on)
ansible.builtin.file:
path: "/home/{{ unix_username }}/.asdf/asdf.sh"
mode: "744"
owner: "{{ unix_username }}"
group: "{{ unix_username }}"
when: asdf_check.rc > 0
但是我尝试的所有方法都无法成功安装 Ruby 最新版本。我试过:
- 先寻找资源
~/.bashrc
再致电asdf
- 使用
asdf
绝对路径 - 使用
bash -lc
- 使用
ansible.builtin.command
- 使用
ansible.builtin.shell
使用ansible.builtin.command
和绝对路径bash -lc
:asdf
任务标记为“已更改”,但未安装任何内容
tasks:
- name: Install asdf ruby plugin
ansible.builtin.command: "bash -lc '/home/{{ unix_username }}/.asdf/asdf.sh plugin add ruby https://github.com/asdf-vm/asdf-ruby.git'"
# Other tasks install Ruby latest and register it globally using the same approach
使用ansible.builtin.command
、bash -lc
和source .bashrc
:rc127,asdf: command not found
tasks:
- name: Install asdf ruby plugin
ansible.builtin.command: "bash -lc 'source /home/{{ unix_username }}/.bashrc && asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git'"
# Other tasks install Ruby latest and register it globally using the same approach
使用ansible.builtin.shell
绝对asdf
路径:rc127,asdf: command not found
tasks:
- name: Install asdf ruby plugin
ansible.builtin.shell:
executable: /bin/bash
cmd: "/home/{{ unix_username }}/.asdf/asdf.sh plugin add ruby https://github.com/asdf-vm/asdf-ruby.git"
# Other tasks install Ruby latest and register it globally using the same approach
使用ansible.builtin.shell
和source .bashrc
:rc127,asdf: command not found
tasks:
- name: Install asdf ruby plugin
ansible.builtin.shell:
executable: /bin/bash
cmd: "source /home/{{ unix_username }}/.bashrc && asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git"
# Other tasks install Ruby latest and register it globally using the same approach
除了找到解决该任务的方案之外,你能解释一下为什么所有这些方法都失败了吗?