ibf/php-ansible
Composer 安装命令:
composer require ibf/php-ansible
包简介
A PHP wrapper for Ansible.
README 文档
README
This library is a OOP-wrapper for the ansible provisioning tool.
I intend to use this library for a symfony2 bundle and also a deployment GUI, based on php/symfony2.
The current implementation is feature-complete for the ansible-playbook and ansible-galaxy commands.
Prerequisites
Your OS should be a flavor of linux and ansible has to be installed. It's easiest if ansible is in PATH :-) The library tries to find ansible-playbook and ansible-galaxy by itself or use the paths/executables you provide.
Usage
First instantiate the base object which works as a factory for the commands. Only the first parameter is mandatory, and provides the library with the path to your ansible deployment file structure.
$ansible = new Ansible( '/path/to/ansible/deployment' );
Optionally, you can specify the path of your ansible-playbook and ansible-galaxy commands, just in case they are not in the PATH.
$ansible = new Ansible( '/path/to/ansible/deployment', '/optional/path/to/command/ansible-playbook', '/optional/path/to/command/ansible-galaxy' );
You can also pass any PSR compliant logging class to have further details logged. This is especially useful to have the actual run command logged.
$ansible = new Ansible( '/path/to/ansible/deployment' ); // $logger is a PSR-compliant logging implementation (e.g. monolog) $ansible->setLogger($logger);
Playbooks
Then you can use the object just like in your previous ansible deployment.
If you don't specify an inventory file with ->inventoryFile('filename'), the wrapper tries to determine one, based on your playbook name:
$ansible ->playbook() ->play('mydeployment.yml') // based on deployment root ->user('maschmann') ->extraVars(['project_release' => 20150514092022]) ->limit('test') ->execute();
This will create following ansible command:
$ ansible-playbook mydeployment.yml -i mydeployment --user=maschmann --extra-vars="project-release=20150514092022" --limit=test
For the execute command you can use a callback to get real-time output of the command:
$ansible ->playbook() ->play('mydeployment.yml') // based on deployment root ->user('maschmann') ->extraVars(['project_release' => 20150514092022]) ->limit('test') ->execute(function ($type, $buffer) { if (Process::ERR === $type) { echo 'ERR > '.$buffer; } else { echo 'OUT > '.$buffer; } });
If no callback is given, the method will return the ansible-playbook output as a string, so you can either echo or directly pipe it into a log/whatever.
You can also pass an external YML/JSON file as extraVars containing a complex data structure to be passed to Ansible:
$ansible ->playbook() ->play('mydeployment.yml') // based on deployment root ->extraVars('/path/to/your/extra/vars/file.yml') ->execute();
You can have a Json output adding json() option that enable 'ANSIBLE_STDOUT_CALLBACK=json' env vars to make a json output in ansible.
$ansible ->playbook() ->json() ->play('mydeployment.yml') // based on deployment root ->extraVars('/path/to/your/extra/vars/file.yml') ->execute();
Galaxy
The syntax follows ansible's syntax with one deviation: list is a reserved keyword in php (array context) and therefore I had to rename it to "modulelist()".
$ansible ->galaxy() ->init('my_role') ->initPath('/tmp/my_path') // or default ansible roles path ->execute();
would generate:
$ ansible-galaxy init my_role --init-path=/tmp/my_path
You can access all galaxy commands:
init()info()install()help()modulelist()remove()
You can combine the calls with their possible arguments, though I don't have any logic preventing e.g. --force from being applied to e.g. info().
Possible arguments/options:
initPath()offline()server()force()roleFile()rolesPath()ignoreErrors()noDeps()
Process timeout
Default process timeout is set to 300 seconds. If you need more time to execute your processes: Adjust the timeout :-)
$ansible ->galaxy() ->setTimeout(600) …
Thank you for your contributions!
thank you for reviewing, bug reporting, suggestions and PRs :-) xabbuh, emielmolenaar, saverio, soupdiver, linaori, paveldanilin and many others!
Future features
The Next steps for implementation are:
- improve type handling and structure, due to overall complexity of the playbook at the moment
- scalar typehints all over the place
- provide docker support for development
- move to php8.0 exclusively for the next major release
- wrapping the library into a bundle -> maybe
- provide commandline-capabilities -> maybe
License
php-ansible is licensed under the MIT license. See the LICENSE for the full license text.
ibf/php-ansible 适用场景与选型建议
ibf/php-ansible 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「ansible」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ibf/php-ansible 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ibf/php-ansible 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ibf/php-ansible 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP wrapper for Ansible.
Create vm for develop by ansible and docker
This will help you to generate a provisioning to run your Symfony apps
Alfabank REST API integration
GZERO CMS Platform.
Use PHP to run Ansible commands and playbooks
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-17