我有一个剧本可以在远程节点上执行一些任务。要求是我必须作为一个用户(例如 foo-user)执行其中一些任务,并作为另一用户(例如 bar-user)执行其他任务。因此,我在库存主机文件中为同一远程节点 IP 定义了两个不同的条目(node-foo 和 node-bar),然后在剧本级别定义了hosts:node-foo。对于这些任务,我想以 bar 用户身份执行,我使用了 delegate_to: node-bar 选项。
在剧本执行期间,第一个任务 delegate_to 失败。据我所知,它无法访问由第一个用户(foo-user)作为远程节点上的第二个用户(bar-user)创建的远程 tmp 目录,因此任务执行失败。
有没有办法在不给远程tmp目录777权限的情况下解决这个问题?
使用 remote_user 而不是 delegate_to。例如,
- hosts: test_01
tasks:
- command: whoami
register: out
remote_user: alice
- debug:
var: out.stdout
- command: whoami
register: out
remote_user: bob
- debug:
var: out.stdout
给出(删节)
out.stdout: alice
out.stdout: bob
默认情况下,remote_user创建远程临时目录$HOME/.ansible
shell> ssh admin@test_01 sudo ls -la /home/alice/ | grep ansible
drwx------ 3 alice alice 3 Nov 15 06:19 .ansible
shell> ssh admin@test_01 sudo ls -la /home/bob/ | grep ansible
drwx------ 3 bob bob 3 Nov 15 06:19 .ansible