基于运行linuxcontainers的动态库存管理

lxcible的Python项目详细描述


pypy badge

lxcible公司

基于运行linuxcontainers的动态库存管理

logo

安装

$ pip install lxcible

或来源:

^{pr2}$

简介

基于运行linuxcontainers的动态ansible库存。在

当用--list调用时,lxcible将根据 当前正在运行的容器。在

使用示例

从包含公钥的映像创建一些容器 /home/ubuntu/.ssh/authorize_keys

$ lxc launch bionic-with-pubkey foo
Creating fooStarting foo$ lxc launch bionic-with-pubkey bar
Creating barStarting bar$ lxc launch bionic-with-pubkey baz
Creating bazStarting baz
$ lxc list
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
| NAME |  STATE  |         IPV4         |                     IPV6                     |   TYPE    | SNAPSHOTS |
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
| bar  | RUNNING | 10.153.75.179 (eth0) | fd42:9f54:f06:b4a5:216:3eff:fe0a:a7d4 (eth0) | CONTAINER | 0         |
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
| baz  | RUNNING | 10.153.75.163 (eth0) | fd42:9f54:f06:b4a5:216:3eff:fe30:3194 (eth0) | CONTAINER | 0         |
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
| foo  | RUNNING | 10.153.75.98 (eth0)  | fd42:9f54:f06:b4a5:216:3eff:fed6:ca26 (eth0) | CONTAINER | 0         |
+------+---------+----------------------+----------------------------------------------+-----------+-----------+

使用这三个运行的容器,lxcible --list生成以下输出:

{"_meta":{"hostvars":{"bar":{"ansible_host":"10.153.75.179"},"baz":{"ansible_host":"10.153.75.163"},"foo":{"ansible_host":"10.153.75.98"}}},"all":["bar","baz","foo"]}

最小测试行动手册,main.yml

----hosts:allserial:1gather_facts:Falsetasks:-name:Fetch ssh host keysdelegate_to:localhostlineinfile:path:~/.ssh/known_hostscreate:yesstate:presentline:"{{lookup('pipe','ssh-keyscan-trsa'+ansible_host)}}"-hosts:allgather_facts:Falsetasks:-name:"Testconnectivity"ping:

这将使用ssh keyscan收集ssh主机密钥。这部分将连续进行,因为 lineinfile不能很好地处理并发写操作(请参见:https://github.com/ansible/ansible/issues/31712)。 第二部分ping主机以测试ssh连接。在

让我们运行剧本:

$ ansible-playbook -u ubuntu -i /usr/local/bin/lxcible main.yml 

PLAY [all] ****************************************************************************************************************************TASK [Fetch ssh host keys] ************************************************************************************************************#10.153.75.179:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
changed: [bar -> localhost]PLAY [all] ****************************************************************************************************************************TASK [Fetch ssh host keys] ************************************************************************************************************#10.153.75.163:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
changed: [baz -> localhost]PLAY [all] ****************************************************************************************************************************TASK [Fetch ssh host keys] ************************************************************************************************************#10.153.75.98:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
changed: [foo -> localhost]PLAY [all] ****************************************************************************************************************************TASK [Test connectivity] **************************************************************************************************************ok: [baz]ok: [bar]ok: [foo]PLAY RECAP ****************************************************************************************************************************bar                        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0baz                        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0foo                        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

团体

现在,让我们将机器添加到ansible组:

$ lxc config set bar user.ansible.groups lemp
$ lxc config set foo user.ansible.groups django,node_exporter
$ lxc config set baz user.ansible.groups node_exporter
$ lxcible --list
{
    "_meta": {
        "hostvars": {
            "bar": {
                "ansible_host": "10.153.75.179"
            },
            "baz": {
                "ansible_host": "10.153.75.163"
            },
            "foo": {
                "ansible_host": "10.153.75.98"
            }
        }
    },
    "all": [
        "bar",
        "baz",
        "foo"
    ],
    "django": [
        "foo"
    ],
    "lemp": [
        "bar"
    ],
    "node_exporter": [
        "baz",
        "foo"
    ]
}

变量

让我们向容器bar添加一个变量,以便在Ansible模板中使用:

lxc config set bar user.ansible.vars '{ "documentroot": "/var/www/myapp/public" }'

您将在hostvars处看到变量:

$ lxcible --list
{
    "_meta": {
        "hostvars": {
            "bar": {
                "documentroot": "/var/www/myapp/public",
                "ansible_host": "10.153.75.179"
            },
            "baz": {
                "ansible_host": "10.153.75.163"
            },
            "foo": {
                "ansible_host": "10.153.75.98"
            }
        }
    },
    "all": [
        "bar",
        "baz",
        "foo"
    ],
    "django": [
        "foo"
    ],
    "lemp": [
        "bar"
    ],
    "node_exporter": [
        "baz",
        "foo"
    ]
}

在ansible模板中,只需使用{{ documentroot }}来获取它的值。在

历史

0.2.0(2020年10月14日)

  • 在ansible清单中使用自定义lxc变量

  • 漂亮的json输出

0.1.4(2020年9月22日)

  • 使用jq选择数据

0.1.3(2020-09-11)

  • 不要在没有IPv4地址的容器上崩溃

0.1.2(2020-04-08)

  • 更新了自述文件

0.1.0(2020-04-07)

  • PyPI的第一个版本。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java我可以使用Hibernate连接不同的数据库并从表中导入数据吗?没有预定义的对象类   java为什么getBoundsInLocal获取错误的坐标?   java在处理并发哈希映射时必须使用锁   java将过滤的JTable数据导出到文本文件   从命令行编译java不使用库   Java使用方法和构造函数绘制图形   java理解优先级队列中的排序方法   尝试解密文件时,java CipherInputStream为空   在Java中将字符串解析为长字符串   java我想在raspberry pi重新启动时在crontabe中执行arecord命令   弹性搜索弹性搜索Lucene公式的Java计算   java使用apachepoi和docx4j读取doc文件   swing显示JavaGUI是否需要特殊处理?   java使用字符串进行数学运算   java在SpringMVC中用JSP初始化菜单项选择表单数据的最佳方法   无多线程的java多客户端程序   swing如何在Java中对BuffereImage进行去饱和?   java在安卓中创建自定义地图