Ansible 无法创建临时目录

Ansible 无法创建临时目录

我正在尝试使用 jumpbox 到达 ansible 上的目标 VM。我有以下文件结构

play.yaml
hosts
  serverA
  serverB
  testServers/
    serverX
    serverY
    group_vars/
      all.yml
           

server[X,Y]主机文件包含

[serverType]
1.2.3.4

all.ymlhosts/testServers/包含下的文件

ansible_ssh_private_key_file: key.priv
proxy_user: myServiceAccount
proxy_user_key: key.priv
ansible_ssh_common_args: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand="ssh -i {{ proxy_user_key }} -W %h:%p -q {{ proxy_user }}@{{ bastion_host }} 

我有一个 ping 模块play.yaml

- hosts: all  
  gather_facts: true
  tasks:
  - action: ping

我运行以下命令

ansible-playbook -vvvv play.yaml -i hosts/testServers/serverX 
  -e ansible_ssh_user="$LDAP_USER" -e ansible_ssh_pass="$LDAP_PASS" 
  -e ansible_become_pass="$LDAP_PASS"
  -e bastion_host="$BASTION_NAME"

我收到以下错误

Failed to create temporary directory.In some cases, you may have
been able to authenticate and did not have permissions on the
target directory. Consider changing the remote tmp path in
ansible.cfg to a path rooted in \"/tmp\", for more error
information use -vvv. Failed command was: ( umask 77 && mkdir -p
\"` echo /tmp `\"&& mkdir
/tmp/ansible-tmp-1626262883.727861-210-80403864334304 && echo
ansible-tmp-1626262883.727861-210-80403864334304=\"` echo
/tmp/ansible-tmp-1626262883.727861-210-80403864334304 `\" ),
exited with result 6

考虑

答案1

这可能是由于配置错误造成的。使用 Ansible 时(无所谓,Linux 或 WSL 或 Mobaxterm 控制器)应始终检查:

  1. /etc/hosts - 包含正确的行12.34.56.78 inventory_host。首先检查这一点。
  2. ansible_ssh_private_key_file - 指向正确的 ssh 私钥(.key,不是 putty 格式)
  3. .ssh 文件夹中的文件 - 具有适当的权限(chmod -R 600)

用于调试运行ansible -i inventory_file -m ping inventory_host -u ssh_user -vvv-vvvv。通常它会说明问题是什么。

对于那些在开始使用 Ansible 时遇到问题的人,这里有很好的 ansible 设置(开始工作的 3 个步骤):

  1. 撰写ansible.cfg位于项目文件夹中
[defaults]
inventory = ./inventory
interpreter_python = auto_silent
verbosity = 1

# first disable 'requiretty' in /etc/sudoers
ANSIBLE_PIPELINING = True

retry_files_enabled = False
deprecation_warnings = False
system_warnings = True
command_warnings = False
error_on_undefined_vars = True
display_args_to_stdout = True
display_skipped_hosts = True
gather_subset = !hardware,!facter,!ohai

# logger
log_path = ansible_log.rb

[ssh_connection]
ssh_args = -C -o ControlMaster=no -o ControlPersist=60s -o ConnectTimeout=10
retries = 1
usetty = True
sftp_batch_mode = True
transfer_method = sftp

[diff]
always = True

这些选项允许跳过库存并默认添加一些有用的选项(抑制警告、更详细的输出和差异)。

对于基于 mobaxterm 的 ansible 控制器,用户可能需要执行以下操作:

sed -i '/Ciphers/s/^/#/g' /etc/ssh_config
sed -i '/MACs/s/^/#/g' /etc/ssh_config

因为发行版中的线路不正确。

  1. 存货
[all:vars]
# common variables

ansible_user=ssh_user
ansible_ssh_private_key_file=/home/{{ ansible_user }}/.ssh/id_rsa

[group1:vars]
# variables for group1 servers

[group2:vars]
# variables for group2 servers

[group1]
host[1:2]

[group2]
host[3:4]

[prod:children]
group1
group2

这意味着您的环境有 2 组 4 台服务器,它们都被称为prod。您可以按组或一次对它们进行 ping 操作。

对于 Windows SSH 远程服务器,添加到清单:

ansible_shell_type=powershell
shell_type=powershell

或使用-e ansible_shell_type=powershell

连接之前不要忘记powershell在远程设置默认 shell(pwsh 示例):

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\PowerShell\7\pwsh.exe" -PropertyType String -Force

或以上:

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
  1. /etc/hosts
ipadress1 host1
ipadress2 host2
ipadress3 host3
ipadress4 host4

然后你可以检查:

ansible -m ping host1
ansible -m setup prod

答案2

我运行后ansible -m ping [hostname] -vvv,得到了额外的详细输出,但“-vvv”标志显示 ansible 用户的默认密码已过期,需要更改才能成功建立 ssh 连接。只需登录远程主机并更改密码(passwd [user])即可。

相关内容