Ansible內置一些專用的Docker模塊,幫助咱們更好的管理容器相關環境html
Ansible 2.9.16 CentOS Linux release 7.9.2009 (Core) Docker version 18.09.9 Python 2.7.5 Docker python module docker-4.4.1
#1 安裝docker https://blog.csdn.net/xys2015/article/details/109370082 #2 開啓EPEL repository https://blog.csdn.net/xys2015/article/details/109378741 #3 安裝pip yum install python-pip #4 安裝docker python module pip install docker
這些依賴固然也能夠經過Ansible來安裝python
話很少說,環境配置好後,咱們先看一個示例nginx
目標git
目錄結構docker
[root@192_168_1_237 ysansible]# tree docker/ docker/ ├── main.yml ├── mybusybox │ └── Dockerfile └── README.md #完整內容參見 https://gitee.com/as4k/ysansible/tree/master/docker/main1.yml
關於Docker自己的一些知識,本文很少作介紹ubuntu
重點提要centos
- name: Ensure Docker image is built from the test Dockerfile. docker_image: #至關於docker build相關功能 name: mybusybox:v3 #這裏是構建後的鏡像名稱,若是須要從新構建鏡像,這裏的版本號須要更改 source: build build: pull: yes #自動拉取構建須要的鏡像,咱們這裏即自動拉取busybox鏡像 path: mybusybox #在 ./mybusybox 找 Dockerfile 文件 state: present - name: Ensure the test container is running. docker_container: #至關於docker run相關功能 image: mybusybox:v3 #運行這個鏡像 name: mybusybox #鏡像的名稱 state: started
執行結果驗證app
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f80619da371e mybusybox:v3 "tail -F /tmp/tmp.txt" 36 minutes ago Up 36 minutes mybusybox # docker exec mybusybox hostname f80619da371e
Docker Command | Ansible state value | 備註 |
---|---|---|
docker run | started | |
docker run | present | 保持原樣,若是容器已是中止狀態,將不會啓動 |
docker stop | stopped | |
docker rm | absent |
1 遠程機器依賴安裝curl
參考上文,固然也可使用Ansible安裝ide
2 改動playbook
- name: copy Dockerfile to target machine copy: src: mybusybox dest: /tmp - name: Ensure Docker image is built from the test Dockerfile. docker_image: name: mybusybox:v3 source: build build: pull: yes path: /tmp/mybusybox state: present - name: Ensure the test container is running. docker_container: image: mybusybox:v3 name: mybusybox state: started
完整內容參考 https://gitee.com/as4k/ysansible/blob/master/docker/main2.yml
3 執行
ansible-playbook main2.yml --limit 192.168.31.101
若是咱們熟悉docker image xxx
相關操做,docker_image
基本上跟其對應,可使用ansible-doc docker_image
查看文檔,或者查看其官方文檔 https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#ansible-collections-community-docker-docker-image-module
下面列舉一些示例
#1 下載鏡像 - name: Pull an image community.docker.docker_image: name: pacur/centos-7 source: pull #2 給鏡像打標籤 - name: Add tag latest to image community.docker.docker_image: name: myimage:7.1.2 #原鏡像名稱 repository: myimage:latest #新打的鏡像名稱 force_tag: yes #若是標籤重複,強制覆蓋 source: local #3 刪除一個鏡像 - name: Remove image community.docker.docker_image: state: absent name: registry.ansible.com/chouseknecht/sinatra tag: v1 #4 導出鏡像爲tar包 - name: Archive image community.docker.docker_image: name: registry.ansible.com/chouseknecht/sinatra tag: v1 archive_path: my_sinatra.tar source: local
同類,這個模塊實現docker container xxx
相關功能,在線文檔 https://docs.ansible.com/ansible/latest/collections/community/docker/docker_container_module.html#ansible-collections-community-docker-docker-container-module
一些參考示例以下:
#1 啓動一個容器 - name: Ensure the test container is running. docker_container: image: mybusybox:v3 pull: yes #不管鏡像是否存在,都進行拉取,這種狀況能夠規避鏡像被更新,可是標籤沒變的狀況 name: mybusybox state: started #2 啓動鏡像並掛載本地存儲 - name: Finer container restart/update control community.docker.docker_container: name: test image: ubuntu:18.04 env: arg1: "true" arg2: "whatever" volumes: - /tmp:/tmp #3 啓動鏡像並附帶健康檢查 - name: Start container with healthstatus community.docker.docker_container: name: nginx-proxy image: nginx:1.13 state: started healthcheck: test: ["CMD", "curl", "--fail", "http://nginx.host.com"] interval: 1m30s timeout: 10s retries: 3 start_period: 30s
#1 沒有安裝docker python module ================== fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on 192_168_1_237's Python /usr/bin/python2. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named requests.exceptions"} #2 沒有安裝docker或者docker未啓動 ================== fatal: [localhost]: FAILED! => {"changed": false, "msg": "Error connecting: Error while fetching server API version: ('Connection aborted.', error(111, 'Connection refused'))"}
https://docs.ansible.com/ansible/latest/scenario_guides/guide_docker.html https://docs.ansible.com/ansible/latest/collections/index_module.html https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#ansible-collections-community-docker-docker-image-module
所有支持的docker模塊截圖以下