按需收集下常用的 ansible 操作。
docker 安装脚本:(这里图省事,没有写成 ansible 任务)
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/debian \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
推荐使用 python3,python2 可能会遇到下面的错误:
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 n129’s Python /usr/bin/python. 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
orpip install docker-py
(Python 2.6). The error was: cannot import name credentials”}
在目标机器上安装 pip3 以及 docker sdk:
- name: install pip3
apt:
name: python3-pip
state: present
- name: install docker sdk
pip:
name: docker
- name: docker is running
systemd:
name: docker
state: started
到 All modules 查找 docker 相关的 module,每个 module 文档的末尾都给出里的大量示范:
docker 登录、删除容器、启动容器等:
- name: docker login
docker_login:
registry_url: xxx.com
username: "XXXX"
password: "XXX"
reauthorize: true
- name: Delete demo container
docker_container:
name: demo
state: absent
force_kill: yes
- name: Start demo container
docker_container:
name: demo
image: alpine
state: started
recreate: yes
force_kill: yes