【问题标题】:remote tmp directory not set for ansible script execution未为 ansible 脚本执行设置远程 tmp 目录
【发布时间】:2017-02-02 20:12:42
【问题描述】:

我需要在远程服务器上安装二进制文件。以下是我正在执行的任务列表。

  1. 复制/scp 二进制文件到远程服务器
  2. 以静默模式运行安装程序

步骤#1 是将二进制文件复制到/tmp,在远程主机上/tmp 的空间非常少,一旦/tmp 已满,scp 就会失败。我知道默认情况下,ansible 脚本/文件将被复制到/tmp 目录,一旦活动完成,它将被删除。由于/tmp 非常低,我需要使用用户目录来复制二进制文件。

下面是ansible.cfg

remote_user = testaccount
host_key_checking = False
scp_if_ssh=True
remote_tmp = $HOME/.ansible/tmp

下面是剧本:

- name: deploy binaries
  hosts: test
  strategy: free
  become_method: 'sudo'
  tasks:
    - name: transfer
      copy: src=./files/weblogic.jar dest=$HOME mode=0777
      register: transfer
    - debug: var=transfer.stdout

剧本执行:

ansible-playbook --inventory="hosts" --ask-pass --ask-become-pass --extra-vars="ansible_ssh_user=<unixaccount>" copybinaries.yml

即使使用上述配置,二进制文件也不会复制到用户主目录,我确保有$HOME/.ansible/tmp 目录,甚至像/home/testaccount/.ansbile/tmp 这样的硬编码。

ansible.cfg 中是否需要覆盖任何其他配置?

【问题讨论】:

  • 我仍然看不到您实际在哪里使用become,也看不到您成为了哪个用户。除此之外,remote_user = testaccountansible_ssh_user=&lt;unixaccount&gt; 可能是矛盾的。至少他们在问题上令人困惑。

标签: ansible


【解决方案1】:

虽然你还没有包含一个连贯的MCVE,我猜:

  • 您正在使用成为非特权用户选项运行任务;

  • 或者您的清单文件、配置文件、执行调用和 playbook 包含不必要和矛盾的设置,使 Ansible 以或多或少不确定的方式运行。


Ansible 将系统临时目录用于以不同用户身份运行的任务。 This line 确定了这一点。

您只能为此类任务指定/tmp(默认)或/var/tmp 的子目录(使用remote_tmp)。请参阅 Ansible 代码中的 the comment

# When system is specified we have to create this in a directory where
# other users can read and access the temp directory.  This is because
# we use system to create tmp dirs for unprivileged users who are
# sudo'ing to a second unprivileged user.  The only dirctories where
# that is standard are the tmp dirs, /tmp and /var/tmp.  So we only
# allow one of those two locations if system=True.  However, users
# might want to have some say over which of /tmp or /var/tmp is used
# (because /tmp may be a tmpfs and want to conserve RAM or persist the
# tmp files beyond a reboot.  So we check if the user set REMOTE_TMP
# to somewhere in or below /var/tmp and if so use /var/tmp.  If
# anything else we use /tmp (because /tmp is specified by POSIX nad
# /var/tmp is not).

【讨论】:

  • 是的。我们需要使用 sudo 帐户运行 playbook。我们还能使用 sudo login 自定义 tmp 目录来运行 playbook 吗?
  • 我更新了答案。只能选择/var/tmp或者修改代码。
  • 非常感谢。我在 ansible.cfg 中将 tmp 设置为“var/tmp”。现在可以传输文件了。
猜你喜欢
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-31
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多