You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Rundeck执行Python脚本?权限问题求助

问题描述

尝试通过Rundeck本地执行Python脚本,设置的节点步骤命令为:

python3 /home/ansible/reboot_cctv.py

脚本权限配置为:

-rwxrwxrwx 1 ansible ansible 455 Feb 24 16:14 reboot_cctv.py

但运行作业时出现权限拒绝错误:

python3: can't open file '/home/ansible/reboot_cctv.py': [Errno 13] Permission denied
Result: 2
Failed: NonZeroResultCode: Result code was 2

完整作业定义如下:

- defaultTab: nodes
  description: ''
  executionEnabled: true
  id: fb24247a-0e30-4533-ad00-01ed4cbc00ff
  loglevel: INFO
  name: CCTV Restart Python
  nodeFilterEditable: false
  plugins:
    ExecutionLifecycle: null
  scheduleEnabled: true
  sequence:
    commands:
    - exec: python3 /home/ansible/reboot_cctv.py
    keepgoing: false
    strategy: node-first
  uuid: fb24247a-0e30-4533-ad00-01ed4cbc00ff

排查与解决步骤

  • 检查父目录权限
    脚本本身权限为777,但Rundeck运行用户需要具备/home/ansible目录的访问权限。执行以下命令查看目录权限:

    ls -ld /home/ansible
    

    确保目录对Rundeck用户(通常为rundeck)至少拥有执行权限(x)。若目录权限为drwx------,其他用户无法进入,可调整目录权限:

    chmod o+x /home/ansible
    

    或更精准地将Rundeck用户加入ansible用户组:

    usermod -aG ansible rundeck
    

    完成后重启Rundeck服务:

    systemctl restart rundeckd
    
  • 验证Rundeck运行用户的访问能力
    切换到Rundeck用户直接测试脚本执行:

    su - rundeck -c "python3 /home/ansible/reboot_cctv.py"
    

    若同样报错,说明是权限问题;若能正常执行,需检查Rundeck节点配置是否有特殊限制(如sudo规则、环境变量等)。

  • 检查SELinux限制(若启用)
    若系统开启SELinux,可能会阻止Rundeck访问目标路径。临时关闭SELinux测试:

    setenforce 0
    

    若脚本能正常执行,需添加SELinux规则允许访问:

    semanage fcontext -a -t bin_t "/home/ansible/reboot_cctv.py"
    restorecon -v /home/ansible/reboot_cctv.py
    
  • 调整作业执行方式
    若以上方法无效,可尝试用sudo执行脚本(需给rundeck用户配置免sudo权限):

    sudo -u ansible python3 /home/ansible/reboot_cctv.py
    

    或把脚本移动到Rundeck默认有权限访问的目录(如/var/lib/rundeck/scripts),修改作业命令为:

    python3 /var/lib/rundeck/scripts/reboot_cctv.py
    

内容的提问来源于stack exchange,提问作者cnrdvdsmt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 01:37:13