将值动态传递给Ansible清单 - Python

时间:2015-05-27 22:12:00

标签: python ansible

我尝试Ansible's Python API来编写测试API(在Python中),可以通过编程方式利用Playbook并将新节点添加到Hadoop群集。我们知道,至少群集中的节点必须是NamenodeJobTracker(MRv1)。为简单起见,我们可以说JobTrackerNamenode位于同一个节点(namenode_ip)。

因此,为了使用Ansible创建一个新节点,并将其自Namenode注册,我创建了以下Python实用程序:

from ansible.playbook import PlayBook                                                                                                                
from ansible.inventory import Inventory                                                                                                              
from ansible.inventory import Group                                                                                                                  
from ansible.inventory import Host                                                                                                                   
from ansible import constants as C                                                                                                                   
from ansible import callbacks                                                                                                                        
from ansible import utils                                                                                                                            

import os                                                                                                                                            
import logging                                                                                                                                       
import config 

def run_playbook(ipaddress, namenode_ip, playbook, keyfile):                                                                                                                                                                     
      utils.VERBOSITY = 0                                                                                                                              
      playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)                                                                               
      stats = callbacks.AggregateStats()                                                                                                               
      runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)                                                                    

      host = Host(name=ipaddress)                                                                                                                      
      group = Group(name="new-nodes")                                                                                                                  
      group.add_host(host)                                                                                                                             
      inventory = Inventory(host_list=[], vault_password="Hello123")                                                                              
      inventory.add_group(group)                                                                                                                       
      key_file = keyfile                                                                  
      playbook_path = os.path.join(config.ANSIBLE_PLAYBOOKS, playbook)                                                                                 
      pb = PlayBook(                                                                                                                                   
              playbook=playbook_path,                                                                                                                  
              inventory = inventory,                                                                                                                   
              remote_user='deploy',                                                                                                                    
              callbacks=playbook_cb,                                                                                                                   
              runner_callbacks=runner_cb,                                                                                                              
              stats=stats,                                                                                                                             
              private_key_file=key_file                                                                                                                
      )                                                                                                                                                

      results = pb.run()                                                                                                                               
      print results

然而,Ansible的{​​{1}} documentation编写得非常糟糕(除了一个简单的例子,它没有提供任何细节)。我需要的是有类似的东西:

Python API

即可,使用ansible-playbook -i hadoop_config -e "namenode_ip=1.2.3.4" namenode_ip的值动态传递到广告资源文件。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

这应该就像在实例化组对象之后和运行playbook之前向脚本中添加一行或多行一样简单:

SELECT 
product_image.image 
FROM product 
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id