ansible的docker_container模块使用

发布时间 2023-04-17 17:40:38作者: 蒲公英PGY

使用docker_container模块创建容器
示例:

---
- hosts: myserverip
  remote_user: root  # 执行用户
  gather_facts: false  # 屏蔽系统信息返回
  vars:
    ansible_python_interpreter: /usr/bin/python3  # 定义ansible使用python3的环境
  serial: "100%"   # 更新比列,可以是百分比,具体数值
  tasks:
  - name: docker login  # 登陆镜像中心
    docker_login:
      registry: "harbor.myregister.com"
      username: "test"
      password: "test"
      reauthorize: yes

  - name: start containerd
    docker_container:
      name: servername
      image: imagename
      hostname: servername
      volumes:
        - "/etc/localtime:/etc/localtime"
      restart_policy: on-failure
      network_mode: host  # 设置网络模式
      log_options:  # 设置日志tag 用户elk日志采集筛选
        tag: servername
      capabilities:
        - aLL
      env:  # 设置环境变量
        java_opts: " -Drocketmq.client.name=servername_ip"
      cpus: 2.0   # 设置cpu使用
      memory: "2G"  # 设置内存使用
      healthcheck:  # 健康检查
        test: ["CMD", "curl -f http://localhost:port/actuator/health || exit 1"]
        interval: "60s"
        timeout: "3s"
        start_period: "5s"
        retries: 3

更多使用参数,参考官方文档:https://docs.ansible.com/ansible/latest/collections/community/docker/docker_container_module.html#ansible-collections-community-docker-docker-container-module