在 Debian 上通过 Ansible 使用 ASDF 安装语言

在 Debian 上通过 Ansible 使用 ASDF 安装语言

我想编写剧本来安装我的开发环境。大多数步骤都顺利,但我很难通过 安装 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 -lcasdf任务标记为“已更改”,但未安装任何内容

  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.commandbash -lcsource .bashrcrc127,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.shellsource .bashrcrc127,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

除了找到解决该任务的方案之外,你能解释一下为什么所有这些方法都失败了吗?

相关内容