这篇文章开始将对Ansible的使用进行稍微复杂一点的场景介绍,以kubernetes1.15.0为例,介绍如何使用ansible进行离线安装,整体以role的方式引入,这篇文章介绍一下如何进行二进制文件的下载。
安装需要如下二进制文件:
role的功能实现在tasks/main.yml中,详细如下所示:
[root@liumiaocn ansible]# cat download/tasks/main.yml
---
# prepare download dir
- name: create download file in advance
file:
path: "{{ item }}"
state: directory
with_items:
- "{{ var_download_base_dir }}"
- "{{ var_download_save_dir_k8s }}"
- "{{ var_download_save_dir_cfssl }}"
- "{{ var_download_save_dir_flannel }}"
- "{{ var_download_save_dir_etcd }}"
- "{{ var_download_save_dir_cni }}"
- "{{ var_download_save_dir_docker }}"
# tasks file for download
- name: download binary kubernetes
get_url:
url: '{{ var_download_url_k8s }}'
dest: '{{ var_download_tmp_file_k8s }}'
mode: '{{ var_download_save_mode }}'
- name: extract binary files of kubernetes
unarchive:
copy: no
src: "{{ var_download_tmp_file_k8s }}"
dest: "{{ var_download_base_dir }}"
extra_opts: "{{ var_download_unarchive_extras_files_k8s }}"
- name: create output kubernetes binary dir
synchronize:
src: "{{ var_download_tmp_dir_k8s }}/"
dest: "{{ var_download_save_dir_k8s }}"
- name: download binary :cfssl
get_url:
url: '{{ var_download_url_cfssl }}'
dest: '{{ var_download_file_cfssl }}'
mode: '{{ var_download_save_mode_bin }}'
- name: download binary :cfssljson
get_url:
url: '{{ var_download_url_cfssljson }}'
dest: '{{ var_download_file_cfssljson }}'
mode: '{{ var_download_save_mode_bin }}'
- name: download binary :cfsslcertinfo
get_url:
url: '{{ var_download_url_cfsslcertinfo }}'
dest: '{{ var_download_file_cfsslcertinfo }}'
mode: '{{ var_download_save_mode_bin }}'
- name: download binary :flannel
get_url:
url: '{{ var_download_url_flannel }}'
dest: '{{ var_download_file_flannel }}'
mode: '{{ var_download_save_mode }}'
- name: extract binary files of flannel
unarchive:
copy: no
src: "{{ var_download_file_flannel }}"
dest: "{{ var_download_save_dir_flannel }}"
extra_opts: "{{ var_download_unarchive_extras_files_flannel }}"
- name: download binary :etcd
get_url:
url: '{{ var_download_url_etcd }}'
dest: '{{ var_download_file_etcd }}'
mode: '{{ var_download_save_mode }}'
- name: extract binary files of etcd
unarchive:
copy: no
src: "{{ var_download_file_etcd }}"
dest: "{{ var_download_save_dir_etcd }}"
extra_opts: "{{ var_download_unarchive_extras_files_etcd }}"
- name: create output docker binary dir
synchronize:
src: "{{ var_download_tmp_dir_etcd }}/"
dest: "{{ var_download_save_dir_etcd }}"
- name: download binary :cni
get_url:
url: '{{ var_download_url_cni }}'
dest: '{{ var_download_file_cni }}'
mode: '{{ var_download_save_mode }}'
- name: extract binary files of cni
unarchive:
copy: no
src: "{{ var_download_file_cni }}"
dest: "{{ var_download_save_dir_cni }}"
- name: download binary :docker
get_url:
url: '{{ var_download_url_docker }}'
dest: '{{ var_download_file_docker }}'
mode: '{{ var_download_save_mode }}'
- name: extract binary files of docker
unarchive:
copy: no
src: "{{ var_download_file_docker }}"
dest: "{{ var_download_save_dir_docker }}"
# change docker directory name because of docker file in directory
- name: copy docker/dockertmp/ from docker/docker/
copy:
src: "{{ var_download_tmp_dir_docker }}/"
dest: "{{ var_download_tmp_dir_docker }}tmp/"
# delete docker/docker directory
- name : delete docker/docker directory for moving action
file:
path: "{{ var_download_tmp_dir_docker }}/"
state: absent
- name: create output docker binary dir
synchronize:
src: "{{ var_download_tmp_dir_docker }}tmp/"
dest: "{{ var_download_save_dir_docker }}"
- name: delete useless temp files
file:
path: "{{ item }}"
state: absent
with_items:
- "{{ var_download_tmp_dir_k8s_server }}"
- "{{ var_download_save_dir_flannel }}/README.md"
- "{{ var_download_tmp_dir_docker }}"
- "{{ var_download_tmp_dir_docker }}tmp"
- "{{ var_download_tmp_dir_etcd }}"
- "{{ var_download_file_cni }}"
- "{{ var_download_file_docker }}"
- "{{ var_download_file_etcd }}"
- "{{ var_download_file_flannel }}"
- "{{ var_download_tmp_file_k8s }}"
[root@liumiaocn ansible]#
此role主要实现了下载的功能,主要使用的基础知识和注意事项如下所示:
[root@liumiaocn ansible]# ansible-playbook download/tests/test.yml
PLAY [localhost] ***********************************************************************************************************************
TASK [download : create download file in advance] **************************************************************************************
changed: [localhost] => (item=/tmp/binary)
changed: [localhost] => (item=/tmp/binary/kubernetes)
changed: [localhost] => (item=/tmp/binary/cfssl)
changed: [localhost] => (item=/tmp/binary/flannel)
changed: [localhost] => (item=/tmp/binary/etcd)
changed: [localhost] => (item=/tmp/binary/cni)
changed: [localhost] => (item=/tmp/binary/docker)
TASK [download : download binary kubernetes] *******************************************************************************************
changed: [localhost]
TASK [download : extract binary files of kubernetes] ***********************************************************************************
changed: [localhost]
TASK [download : create output kubernetes binary dir] **********************************************************************************
changed: [localhost]
TASK [download : download binary :cfssl] ***********************************************************************************************
changed: [localhost]
TASK [download : download binary :cfssljson] *******************************************************************************************
changed: [localhost]
TASK [download : download binary :cfsslcertinfo] ***************************************************************************************
changed: [localhost]
TASK [download : download binary :flannel] *********************************************************************************************
changed: [localhost]
TASK [download : extract binary files of flannel] **************************************************************************************
changed: [localhost]
TASK [download : download binary :etcd] ************************************************************************************************
changed: [localhost]
TASK [download : extract binary files of etcd] *****************************************************************************************
changed: [localhost]
TASK [download : create output docker binary dir] **************************************************************************************
changed: [localhost]
TASK [download : download binary :cni] *************************************************************************************************
changed: [localhost]
TASK [download : extract binary files of cni] ******************************************************************************************
changed: [localhost]
TASK [download : download binary :docker] **********************************************************************************************
changed: [localhost]
TASK [download : extract binary files of docker] ***************************************************************************************
changed: [localhost]
TASK [download : copy docker/dockertmp/ from docker/docker/] ***************************************************************************
changed: [localhost]
TASK [download : delete docker/docker directory for moving action] *********************************************************************
changed: [localhost]
TASK [download : create output docker binary dir] **************************************************************************************
changed: [localhost]
TASK [download : delete useless temp files] ********************************************************************************************
changed: [localhost] => (item=/tmp/binary/kubernetes/server)
ok: [localhost] => (item=/tmp/binary/flannel/README.md)
changed: [localhost] => (item=/tmp/binary/docker/docker)
changed: [localhost] => (item=/tmp/binary/docker/dockertmp)
changed: [localhost] => (item=/tmp/binary/etcd/etcd-v3.3.13-linux-amd64)
changed: [localhost] => (item=/tmp/binary/cni/cni-plugins-amd64-v0.7.4.tgz)
changed: [localhost] => (item=/tmp/binary/docker/docker-18.09.7.tgz)
changed: [localhost] => (item=/tmp/binary/etcd/etcd-v3.3.13-linux-amd64.tar.gz)
changed: [localhost] => (item=/tmp/binary/flannel/flannel-v0.11.0-linux-amd64.tar.gz)
changed: [localhost] => (item=/tmp/binary/kubernetes.tar.gz)
PLAY RECAP *****************************************************************************************************************************
localhost : ok=20 changed=20 unreachable=0 failed=0
[root@liumiaocn ansible]#
[root@liumiaocn ansible]# tree /tmp/binary/
/tmp/binary/
|-- cfssl
| |-- cfssl
| |-- cfssl-certinfo
| `-- cfssljson
|-- cni
| |-- bridge
| |-- dhcp
| |-- flannel
| |-- host-device
| |-- host-local
| |-- ipvlan
| |-- loopback
| |-- macvlan
| |-- portmap
| |-- ptp
| |-- sample
| |-- tuning
| `-- vlan
|-- docker
| |-- containerd
| |-- containerd-shim
| |-- ctr
| |-- dockerd
| |-- docker-init
| |-- docker-proxy
| `-- runc
|-- etcd
| |-- etcd
| `-- etcdctl
|-- flannel
| |-- flanneld
| `-- mk-docker-opts.sh
`-- kubernetes
|-- kube-apiserver
|-- kube-controller-manager
|-- kubectl
|-- kubelet
|-- kube-proxy
`-- kube-scheduler
6 directories, 33 files
[root@liumiaocn ansible]#
和Shell脚本的比较可以参看: