【问题标题】:Ansible through Apache: Authentication or permission failureAnsible 通过 Apache:身份验证或权限失败
【发布时间】:2015-01-11 13:56:40
【问题描述】:

我尝试通过 Apache2 CGI 运行 Ansible。

这是我的脚本:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-# enable debugging
print "Content-type: text/html"
print

import cgitb
cgitb.enable()
print "<html><head>"
print ""
print "</head><body>"

import os
import ansible.runner
import sys
results = ansible.runner.Runner(
#    transport='ssh',
#    remote_user='root',
#    remote_pass='pass',
    transport='local',
#    su='yes',
#    su_user='root',
#    su_pass='su_pass',
    sudo='yes',
    sudo_user='root',
    sudo_pass='sudo_pass',
    pattern='127.0.0.1',
    module_name='service',
    module_args='name=shoc-lxc-net state=started enabled=yes',
    forks=10,
).run()

if results is None:
   print "No hosts found"
   sys.exit(1)

import json
print(json.dumps(results,
                 default=lambda obj: vars(obj),
                 indent=1))

for (hostname, result) in results['contacted'].items():
    if not 'failed' in result:
        print "%s >>> %s" % (hostname, result['enabled'])

print "</body></html>"

当我运行它时,我得到:

{ "dark": { "127.0.0.1": { "msg": "认证或权限 失败。在某些情况下,您可能已经能够进行身份验证并且确实 没有远程目录的权限。考虑改变 ansible.cfg 中的远程临时路径指向以 \"/tmp\" 为根的路径。失败的 命令是:mkdir -p $HOME/.ansible/tmp/ansible-1420984254.94-125323999727459 && chmod a+rx $HOME/.ansible/tmp/ansible-1420984254.94-125323999727459 && echo $HOME/.ansible/tmp/ansible-1420984254.94-125323999727459,退出 结果 1”、“失败”:真 } }、“已联系”:{} }

但是当我通过 shell 运行这个脚本时,我得到:

$ /lxc/www/py/index.py 
Content-type: text/html

<html><head> </head><body> {  "dark": {},  "contacted": {  
"127.0.0.1": {    "invocation": {
    "module_name": "service",
    "module_args": "name=shoc-lxc-net state=started enabled=yes"    },    "state": "started",    "changed": false,    "enabled": true,   
"name": "shoc-lxc-net"   }  } }
127.0.0.1 >>> True

当我检查时:

# ls -la /var/www/
total 20
drwxrwxrwx  4 root     root     4096 Jan 11 15:57 .
drwxr-xr-x 14 root     root     4096 Jan  8 15:58 ..
drwxr-xr-x  2 root     root     4096 Jan 11 14:54 etc
-rw-r--r--  1 root     root      177 Mar 18  2013 index.html
drwx------  2 www-data www-data 4096 Jan 11 15:36 .ssh
# su www-data
$ mkdir /var/www/.ansible/tmp -p
$
# ls -la /var/www/.ansible/
total 12
drwxrwxr-x 3 www-data www-data 4096 Jan 11 15:57 .
drwxrwxrwx 5 root     root     4096 Jan 11 15:57 ..
drwxrwxr-x 2 www-data www-data 4096 Jan 11 15:57 tmp

【问题讨论】:

    标签: python ansible


    【解决方案1】:

    脚本将以 apache 用户(或您正在运行 apache 的任何用户 - 在某些情况下可能是“nobody”或完全是其他用户)身份运行。该用户很可能没有与您相同的权限。您可能需要 setuid 脚本以具有适当权限的用户身份运行,或者向 /etc/sudoers 添加一个条目以允许脚本以其他用户身份运行而无需提示输入密码,然后使用包装器外壳脚本让 Apache 通过sudo 调用脚本。

    【讨论】:

    • 如果我将 temp dir 更改为 ansible.cfg(tmp = $HOME/.ansible/tmp ) 到 tmp=/tmp 它是有效的。但我不能在我的服务器上这样做
    【解决方案2】:

    我在 Molecule 中运行我的 Ansible 项目时遇到了同样的错误。这是我发现的:

    运行命令molecule test --destroy=never 时,我在 tmp 文件夹/文件中重现了同样的问题。如果我跳过destroy 标志而只运行molecule test - 它工作正常。

    在社区中发布此答案可能会遇到此问题。作为一种解决方法,您可以在分子使用verify 步骤完成之后,就在它完成执行之前Ctrl+C,即destroy

    分子步骤:

    └── 默认 ├── 皮棉 ├── 破坏 ├── 依赖 ├── 语法 ├── 创造 ├── 准备 ├── 收敛 ├── 幂等性 ├── 副作用 ├── 验证 └── 破坏

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2018-11-27
      • 2022-08-11
      • 2017-07-24
      • 1970-01-01
      相关资源
      最近更新 更多