我能够 ping 通实例,但是当我运行 ansible playbook 时,实例上失败了

问题描述 投票:0回答:2
suchetla@xhdlc210320:/scratch$ ansible all -m ping -i /opt/hosts
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
xhdlc201168 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
xhdlc201164 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
xhdlc201166 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

这里运行没有任何错误

suchetla@xhdlc210320:/scratch$ ansible-playbook -i /opt/hosts mars.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
Sudo password:

PLAY [xhd-hosts] ***************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
fatal: [xhdlc210319]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"ansible.legacy.setup": {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "failed": true, "module_stderr": "Shared connection to xhdlc210319 closed.\r\n", "module_stdout": "\r\n/usr/bin/python3: can't open file '/home/suchetla/.ansible/tmp/ansible-tmp-1711451084.9141061-329683-269580434256761/AnsiballZ_setup.py': [Errno 13] Permission denied\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 2}}, "msg": "The following modules failed to execute: ansible.legacy.setup\n"}

下面是playbook.yml

---
- hosts: xhd-hosts
  become: true
  become_method: sudo
  become_exe: "/tools/xgs/bin/sudo"
  #become_exe: "sudo"

  vars_prompt:
    - name: "ansible_sudo_pass"
      prompt: "Sudo password"
      private: yes

  tasks:
    - name: Install Prometheus Node Exporter
      apt:
        name: device-tree-compiler
        state: present

我的库存位于 /opt/hosts 主机名1 主机名2 主机名3 我在 ansible 安装实例上创建了一个公钥,并将其复制到目标服务器上的授权密钥

ansible-2.x ansible-inventory
2个回答
0
投票

根据您的日志,您的问题与网络无关,而是与您的文件系统权限有关!

这一行:

/usr/bin/python3: can't open file '/home/suchetla/.ansible/tmp/ansible-tmp-1711451084.9141061-329683-269580434256761/AnsiballZ_setup.py': [Errno 13] Permission denied

请检查路径并查看是否存在任何特定权限问题,或使用

ls -alh

更新您的问题

0
投票

尽管 ping 操作期间没有出现错误,但在尝试在主机 xhdlc210319 上收集事实(ansible.legacy.setup)时,playbook 执行遇到了权限问题。

错误消息表明 Ansible 由于权限被拒绝错误而无法打开文件 AnsiballZ_setup.py:

/usr/bin/python3: can't open file '/home/suchetla/.ansible/tmp/ansible-tmp-1711451084.9141061-329683-269580434256761/AnsiballZ_setup.py': [Errno 13] Permission denied

您可以采取以下步骤来解决此问题:

检查权限:确保 Ansible 用户 (suchetla) 具有适当的权限来访问和执行目标主机 (xhdlc210319) 上 /home/suchetla/.ansible/tmp/ 目录中的文件。您可能需要调整此目录及其内容的权限或所有权。

验证 SELinux/AppArmor 策略:如果目标主机上启用了 SELinux 或 AppArmor,则可能会阻止 Ansible 访问某些文件或目录。检查策略并在必要时进行调整,以使 Ansible 正确运行。

临时解决方法: 作为临时解决方法,您可以尝试在没有权限升级的情况下运行 playbook(删除变为:true 和相关参数),看看它是否可以在没有 sudo 的情况下工作。这可以帮助确定问题是否与 sudo 配置有关。

调试: 以更详细的方式运行 playbook (ansible-playbook -i /opt/hosts mars.yml -vvv) 以获取更详细的输出。这可以帮助确定问题发生的位置并为故障排除提供更多线索。

通过解决权限问题并确保 Ansible 用户在远程主机上拥有必要的权限,您应该能够解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.