问题

问题

问题

我想使用 ansible 来配置一个虚拟盒。这个盒子需要做很多事情,但它需要安装 R 以及基本 R 中未包含的一些特定软件包。

我使用下面提供的 vagrant 文件创建一个新的虚拟机,然后使用 yum 安装 ansible、epel-release 和 git。然后,在该虚拟机上,我使用下面提供的“Alma Linux 8”和“Docker”剧本,然后使用我认为有问题的剧本。

然后我运行这个剧本来安装 R

---
- name: "Setup R on the local machine"
  hosts: localhost
  connection: local 
  tasks:

  # Update all installed packages

  - name: Update all packages to their latest version
    become: yes
    ansible.builtin.package:
      name: "*"
      state: latest

  # Install EPEL to get extra packages

  - name: Install EPEL
    become: yes
    ansible.builtin.package:
      name: epel-release
      state: latest

  # Enable the Code Ready Builder (CRB) repository

  - name: Install utilities to enable package repositories
    become: yes
    ansible.builtin.package:
      name: yum-utils
      state: latest

  - name: Enable the PowerTools repository
    become: yes
    shell: dnf config-manager --set-enabled powertools

  # Install R

  - name: Install R
    become: yes
    ansible.builtin.package:
      name: R
      state: latest

运行此剧本后,我可以运行命令

Rscript --slave --no-save --no-restore-history -e "print('Test')"
[1] "Test"

表明 R 至少在某种程度上发挥着作用。但是当我尝试使用它来安装软件包时,我收到此错误

Rscript --slave --no-save --no-restore-history -e "install.packages('tidyverse')"

将软件包安装到“/usr/lib64/R/library”(因为未指定“lib”) install.packages(“tidyverse”) 中出现警告:“lib =“/usr/lib64/R/library””不可写错误在 install.packages("tidyverse") 中:无法安装包执行停止

尝试修复

我尝试通过将这些行附加到上面 R 剧本的末尾来解决此问题

- name: Make directory writable
  become: yes
  file:
    path: /usr/lib64/R/library
    mode: '0777'

- name: Make directory writable
  become: yes
  file:
    path: /usr/share/doc/R/html
    mode: '0777'

手动使有问题的目录可读,但这不起作用。

参考文件

流浪文件

Vagrant.configure("2") do |config|

    config.vm.box = "almalinux/8"

    config.vm.provider "virtualbox" do |vb|
        vb.memory = "2048"
        vb.cpus = 2
    end
    
end

Ansible 手册

阿尔玛 Linux 8

---
- name: "Setup AlmaLinux 8 on the local machine"
  hosts: localhost
  connection: local
  tasks:

  # Install the base operating systemctl

  - name: Update all packages to their latest versions
    become: yes
    ansible.builtin.package:
      name: '*'
      state: latest

  - name: Install virtual machine support
    become: yes
    ansible.builtin.package:
      name: open-vm-tools
      state: latest

  # Install EPEL to get extra packages

  - name: Install EPEL
    become: yes
    ansible.builtin.package:
      name: epel-release
      state: latest

码头工人

---
- name: "Setup Docker on the local machine"
  hosts: localhost
  connection: local 
  tasks:

  # Install Docker

  - name: Uninstall any older versions of Docker
    become: yes
    ansible.builtin.package:
      name:
        - docker
        - docker-client
        - docker-client-latest
        - docker-common
        - docker-latest
        - docker-latest-logrotate
        - docker-logrotate
        - docker-engine
      state: absent

  - name: Install yum-utils to install a new package repository
    become: yes
    ansible.builtin.package:
      name: yum-utils
      state: latest

  - name: Install the Docker Repository
    become: yes
    shell: yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

  - name: Install Docker
    become: yes
    ansible.builtin.package:
      name:
        - docker-ce
        - docker-ce-cli
        - containerd.io
        - docker-buildx-plugin
        - docker-compose-plugin
      state: latest

  - name: Enable the docker service
    become: yes
    shell: systemctl enable docker

  - name: Enable the containerd service
    become: yes
    shell: systemctl enable containerd

  - name: Start the docker service
    become: yes
    shell: systemctl start docker

  - name: Start the containerd service
    become: yes
    shell: systemctl start containerd

  - name: Ensure docker group exists
    become: yes
    ansible.builtin.group:
      name: docker
      state: present

  - name: Add the vagrant user to the docker group
    become: yes
    ansible.builtin.user:
      name: vagrant
      groups: docker
      append: yes

答案1

但是当我尝试使用它来安装软件包时

您以普通用户身份执行此操作,但尝试安装到系统目录中,这就是问题所在。

您想在系统范围内安装吗?然后以 root 身份执行此操作。您只需要该一位用户的软件包吗?然后不要在系统范围内安装。

相关内容