python - 使用 Python API 运行 Ansible playbook

标签 python ansible

我创建了一个 Ansible 剧本来启动 5 个 AWS EC2 实例。我想使用 Python API 运行此剧本,但我对如何执行此操作感到困惑。

这是我的剧本:

---
- name: Provision an EC2 Instance
hosts: local
connection: local
gather_facts: False
tags: provisioning
# Necessary Variables for creating/provisioning the EC2 Instance
vars:
  instance_type: t2.micro
  security_group: webserver 
  image: ami-f95ef58a
  region: eu-west-1c 
  keypair: Daniel 
  count: 5

# Task that will be used to Launch/Create an EC2 Instance
tasks:

  - name: Create a security group
    local_action: 
      module: ec2_group
      name: "{{ security_group }}"
      description: Security Group for webserver Servers
      region: "{{ region }}"
      rules:
        - proto: tcp
          type: ssh
          from_port: 22
          to_port: 22
          cidr_ip: 0.0.0.0/0
        - proto: tcp
          from_port: 80
          to_port: 80
          cidr_ip: 0.0.0.0/0
      rules_egress:
        - proto: all
          type: all
          cidr_ip: 0.0.0.0/0


  - name: Launch the new EC2 Instance
    local_action: ec2 
                  group={{ security_group }} 
                  instance_type={{ instance_type}} 
                  image={{ image }} 
                  wait=true 
                  region={{ region }} 
                  keypair={{ keypair }}
                  count={{count}}
    register: ec2

  - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
    local_action: lineinfile 
                  dest="./hosts" 
                  regexp={{ item.public_ip }} 
                  insertafter="[webserver]" line={{ item.public_ip }}
    with_items: ec2.instances


  - name: Wait for SSH to come up
    local_action: wait_for 
                  host={{ item.public_ip }} 
                  port=22 
                  state=started
    with_items: ec2.instances

  - name: Add tag to Instance(s)
    local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
    with_items: ec2.instances
    args:
      tags:
        Name: webserver

这是运行剧本的代码:

ansible-playbook -i hosts ec2_launch.yml

如何在 Python 项目文件中使用此代码运行 playbook?

最佳答案

如果您只想运行一个可以通过 Ansible 正常运行的完整剧本,那么为什么不直接使用 subprocess掏出钱来运行它?

这应该像这样简单:

from subprocess import call
call(["ansible-playbook", "-i", "hosts", "ec2_launch.yml"])

只要 playbook 和 inventory 与您的 Python 项目位于相同的相对路径中。

如果您想与 Ansible 的 Python API 进行更精细的交互,那么您可能需要 read the docs .

关于python - 使用 Python API 运行 Ansible playbook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37099632/

相关文章:

python - 在 Pandas 中连接时,数据未将分隔符应用于 csv 文件

python - 编写干净的代码 : Nested For/If/Elif in Pyramid

ansible - 在 Ansible 的调试任务中格式化 stdout

node.js - 如何使用 ansible 永久设置 NODE_ENV?

ansible - 如果 json 键名称中包含破折号,则带有 json 循环的 Ansible playbook 不起作用

python - 在现有 DataFrame 中设置 DateTimeIndex

javascript - URL更改后附加内容消失

Python3 randrange 给出相同的结果

ansible - pause ansible playbook 以供用户确认,是否运行休息任务

ansible - 在 Ansible 中,如何连接到 Windows 主机?