lucbu/ansible-wrapper-bundle

Ansible 的 Symfony 包装器,提供启动器、事件...

dev-master 2016-11-17 22:11 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:21:47 UTC


README

介绍

此捆绑包为 Ansible 2 提供包装器和工具。

它可以配置和执行命令(ansible,ansible-playbook),触发事件,重新格式化错误信息。

我不是专家,所以,请随时发送 pull requests。

先决条件

##安装

使用以下命令安装:

composer require lucbu/ansible-wrapper-bundle

将捆绑包添加到内核

new Lucbu\AnsibleWrapperBundle\LucbuAnsibleWrapperBundle(),

您可以创建一个用户,该用户将用于启动 ansible/ansible-playbook 命令。

useradd ansible-launcher

然后使用 visudo(在 sudoers 文件中)为您的 http 用户添加以下用户使用权限:

www-data ALL=(ansible-launcher) NOPASSWD: /usr/local/bin/ansible
www-data ALL=(ansible-launcher) NOPASSWD: /usr/local/bin/ansible-playbook

您应该在 ansible.cfg 中的 "[defaults]" 后添加此行:

stdout_callback = json

在您的配置(config.yml)中添加此行:

lucbu_ansible_wrapper:
  ansible:
    user: 'ansible-launcher'    #The user that will launch ansible command
    dir: '/usr/local/bin'       #The folder where is located 'ansible'
  ansible_playbook:
    user: 'ansible-launcher'    #The user that will launch ansible-playbook command
    dir: '/usr/local/bin'       #The folder where is located 'ansible-playbook'

使用

Playbook 文件(/tmp/ansible/addfile.yml)

-
    hosts: all
    tasks:
      - file: path=/tmp/{{ filename }} state=touch mode="u=rw,g=r,o=r"

在控制器中

$playbookFile = '/tmp/ansible/addfile.yml';
$vars = ['filename'=>'test'];
$hostFile = '/tmp/hostfile.yml';

$process = AnsiblePlaybookProcessBuilder::createBuilder($playbookFile)
  // Add the vars to command
  ->extraVars($vars)
  // Set the InventoryFile
  ->inventoryFile($hostFile)
  // SSH connecting with user "root"
  ->user('root')
  // Create the process
  ->getAnsiblePlaybookProcess();

$cmd_line = $process->getProcess()->getCommandLine();

try {
    // Executing code (and firing start/new output/finish events)
    $output = $this->container->get('lucbu_ansible_wrapper.ansible_playbook_process_executor')->executeAnsiblePlaybookProcess($process);
} catch (\Exception $e) {
    $output = $e->getMessage();
}

待办事项

  • 重新格式化错误信息(AnsibleMessageFormatter)(到一个对象?)
  • 清理 AbstractProcessExecutor?