我有一个可靠的文件。我只想 运行 烧瓶中的 ansible 文件 API

I have a ansible file . I just want to run ansible file in flask API

我有一个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 运行在 vagrant 中成功
(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' )

但是没用

这应该有效:

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"
}