Python 2.7 如何在使用ansible python模块运行时更改ansible的主机值?

Python 2.7 如何在使用ansible python模块运行时更改ansible的主机值?,python-2.7,ansible,ansible-inventory,Python 2.7,Ansible,Ansible Inventory,这是我的代码,我在这里传递主机的值,但它在本地主机上执行命令。我还尝试传递硬编码的值,在输出中显示它在给定主机上运行。 代码如下:- 类定义,然后 def __init__(self): self.variable_manager = VariableManager() self.loader = DataLoader() self.inventory = Inventory(loader=self.loader, variable_manager=self.variab

这是我的代码,我在这里传递主机的值,但它在本地主机上执行命令。我还尝试传递硬编码的值,在输出中显示它在给定主机上运行。 代码如下:-

类定义,然后

def __init__(self):
    self.variable_manager = VariableManager()
    self.loader = DataLoader()
    self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list="host")
    Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
    self.options = Options(listtags=False, listtasks=False, listhosts=True, syntax=False, connection='local', module_path=None, forks=100, remote_user='ubuntu', private_key_file="/tmp/xxx-key2.pem", ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user='root', verbosity=None, check=False)

def execute_playbook(self, playbook, host, scriptname=None, command=None,
                     path=None, username=None, password=None, key=None):
    if not os.path.exists(playbook):
        print '[INFO] The playbook does not exist'
        sys.exit()

    script_path = None
    if scriptname is not None:
        script_path = os.getcwd() + '/' + scriptname
        if not os.path.exists(script_path):
            print '[INFO] The script does not exist'
            sys.exit()
    self.variable_manager.extra_vars = {'scriptname': script_path,
                        'host': host, 'command': command, 'path': path} # This can accomodate various other command line arguments.`
    passwords = {}
    if password is not None:
        self.loader.set_vault_password(password)
    play_source =  dict(
        name = "Ansible Play",
        hosts = host,
        gather_facts = 'no',
        tasks = [
            dict(action=dict(module='shell', args='sudo mkdir /tmp/test-ansible'), register='shell_out'),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
         ]
    )
    play = Play.load(play_source, self.variable_manager, self.loader)

    tqm = TaskQueueManager(
        inventory=self.inventory,
        variable_manager=self.variable_manager,
        loader=self.loader,
        options=self.options,
        passwords=passwords,
    )

    try:
        result = tqm.run(play)
    except Exception as e:
        print e, "Exception in Ansible tqm.run()"
输出为:-

PLAY [Ansible Play] *************************************************************************************************************************

TASK [command] ******************************************************************************************************************************
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo

changed: [110.110.112.139]

TASK [debug] ********************************************************************************************************************************
ok: [110.110.112.139] => {
"msg": ""
}

但它会在我的本地主机中创建目录,而不是在“110.110.112.139”上。

您将
connection='local'
设置为
\uuuu init\uuuu
中的选项
这意味着无论目标主机是什么,都要在本地主机上执行任务


不要设置它(保留默认设置)或使用
ssh
远程执行任务。

而不是play\u source=dict(name=“Ansible play”,hosts=host,gather\u facts='no',tasks=[dict(action=dict(module='shell',args='sudo mkdir/tmp/test Ansible'),register='shell\u out'),dict(action=dict(module='debug',args=dict(msg='{{shell_out.stdout}}'))]))如果我正在使用DataLoader()加载剧本,则从剧本文件(“剧本”)加载剧本,并将其传递给播放,但我得到了AttributeError:'NoneType'对象没有属性'upper'。
def __init__(self):
    self.variable_manager = VariableManager()
    self.loader = DataLoader()
    self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list="host")
    Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
    self.options = Options(listtags=False,
                           listtasks=False,
                           listhosts=True,
                           syntax=False,
                           **connection='ssh'**, 
                           module_path=None,
                           forks=100, remote_user='ubuntu', 
                           private_key_file="/tmp/xxx-key2.pem", 
                           ssh_common_args=None,
                           ssh_extra_args=None,
                           sftp_extra_args=None,
                           scp_extra_args=None,
                           become=False,
                           become_method=None,
                           become_user='root', verbosity=None,
                           check=False
                   )