Newbie Developer Question: how does TaskQueueManager work exactly please?

Hi,

I am trying to retrofit the pre2.0 runner based script to use the new TaskQueueManager interface and I tried to tweak the example given in the ansible API documentation and it simply will NOT work.

I have ssh keyless login already enabled on a list of hosts and my script is to run a ssh command, namely “ls” on these remote hosts and returns but I failed the attempt at the following:

#! /usr/bin/python

import os

import sys

import time

from socket import inet_aton

import jinja2

from tempfile import NamedTemporaryFile

from ansible.playbook import Playbook

from ansible import utils

from docopt import docopt

from collections import namedtuple

from ansible.parsing.dataloader import DataLoader

from ansible.vars import VariableManager

from ansible.inventory import Inventory

from ansible.playbook.play import Play

from ansible.executor.task_queue_manager import TaskQueueManager

import re

hosts_list = “adobe1,adobe2,adobe3”

create play with tasks

play_source = dict(

name = ‘command’,

hosts = hosts_list,

gather_facts = ‘no’,

tasks = [

dict(action=dict(module=‘shell’, args=‘ls’), register=‘shell_out’),

]

)

Options = namedtuple(‘Options’, [‘connection’, ‘module_path’, ‘forks’, ‘become’, ‘become_method’, ‘become_user’, ‘check’, ‘remote_user’])

#initialize needed objects

variable_manager = VariableManager()

loader = DataLoader()

options = Options(connection=‘local’, module_path=‘./’, forks=100, become=None, become_method=‘ssh’, become_user=‘root’, check=False, remote_user=‘root’)

passwords = dict(vault_pass=‘’)

inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hosts_list)

variable_manager.set_inventory(inventory)

play = Play().load(play_source, variable_manager=variable_manager, loader=loader)

actually run it

tqm = None

try:

tqm = TaskQueueManager(

inventory=inventory,

variable_manager=variable_manager,

loader=loader,

options=options,

passwords=passwords,

stdout_callback=‘default’,

)

result = tqm.run(play)

finally:

if tqm is not None:

tqm.cleanup()

sys.exit(0)

It complained when executing:

raceback (most recent call last):

File “./run_test.py”, line 60, in

result = tqm.run(play)

File “/Library/Python/2.7/site-packages/ansible/executor/task_queue_manager.py”, line 206, in run

play_context = PlayContext(new_play, self._options, self.passwords, self._connection_lockfile.fileno())

File “/Library/Python/2.7/site-packages/ansible/playbook/play_context.py”, line 212, in init

self.set_options(options)

File “/Library/Python/2.7/site-packages/ansible/playbook/play_context.py”, line 259, in set_options

self.private_key_file = options.private_key_file

AttributeError: ‘Options’ object has no attribute ‘private_key_file’

Can somebody please point me to the correct direction? This seems to be such a trivial task and yet I can’t tweak it into working.

Many thanks,

Yang

You are using a bit older version of Ansible than what the sample script implements, in initial 2.0 many other options were needed to be passed in, this was fixed for 2.0.1.

You can upgrade ansible or look at the docs version history for the ‘full list’ for the previous versions.

Hi,

Thanks for the reply.

Is there code snippet which shows how it’s done in the newer version please?
As I said, all I need to do is to perform some remote ssh commands on bunch of hosts.

Many thanks,

Yang

You really shouldn’t be running Ansible this way.

An example is documented here:

http://docs.ansible.com/ansible/developing_api.html#python-api-2-0