执行ansible playbook AttributeError:' str'对象没有属性' set_playbook_basedir'

时间:2015-07-29 01:27:39

标签: python amazon-web-services ansible aws-cli cloud-init

您好,

我有两个问题。

第一部分: 我试图从下载一个剧本并执行它的python脚本中执行一个剧本。我知道剧本有效,因为我已经对它进行了测试。但是,当我尝试使用python代码执行playbook时,我得到 no属性错误' set_playbook_basedir' 不太确定它来自何处。这是完整的错误输出。

[ec2-user@ip-172-30-199-190 scripts]$ sudo python /var/lib/cloud/instance/scripts/part-001
/usr/lib64/python2.7/dist-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
  _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)
/bin/bash: line 0: export: `=': not a valid identifier
/bin/bash: line 0: export: `3': not a valid identifier
~/vision_provis/storm.yml
Traceback (most recent call last):
  File "/var/lib/cloud/instance/scripts/part-001", line 71, in <module>
    fatal: destination path 'vision_provis' already exists and is not an empty directory.
check=True)
  File "/usr/local/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 169, in __init__
    self.inventory.set_playbook_basedir(self.basedir)
AttributeError: 'str' object has no attribute 'set_playbook_basedir'


#!/usr/bin/python
import ansible.runner
from ansible.playbook import PlayBook
from ansible import inventory
from ansible import callbacks
import json
import subprocess
import os
from ansible import utils
import time

def shell_command_execute(cmd):
    try:
        subprocess.Popen(['/bin/bash', '-c', cmd])
    except:
        print 'There seems to be an error'
repo = "https://github.com/zukeru/vision_provis.git"
playbook = "storm.yml"


echo_bash_profile = "export CLOUD_ENVIRONMENT=integration|export CLOUD_MONITOR_BUCKET=0|export CLOUD_APP=ES-test-storm-deploy-DEV--_yV_cyE-|export CLOUD_STACK=infra|export CLOUD_CLUSTER=0|export CLOUD_AUTO_SCALE_GROUP=0|export CLOUD_LAUNCH_CONFIG=0|export EC2_REGION=us-west-2|export CLOUD_DEV_PHASE=0|export CLOUD_REVISION=0|export CLOUD_DOMAIN=0|export SG_GROUP=ES-test-storm-deploy-DEV--qtv6a_Uj"

for commands in echo_bash_profile.split('|'):
    command_to_send = 'echo "' + commands + '" >> /home/ec2-user/.bash_profile'
    shell_command_execute(commands)
    shell_command_execute(command_to_send)

var_user_data = "export SHARDS = 3"

for commands in var_user_data.split('|'):
    echo_bash_profile_passed = 'echo "' + commands  + '" >> /home/ec2-user/.bash_profile'
    shell_command_execute(commands)
    shell_command_execute(echo_bash_profile_passed)

command_remove = 'rm -rf /home/ec2-user/'+repo
shell_command_execute(command_remove)

command = 'cd /home/ec2-user/; git clone ' + repo
shell_command_execute(command)

folder = repo.split('/')[4].replace('.git','')
full_path = '/home/ec2-user/' + folder + '/' + playbook

time.sleep(6)

# setting callbacks
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

print full_path
# creating the playbook instance to run, based on "test.yml" file
pb = PlayBook(playbook = full_path,
                               stats = stats,
                               callbacks = playbook_cb,
                               runner_callbacks = runner_cb,
                               inventory = "localhost",
                               check=True)
# running the playbook
pr = pb.run()

# print the summary of results for each host
#print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))

第二个问题是:

这个脚本实际上是作为base64编码的aws用户数据及其执行传递的,但它似乎并没有认识到它是python。这是cloud-init-output.log

Complete!
Cloud-init v. 0.7.6 running 'modules:final' at Wed, 29 Jul 2015 00:17:16 +0000. Up 103.79 seconds.
/var/lib/cloud/instance/scripts/part-001: line 3: import: command not found
/var/lib/cloud/instance/scripts/part-001: line 4: import: command not found
/var/lib/cloud/instance/scripts/part-001: line 5: import: command not found
/var/lib/cloud/instance/scripts/part-001: line 6: import: command not found
/var/lib/cloud/instance/scripts/part-001: line 7: import: command not found
/var/lib/cloud/instance/scripts/part-001: line 8: import: command not found
/var/lib/cloud/instance/scripts/part-001: line 10: syntax error near unexpected token `('
/var/lib/cloud/instance/scripts/part-001: line 10: `def shell_command_execute(cmd):'
Jul 29 00:17:16 cloud-init[1958]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [2]
Jul 29 00:17:16 cloud-init[1958]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Jul 29 00:17:16 cloud-init[1958]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Jul 29 00:17:16 cloud-init[1958]: templater.py[WARNING]: Cheetah not available as the default renderer for unknown template, reverting to the basic renderer.
Cloud-init v. 0.7.6 finished at Wed, 29 Jul 2015 00:17:16 +0000. Datasource DataSourceEc2.  Up 103.97 seconds

1 个答案:

答案 0 :(得分:0)

我无法重现您的问题,但我最好的猜测是:

1)您需要使用Inventory对象而不是字符串初始化您的Playbook:

from ansible.inventory import Inventory
pb = PlayBook(playbook = full_path,
              stats = stats,
              callbacks = playbook_cb,
              runner_callbacks = runner_cb,
              inventory = Inventory(["localhost"]),
              check=True)

2)你的python脚本需要以一个指示为前缀,以表明它应该由python执行。只需将以下shebang放在python脚本的顶部:

#! /usr/bin/env python

或者,您可以使用以下命令运行脚本:

python /var/lib/cloud/instance/scripts/part-001

而不只是:

/var/lib/cloud/instance/scripts/part-001