使用 sudo 时的 Ansible 包警告

Ansible package warning when using sudo

我正在按照这个 doc 在 RedHat 7.3 中安装 shiny 包。文档中提供的命令是:

$ sudo su - \
-c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\""

在Ansible中,我是这样写的:

- name: Installing Shiny Packages
    shell: sudo su - -c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\""
   #when: install_R|changed

我在 运行 我的剧本时收到警告:

TASK [Installing Shiny Packages] ***********************************************
 [WARNING]: Consider using 'become', 'become_method', and 'become_user' rather
than running sudo

changed: [test]

请告诉我如何在ansible中写这个,这样我就可以避免警告。

这可能是因为版本 1.9 中的 sudo 用法已过时。来自官方 Ansible 文档。

Before 1.9 Ansible mostly allowed the use of sudo and a limited use of su to allow a login/remote user to become a different user and execute tasks, create resources with the 2nd user’s permissions. As of 1.9, become supersedes the old sudo/su, while still being backwards compatible.

您可以使用 become module 删除它,它允许您 'become' 另一个用户,不同于登录机器的用户(远程用户)。您需要设置为 true 以激活权限升级。

name: Installing Shiny Packages
   shell: R -e "install.packages('shiny', repos='https://cran.rstudio.com/')"
become: true