实例学习ansible系列(12)常用模块之docker

                       
 

知识点:ansible使用docker可以对其进行管理。基本接近docker-compose对docker的使用支持,非常接近。诸如从port的设定到volume_from都支持,但是需要docker-py0.3.0 以上的支持。

使用前提条件

 

需要使用docker的module的管理对象节点需要满足如下前提

               
Package 所需版本
python 2.6 以上
docker-py 0.3.0 以上
docker server 0.10.0 以上

安装docker-py

 

一般python等基本上无需意识,一般安装了docker-py本模块就能支持。

安装步骤[root@host32 ~]# yum -y python-pip[root@host31 ~]# yum -y install python-pip[root@host31 ~]# pip install docker-py
  
  
  
  • 1
  • 2
  • 3
  • 4

使用docker模块启动container

 

事前确认

[root@host31 ~]# ansible host32 -m shell -a "docker images && docker ps -a"host32 | SUCCESS | rc=0 >>REPOSITORY          TAG                 IMAGE ID            CREATED             SIZEdocker.io/jenkins   latest              b4974ba62598        2 weeks ago         741 MBCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES[root@host31 ~]#
  
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
 

启动container

[root@host31 ~]# ansible host32 -m docker -a "image=docker.io/jenkins ports=8080:8080"host32 | SUCCESS => {    "ansible_facts": {        "docker_containers": [            {                "AppArmorProfile": "",                "Args": [                    "--",                    "/usr/local/bin/jenkins.sh"                ],                "Config": {                    "AttachStderr": false,                    "AttachStdin": false,                    "AttachStdout": false,                    "Cmd": null,                    "Domainname": "",                    "Entrypoint": [                        "/bin/tini",                        "--",                        "/usr/local/bin/jenkins.sh"                    ],                    "Env": [                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",                        "LANG=C.UTF-8",                        "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64",                        "JAVA_VERSION=8u91",                        "JAVA_DEBIAN_VERSION=8u91-b14-1~bpo8+1",                        "CA_CERTIFICATES_JAVA_VERSION=20140324",                        "JENKINS_HOME=/var/jenkins_home",                        "JENKINS_SLAVE_AGENT_PORT=50000",                        "TINI_VERSION=0.9.0",                        "TINI_SHA=fa23d1e20732501c3bb8eeeca423c89ac80ed452",                        "JENKINS_VERSION=2.7.1",                        "JENKINS_SHA=12d820574c8f586f7d441986dd53bcfe72b95453",                        "JENKINS_UC=https://updates.jenkins.io",                        "COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log"                    ],                    "ExposedPorts": {                        "50000/tcp": {},                        "8080/tcp": {}                    },                    "Hostname": "283789b73a8e",                    "Image": "docker.io/jenkins",                    "Labels": {},                    "OnBuild": null,                    "OpenStdin": false,                    "StdinOnce": false,                    "Tty": false,                    "User": "jenkins",                    "Volumes": {                        "/var/jenkins_home": {}                    },                    "WorkingDir": ""                },                "Created": "2016-07-31T03:03:38.350849615Z",                "Driver": "devicemapper",                "ExecIDs": null,                "GraphDriver": {                    "Data": {                        "DeviceId": "25",                        "DeviceName": "docker-253:0-19943542-b5917ff17a3050352a5cf430fa1449362166da5142d76e931553119acb600654",                        "DeviceSize": "10737418240"                    },                    "Name": "devicemapper"                },                "HostConfig": {                    "Binds": null,                    "BlkioDeviceReadBps": null,                    "BlkioDeviceReadIOps": null,                    "BlkioDeviceWriteBps": null,                    "BlkioDeviceWriteIOps": null,                    "BlkioWeight": 0,                    "BlkioWeightDevice": null,                    "CapAdd": null,                    "CapDrop": null,                    "CgroupParent": "",                    "ConsoleSize": [                        0,                        0                    ],                    "ContainerIDFile": "",                    "CpuPeriod": 0,                    "CpuQuota": 0,                    "CpuShares": 0,                    "CpusetCpus": "",                    "CpusetMems": "",                    "Devices": null,                    "Dns": null,                    "DnsOptions": null,                    "DnsSearch": null,                    "ExtraHosts": null,                    "GroupAdd": null,                    "IpcMode": "",                    "Isolation": "",                    "KernelMemory": 0,                    "Links": null,                    "LogConfig": {                        "Config": {},                        "Type": "journald"                    },                    "Memory": 0,                    "MemoryReservation": 0,                    "MemorySwap": 0,                    "MemorySwappiness": -1,                    "NetworkMode": "default",                    "OomKillDisable": false,                    "OomScoreAdj": 0,                    "PidMode": "",                    "PidsLimit": 0,                    "PortBindings": {                        "8080/tcp": [                            {                                "HostIp": "0.0.0.0",                                "HostPort": "8080"                            }                        ]                    },                    "Privileged": false,                    "PublishAllPorts": false,                    "ReadonlyRootfs": false,                    "RestartPolicy": {                        "MaximumRetryCount": 0,                        "Name": ""                    },                    "SecurityOpt": null,                    "ShmSize": 67108864,                    "UTSMode": "",                    "Ulimits": null,                    "VolumeDriver": "",                    "VolumesFrom": null                },                "HostnamePath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/hostname",                "HostsPath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/hosts",                "Id": "283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955",                "Image": "sha256:b4974ba625983cc1250f3399a262d98398b914ff04d2ba360760b81d1c944983",                "LogPath": "",                "MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c122,c287"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值