场景:自己装了5台虚拟机,命令配置感觉有点小麻烦,干脆写个剧本,一次搞定,下次还能用。
注意:编写playbook时参考网址请到开源镜像站找到具体yum地址配置,然后替换就可以了。
1、创建以下目录中三个文件,目录自定义。
[student@master01 ~]# sudo ls -ll /opt/ansible/
total 8
-rw-r--r-- 1 root root 803 Dec 30 17:31 ansible.cfg
-rw-r--r-- 1 root root 50 Dec 29 21:53 inventory
drwxr-xr-x 2 root root 53 Dec 30 17:43 playbook
[student@master01 ~]# sudo ls -ll /opt/ansible/playbook/
total 8
-rw-r--r-- 1 root root 579 Dec 29 22:07 create_user.yml
-rw-r--r-- 1 root root 756 Dec 30 17:43 k8s_yum_file.yml
2、编写ansible.cfg文件。
[student@master01 ~]# sudo cat /opt/ansible/ansible.cfg
# Since Ansible 2.12 (core):
......
[defaults]
inventory = /opt/ansible/inventory
remote_user = student
ask_pass = false
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
指令 | 描述 |
---|---|
inventory | 指定清单文件的路径 |
remote_user | 要在受管主机上登录的用户的名称;未指定则使用当前用户的名称。 |
ask_pass | 是否提示输入SSH密码。如果使用SSH公钥身份验证,则可以是false。 |
become | 连接后是否自动在受管主机上切换用户(通常为root)。也可以使用play来指定。 |
become_method | 如何切换用户(通常为sudo,这也是默认设置,但也可选择su)。 |
become_user | 要在受管主机上切换到的用户(通常是root,这也是默认值)。 |
become_ask_pass | 是否需要为become_method提示输入密码。默认为false。 |
2、编写inventory清单。
[student@master01 ~]# sudo /opt/ansible/inventory
[client]
master01
master02
master03
node01
node02
3、编写playbook。
[student@master01 ~]# sudo cat /opt/ansible/playbook/k8s_yum_file.yml
---
- name: Configure the yum file
hosts: client
tasks:
- name: add yum repo
yum_repository:
file: kubernetes
name: kubernetes
description: kubernetes
baseurl: https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled: yes
gpgcheck: yes
gpgkey: https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude: kubelet kubeadm kubectl
state: present
4、运行playbook。
[student@master01 ansible]$ ansible-playbook playbook/k8s_yum_file.yml
PLAY [Configure the yum file] ***************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
......
PLAY RECAP **********************************************************************************************************
master01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
master02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
master03 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5、验证。
[student@master01 ansible]$ sudo cat /etc/yum.repos.d/kubernetes.repo
[kubernetes]
baseurl = https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled = 1
exclude = kubelet kubeadm kubectl
gpgcheck = 1
gpgkey = https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
name = kubernetes
[student@master01 ansible]$ ssh student@master02 "cat /etc/yum.repos.d/kubernetes.repo"
[kubernetes]
baseurl = https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled = 1
exclude = kubelet kubeadm kubectl
gpgcheck = 1
gpgkey = https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
name = kubernetes
文章评论