我有一个ansible文件。我只想在flask API中运行ansible文件

时间:2015-02-04 12:28:43

标签: python-2.7 flask ansible-playbook

我有一个ansible文件:

 hosts: all
 user: vagrant
 sudo: true
 tasks:
 - user: name=pape shell=/bin/bash groups=admin append=yes
   sudo: true

 - name: Creates directory
   file: path=/home/pape/.ssh state=directory owner=pape  group=pape  
         mode=0700
   sudo: true

 - name: Make sure that the /home/{{createuser}} directory exsists
   action: file path=/home/james/.ssh state=directory

 - name: Setup | authorized key upload
   copy: src=/home/birendra/.ssh/id_rsa.pub 
    dest=/home/pape/.ssh/authorized_keys owner=pape group=pape  
    mode=0600

这个ansible-playbook在流浪汉中成功运行    (config.vm.network“private_network”,ip:“192.168.33.10”)。

我想在flask API中运行此文件。我还有一个库存文件    (127.0.0.1和192.168.33.10)

我写了剧本ansible API

 playbook = PlayBook(remote_user='vagrant',      
 playbook='/home/birendra/vagrant/playbook.yml',callbacks=playbook_cb, 
 runner_callbacks=runner_cb, stats=stats, host_list=  
 '/home/birendra/birendra/inventory/swat-
 staging.ini' )

但它不起作用

1 个答案:

答案 0 :(得分:3)

这应该有效:

import ansible.playbook
from ansible import callbacks
from ansible import utils
ANSIBLE_HOSTS = 'hosts'
PLAYBOOK = 'foo.yml'
def deploy():
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    inventory = ansible.inventory.Inventory(ANSIBLE_HOSTS)
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats,verbose=utils.VERBOSITY)

    pb = ansible.playbook.PlayBook(playbook=PLAYBOOK,
                                   callbacks=playbook_cb,
                                   runner_callbacks=runner_cb,
                                   stats=stats, 
                                   inventory=inventory,
                                   extra_vars={'name': "AAA"})
    pb.run()

if __name__ == "__main__":
    deploy()

foo.yml:

---
- hosts: localhost
  connection: local
  user: sergey
  sudo: no

  tasks:
    - name: Print hello
      debug: msg="Hey how are you {{ name }}"

主机

[localhost]
localhost ansible_connection=local

输出:

PLAY [localhost] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [localhost]

TASK: [Print hello] *********************************************************** 
ok: [localhost] => {
    "msg": "Hey how are you AAA"
}