# cat k8s-apps.yaml
# 设置变量 action 的默认值,并用于全部主机调用
- name: prepare
hosts: all
become: true
gather_facts: no
tasks:
- name: define var with default value
set_fact:
action: "{{ action | default('started') }}"
# 如果执行剧本没有指定变量 action的值,则使用默认的值: started
tags:
- k8s-master
- k8s-nginx
- k8s-nginx
- started
- stopped
- restarted
- name: 控制台输出 action 变量的值
debug:
var: action
# 启动集群服务
- name: start k8s master containerd
hosts: k8s-master
become: true
gather_facts: no
tasks:
- name: start master containerd service
service:
name: containerd
state: started
tags:
- k8s-master
- started
when: action == "started"
- name: start k8s node containerd
hosts: k8s-node
become: true
gather_facts: no
tasks:
- name: start node containerd service
service:
name: containerd
state: started
tags:
- k8s-node
- started
when: action == "started"
- name: start k8s nginx
hosts: k8s-nginx
become: true
gather_facts: no
tasks:
- name: start nginx service
service:
name: nginx
state: started
tags:
- k8s-nginx
- started
when: action == "started"
# 停止集群服务
- name: stop k8s master containerd
hosts: k8s-master
become: true
gather_facts: no
tasks:
- name: stop master containerd service
service:
name: containerd
state: stopped
tags:
- k8s-master
- stopped
when: action == "stopped"
- name: stop k8s node containerd
hosts: k8s-node
become: true
gather_facts: no
tasks:
- name: stop node containerd service
service:
name: containerd
state: stopped
tags:
- k8s-node
- stopped
when: action == "stopped"
- name: stop k8s nginx
hosts: k8s-nginx
become: true
gather_facts: no
tasks:
- name: stop nginx service
service:
name: nginx
state: stopped
tags:
- k8s-nginx
- stopped
when: action == "stopped"
# 重启集群服务
- name: restart k8s master containerd
hosts: k8s-master
become: true
gather_facts: no
tasks:
- name: restart master containerd service
service:
name: containerd
state: restarted
tags:
- k8s-master
- restarted
when: action == "restarted"
- name: restart k8s node containerd
hosts: k8s-node
become: true
gather_facts: no
tasks:
- name: restart node containerd service
service:
name: containerd
state: restarted
tags:
- k8s-node
- restarted
when: action == "restarted"
- name: restart k8s nginx
hosts: k8s-nginx
become: true
gather_facts: no
tasks:
- name: restart nginx service
service:
name: nginx
state: restarted
tags:
- k8s-nginx
- restarted
when: action == "restarted"
以下列出部分常用命令,关键是通过指定tags标签以及task任务来执行相应的任务动作
1、关闭集群应用使用命令,为了安全起见,设置一个条件变量action,防止错误操作导致停止应用服务
ansible-playbook k8s-apps.yaml -t stopped -e action=stopped
2、启动集群应用使用命令
ansible-playbook k8s-apps.yaml -t started -e action=started
3、重启集群应用启动命令
ansible-playbook k8s-apps.yaml -t restarted -e action=restarted
4、停止集群master节点的docker服务
ansible-playbook k8s-apps.yaml -t k8s-master,stopped -e action=stopped
5、停止集群node节点的docker服务
ansible-playbook k8s-apps.yaml -t k8s-node,stopped -e action=stopped
6、停止集群nginx负载均衡服务
ansible-playbook k8s-apps.yaml -t k8s-nginx,stopped -e action=stopped
7、执行指定的task剧本任务,以移动nginx服务为例
ansible-playbook k8s-apps.yaml --start-at-task="start nginx service" -e action=started
1、启动nginx组件的使用例子
# 启动nginx组件服务使用例子
[root@master01 ansible]# ansible-playbook k8s-apps.yaml -t k8s-nginx,started -e action=started
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [start k8s master containerd] ***************************************************************************************************************************************
TASK [start master containerd service] ***********************************************************************************************************************************
ok: [172.18.30.54]
ok: [172.18.30.55]
ok: [172.18.30.53]
PLAY [start k8s node containerd] *****************************************************************************************************************************************
TASK [start node containerd service] *************************************************************************************************************************************
ok: [172.18.30.57]
ok: [172.18.30.59]
ok: [172.18.30.56]
ok: [172.18.30.60]
ok: [172.18.30.58]
PLAY [start k8s nginx] ***************************************************************************************************************************************************
TASK [start nginx service] ***********************************************************************************************************************************************
ok: [172.18.30.51]
PLAY [stop k8s master containerd] ****************************************************************************************************************************************
PLAY [stop k8s node containerd] ******************************************************************************************************************************************
PLAY [stop k8s nginx] ****************************************************************************************************************************************************
TASK [stop nginx service] ************************************************************************************************************************************************
changed: [172.18.30.51]
PLAY [restart k8s master containerd] *************************************************************************************************************************************
PLAY [restart k8s node containerd] ***************************************************************************************************************************************
PLAY [restart k8s nginx] *************************************************************************************************************************************************
TASK [restart nginx service] *********************************************************************************************************************************************
changed: [172.18.30.51]
PLAY RECAP ***************************************************************************************************************************************************************
172.18.30.51 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.53 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.54 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.55 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.56 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.57 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.58 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.59 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.60 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@master01 ansible]#
2、启动集群所有服务使用例子
# 启动集群所有服务使用例子
[root@master01 ansible]# ansible-playbook k8s-apps.yaml -t started -e action=started
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [start k8s master containerd] ***************************************************************************************************************************************
TASK [start master containerd service] ***********************************************************************************************************************************
ok: [172.18.30.54]
ok: [172.18.30.55]
ok: [172.18.30.53]
PLAY [start k8s node containerd] *****************************************************************************************************************************************
TASK [start node containerd service] *************************************************************************************************************************************
ok: [172.18.30.59]
ok: [172.18.30.57]
ok: [172.18.30.56]
ok: [172.18.30.60]
ok: [172.18.30.58]
PLAY [start k8s nginx] ***************************************************************************************************************************************************
TASK [start nginx service] ***********************************************************************************************************************************************
ok: [172.18.30.51]
PLAY [stop k8s master containerd] ****************************************************************************************************************************************
PLAY [stop k8s node containerd] ******************************************************************************************************************************************
PLAY [stop k8s nginx] ****************************************************************************************************************************************************
PLAY [restart k8s master containerd] *************************************************************************************************************************************
PLAY [restart k8s node containerd] ***************************************************************************************************************************************
PLAY [restart k8s nginx] *************************************************************************************************************************************************
PLAY RECAP ***************************************************************************************************************************************************************
172.18.30.51 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.53 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.54 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.55 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.56 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.57 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.58 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.59 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.60 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@master01 ansible]#
3、启动master节点容器服务例子
# 启动master节点容器服务例子
[root@master01 ansible]# ansible-playbook k8s-apps.yaml -t k8s-master,started -e action=started
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [start k8s master containerd] ***************************************************************************************************************************************
TASK [start master containerd service] ***********************************************************************************************************************************
ok: [172.18.30.54]
ok: [172.18.30.55]
ok: [172.18.30.53]
PLAY [start k8s node containerd] *****************************************************************************************************************************************
TASK [start node containerd service] *************************************************************************************************************************************
ok: [172.18.30.57]
ok: [172.18.30.59]
ok: [172.18.30.56]
ok: [172.18.30.60]
ok: [172.18.30.58]
PLAY [start k8s nginx] ***************************************************************************************************************************************************
TASK [start nginx service] ***********************************************************************************************************************************************
ok: [172.18.30.51]
PLAY [stop k8s master containerd] ****************************************************************************************************************************************
TASK [stop master containerd service] ************************************************************************************************************************************
changed: [172.18.30.54]
changed: [172.18.30.55]
changed: [172.18.30.53]
PLAY [stop k8s node containerd] ******************************************************************************************************************************************
PLAY [stop k8s nginx] ****************************************************************************************************************************************************
PLAY [restart k8s master containerd] *************************************************************************************************************************************
TASK [restart master containerd service] *********************************************************************************************************************************
changed: [172.18.30.55]
changed: [172.18.30.54]
changed: [172.18.30.53]
PLAY [restart k8s node containerd] ***************************************************************************************************************************************
PLAY [restart k8s nginx] *************************************************************************************************************************************************
PLAY RECAP ***************************************************************************************************************************************************************
172.18.30.51 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.53 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.54 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.55 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.56 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.57 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.58 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.59 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.60 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@master01 ansible]#
4、指定执行nginx启动服务的task内容
# 指定执行nginx启动服务的task内容
[root@master01 ansible]# ansible-playbook k8s-apps.yaml --start-at-task="start nginx service" -e action=started
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [start k8s master containerd] ***************************************************************************************************************************************
PLAY [start k8s node containerd] *****************************************************************************************************************************************
PLAY [start k8s nginx] ***************************************************************************************************************************************************
TASK [start nginx service] ***********************************************************************************************************************************************
ok: [172.18.30.51]
PLAY [stop k8s master containerd] ****************************************************************************************************************************************
TASK [stop master containerd service] ************************************************************************************************************************************
changed: [172.18.30.55]
changed: [172.18.30.54]
changed: [172.18.30.53]
PLAY [stop k8s node containerd] ******************************************************************************************************************************************
TASK [stop node containerd service] **************************************************************************************************************************************
changed: [172.18.30.59]
changed: [172.18.30.56]
changed: [172.18.30.57]
changed: [172.18.30.58]
changed: [172.18.30.60]
PLAY [stop k8s nginx] ****************************************************************************************************************************************************
TASK [stop nginx service] ************************************************************************************************************************************************
changed: [172.18.30.51]
PLAY [restart k8s master containerd] *************************************************************************************************************************************
TASK [restart master containerd service] *********************************************************************************************************************************
changed: [172.18.30.55]
changed: [172.18.30.54]
changed: [172.18.30.53]
PLAY [restart k8s node containerd] ***************************************************************************************************************************************
TASK [restart node containerd service] ***********************************************************************************************************************************
changed: [172.18.30.57]
changed: [172.18.30.59]
changed: [172.18.30.56]
changed: [172.18.30.60]
changed: [172.18.30.58]
PLAY [restart k8s nginx] *************************************************************************************************************************************************
TASK [restart nginx service] *********************************************************************************************************************************************
changed: [172.18.30.51]
PLAY RECAP ***************************************************************************************************************************************************************
172.18.30.51 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.53 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.54 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.55 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.56 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.57 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.58 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.59 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.30.60 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@master01 ansible]#