gpt4 book ai didi

linux - Ansible权限问题

转载 作者:IT王子 更新时间:2023-10-29 00:20:03 30 4
gpt4 key购买 nike

我正在尝试将当前用户添加到系统中的一个组,然后执行一个需要该组权限的命令。我的剧本是这样的:

- name: Add this user to RVM group
sudo: true
user: state=present name=vagrant append=yes groups=rvm group=rvm
- name: Install Ruby 1.9.3
command: rvm install ruby-1.9.3-p448 creates=/usr/local/rvm/bin/ruby-1.9.3-p448

问题是所有这些都发生在同一个 shell 中。 vagrant 的 shell 还没有用新的组更新。是否有一种干净的方法来刷新用户在 Ansible 中的当前组?我想我需要让它重新连接或打开一个新的 shell 。

但是我尝试打开一个新的 shell,它只是挂起:

- name: Open a new shell for the new groups
shell: bash

当然挂了:进程永远不会退出!

与 newgrp 相同

- name: Refresh the groups
shell: newgrp

因为它基本上做同样的事情。

有什么想法吗?

最佳答案

阅读手册。

此处的解决方案是为“命令”或“shell ”模块使用“可执行”参数。

所以我尝试像这样使用命令模块:

- name: install ruby 1.9.3
command: rvm install ruby-1.9.3-p448 executable=/bin/bash creates=/usr/local/rvm/bin/ruby-1.9.3-p448
ignore_error: true

但是剧本无限期挂起。 manual状态:

If you want to run a command through the shell (say you are using <, >, |, etc), you actually want the shell module instead. The command module is much more secure as it's not affected by the user's environment.

所以我尝试使用 shell 模块:

- name: install ruby 1.9.3
shell: rvm install ruby-1.9.3-p448 executable=/bin/bash creates=/usr/local/rvm/bin/ruby-1.9.3-p448
ignore_error: true

而且有效!

关于linux - Ansible权限问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19867298/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com