1
活動/聚會區 / Re: 2022 2月份 SA@Taipei 2/17(四) kubernetes 帶來的改變以及如何進入 kubernetes 的世界
« 於: 2022-02-08 20:47 »
純推不下
這裡允許您檢視這個會員的所有文章。請注意, 您只能看見您有權限閱讀的文章。
# zypper install nagios monitoring-plugins
# htpasswd2 -c /etc/nagios/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin
# htpasswd2 -bc /etc/nagios/htpasswd.users nagiosadmin test
Adding password for user nagiosadmin
# echo test | htpasswd2 -ic /etc/nagios/htpasswd.users nagiosadmin
Adding password for user nagiosadmin
# systemctl is-enabled nagios
nagios.service is not a native service, redirecting to systemd-sysv-install
Executing /usr/lib/systemd/systemd-sysv-install is-enabled nagios
disabled
# systemctl enable nagios
nagios.service is not a native service, redirecting to systemd-sysv-install
Executing /usr/lib/systemd/systemd-sysv-install enable nagios
# systemctl is-enabled nagios
nagios.service is not a native service, redirecting to systemd-sysv-install
Executing /usr/lib/systemd/systemd-sysv-install is-enabled nagios
enabled
# systemctl restart apache2.service
Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.
# systemctl status apache2.service
12月 31 11:11:50 template start_apache2[7143]: AH00526: Syntax error on line 15 of /etc/apache2/conf.d/nagios.conf:
12月 31 11:11:50 template start_apache2[7143]: Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration
12月 31 11:11:51 template systemd[1]: apache2.service: Main process exited, code=exited, status=1/FAILURE
可以參考# a2enmod mod_access_compat
# apache2ctl -M
會將設定寫入 /etc/apache2/sysconfig.d/loadmodule.conf 檔案內 LoadModule access_compat_module /usr/lib64/apache2-prefork/mod_access_compat.so# systemctl restart apache2.service
# systemctl status apache2.service
# systemctl start nagios
# systemctl status nagios
#yast2 firewall
#vi /etc/nagios/objects/localhost.cfg
註解 HTTP, linux-servers 群組 以及調整 Total Process
# 2014/1/8 edit by sakana, temp disable HTTP monitor
#define service{
# use local-service ; Name of service template to use
# host_name localhost
# service_description HTTP
# check_command check_http
# notifications_enabled 0
# }
# Define an optional hostgroup for Linux machines
#
#define hostgroup{
# hostgroup_name linux-servers ; The name of the hostgroup
# alias Linux Servers ; Long name of the group
# members localhost ; Comma separated list of hosts that belong to this group
# }
# 2014/1/8 edit by sakana change check_local_procs from 250 to 400, 400 to 800
define service{
use local-service ; Name of service template to use
host_name localhost
service_description Total Processes
check_command check_local_procs!400!800!RSZDT
}
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/localhost.cfg
--2016-12-31 12:21:29-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/localhost.cfg
正在查找主機 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
正在連接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... 連上了。
已送出 HTTP 要求,正在等候回應... 200 OK
長度: 5546 (5.4K) [text/plain]
Saving to: ‘localhost.cfg’
100%[=====================================================================================================================>] 5,546 --.-K/s in 0s
2016-12-31 12:21:30 (28.9 MB/s) - ‘localhost.cfg’ saved [5546/5546]
# ls
bin Desktop Documents Downloads inst-sys localhost.cfg Music Pictures Public Templates Videos
# mv localhost.cfg /etc/nagios/objects/localhost.cfg
# vi /etc/nagios/objects/contacts.cfg
修改預設的 e-mail
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email 自己帳號@郵件 ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
#nagios -v /etc/nagios/nagios.cfg
# systemctl restart nagios.service
# zypper install nrpe monitoring-plugins-nrpe
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/templates.cfg
--2016-12-31 16:13:07-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/templates.cfg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19833 (19K) [text/plain]
Saving to: ‘templates.cfg’
100%[=====================================================================================================================>] 19,833 --.-K/s in 0.06s
2016-12-31 16:13:07 (324 KB/s) - ‘templates.cfg’ saved [19833/19833]
# ls
Desktop Documents Downloads Music Pictures Public Templates Videos bin inst-sys templates.cfg
# mv templates.cfg /etc/nagios/objects/templates.cfg
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/commands.cfg
--2016-12-31 16:18:12-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/commands.cfg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7876 (7.7K) [text/plain]
Saving to: ‘commands.cfg’
100%[=====================================================================================================================>] 7,876 --.-K/s in 0s
2016-12-31 16:18:13 (60.2 MB/s) - ‘commands.cfg’ saved [7876/7876]
# ls
Desktop Documents Downloads Music Pictures Public Templates Videos bin commands.cfg inst-sys
# mv commands.cfg /etc/nagios/objects/commands.cfg
#mkdir /etc/nagios/servers
#mkdir /etc/nagios/pcs
#mkdir /etc/nagios/racks
#mkdir /etc/nagios/switches
#mkdir /etc/nagios/projects
#mkdir /etc/nagios/labs
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/linuxPublic.cfg
--2016-12-31 16:37:12-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/linuxPublic.cfg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2652 (2.6K) [text/plain]
Saving to: ‘linuxPublic.cfg’
100%[=====================================================================================================================>] 2,652 --.-K/s in 0s
2016-12-31 16:37:12 (28.7 MB/s) - ‘linuxPublic.cfg’ saved [2652/2652]
# mv linuxPublic.cfg /etc/nagios/objects/
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/windowsPublic.cfg
--2016-12-31 16:38:37-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/windowsPublic.cfg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2345 (2.3K) [text/plain]
Saving to: ‘windowsPublic.cfg’
100%[=====================================================================================================================>] 2,345 --.-K/s in 0s
2016-12-31 16:38:37 (23.4 MB/s) - ‘windowsPublic.cfg’ saved [2345/2345]
# mv windowsPublic.cfg /etc/nagios/objects/
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/switchSimple.cfg
--2016-12-31 16:41:58-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/switchSimple.cfg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3365 (3.3K) [text/plain]
Saving to: ‘switchSimple.cfg’
100%[=====================================================================================================================>] 3,365 --.-K/s in 0s
2016-12-31 16:41:58 (29.6 MB/s) - ‘switchSimple.cfg’ saved [3365/3365]
# mv switchSimple.cfg /etc/nagios/objects/
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/rackHost.cfg
--2016-12-31 16:43:38-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/rackHost.cfg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2716 (2.7K) [text/plain]
Saving to: ‘rackHost.cfg’
100%[=====================================================================================================================>] 2,716 --.-K/s in 0s
2016-12-31 16:43:38 (18.2 MB/s) - ‘rackHost.cfg’ saved [2716/2716]
# mv rackHost.cfg /etc/nagios/objects/
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/windows.cfg
--2016-12-31 16:53:25-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/windows.cfg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4023 (3.9K) [text/plain]
Saving to: ‘windows.cfg’
100%[=====================================================================================================================>] 4,023 --.-K/s in 0s
2016-12-31 16:53:25 (45.9 MB/s) - ‘windows.cfg’ saved [4023/4023]
# mv windows.cfg /etc/nagios/objects/
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/nagios.cfg
--2016-12-31 16:48:01-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/nagios.cfg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 44650 (44K) [text/plain]
Saving to: ‘nagios.cfg’
100%[=====================================================================================================================>] 44,650 --.-K/s in 0.1s
2016-12-31 16:48:01 (397 KB/s) - ‘nagios.cfg’ saved [44650/44650]
# mv nagios.cfg /etc/nagios/
#nagios -v /etc/nagios/nagios.cfg
這邊可能會有警告, 因為 rack 主機我們只檢查主機不檢查服務# systemctl restart nagios.service
# zypper install nrpe monitoring-plugins-nrpe monitoring-plugins
#grep 5666 /etc/services
# vi /etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.100.199
# systemctl start nrpe
# systemctl status nrpe
● nrpe.service - Daemon to remotely execute Nagios plugins
Loaded: loaded (/usr/lib/systemd/system/nrpe.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2016-12-31 19:17:50 CST; 3min 50s ago
Process: 3457 ExecStart=/usr/sbin/nrpe -c /etc/nrpe.cfg -d (code=exited, status=0/SUCCESS)
Main PID: 3461 (nrpe)
Tasks: 1 (limit: 512)
CGroup: /system.slice/nrpe.service
└─3461 /usr/sbin/nrpe -c /etc/nrpe.cfg -d
12月 31 19:17:50 template systemd[1]: Starting Daemon to remotely execute Nagios plugins...
12月 31 19:17:50 template systemd[1]: Started Daemon to remotely execute Nagios plugins.
12月 31 19:17:50 template nrpe[3461]: Starting up daemon
12月 31 19:17:50 template nrpe[3461]: Cannot write to pidfile '/run/nrpe/nrpe.pid' - check your privileges.
# systemctl enable nrpe
# systemctl is-enabled nrpe
enabled
請於Client 端# /usr/lib/nagios/plugins/check_nrpe -H 127.0.0.1
NRPE v2.15
#/usr/lib/nagios/plugins/check_nrpe -H 192.168.100.100
NRPE v2.15
#vi /etc/nagios/nrpe.cfg
加入
#command[check_hda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1
command[check_sda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda1
command[check_sda2]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda2
command[check_ssh]=/usr/lib/nagios/plugins/check_ssh 127.0.0.1
command[check_smtp]=/usr/lib/nagios/plugins/check_smtp 127.0.0.1
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/nrpe.cfg
--2016-12-31 19:49:29-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/nrpe.cfg
正在查找主機 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
正在連接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... 連上了。
已送出 HTTP 要求,正在等候回應... 200 OK
長度: 8474 (8.3K) [text/plain]
Saving to: ‘nrpe.cfg’
100%[===========================================================================>] 8,474 --.-K/s in 0s
2016-12-31 19:49:29 (53.1 MB/s) - ‘nrpe.cfg’ saved [8474/8474]
#vi /etc/nagios/nrpe.cfg
(如果IP有改就改 allowed_hosts= 後面接的IP吧 !! )
覆蓋原來的檔案# mv nrpe.cfg /etc/
# systemctl restart nrpe
# /usr/lib/nagios/plugins/check_nrpe -H 127.0.0.1 -c check_sda2
DISK OK - free space: / 12753 MB (69% inode=97%);| /=5661MB;14732;16573;0;18415
# /usr/lib/nagios/plugins/check_nrpe -H 127.0.0.1 -c check_users
USERS OK - 2 users currently logged in |users=2;5;10;0
# /usr/lib/nagios/plugins/check_nrpe -H 127.0.0.1 -c check_load
OK - load average: 0.00, 0.00, 0.00|load1=0.000;15.000;30.000;0; load5=0.000;10.000;25.000;0; load15=0.000;5.000;20.000;0;
# /usr/lib/nagios/plugins/check_nrpe -H 127.0.0.1 -c check_total_procs
PROCS OK: 208 processes | procs=208;250;300;0;
# /usr/lib/nagios/plugins/check_nrpe -H 192.168.100.100 -c check_sda2
DISK OK - free space: / 12753 MB (69% inode=97%);| /=5661MB;14732;16573;0;18415
# wget https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/linux.cfg
--2016-12-31 20:14:49-- https://raw.githubusercontent.com/sakanamax/LearnAnsible/master/playbook/general/nagios/files/linux.cfg
正在查找主機 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
正在連接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... 連上了。
已送出 HTTP 要求,正在等候回應... 200 OK
長度: 3664 (3.6K) [text/plain]
Saving to: ‘linux.cfg’
100%[===================================================================>] 3,664 --.-K/s in 0s
2016-12-31 20:14:49 (19.2 MB/s) - ‘linux.cfg’ saved [3664/3664]
移動到 /etc/nagios/objects 下# mv linux.cfg /etc/nagios/objects/
# cp /etc/nagios/objects/linux.cfg /etc/nagios/servers/linux100.cfg
#vi /etc/nagios/servers/linux100.cfg
address 192.168.3.129 請改成實際的IP
host_name suseserver129 請改成實際的名稱
確認設定檔是否無誤# nagios -v /etc/nagios/nagios.cfg
# systemctl restart nagios
- name: Install spark with local
hosts: sparkSingle
# 透過 vars_prompt 動態詢問要裝的版本, 預設是 1.4.0
vars_prompt:
- name: "spark_version"
prompt: "Please enter spark version, example 1.4.0"
private: no
default: "1.4.0"
tasks:
- name: Install java, wget with openSUSE Leap
# Use sudo before ansible 1.9
# sudo: True
become: True
# 使用 pkg 加上 {{ item }} 配合後面的 with_item 來安裝多個套件
zypper: name={{ item }}
with_items:
- java-1.7.0-openjdk-devel
- wget
when: ansible_distribution == "openSUSE Leap"
- name: Install java, wget with CentOS
become: True
yum: name={{ item }}
with_items:
- java-1.7.0-openjdk-devel
- wget
when: ansible_distribution == "CentOS"
- name: Install java, wget and scala with Ubuntu
become: True
apt: name={{ item }} update_cache=yes
with_items:
- openjdk-7-jdk
- wget
- scala
when: ansible_distribution == "Ubuntu"
# ----------------------------------------------------------
- name: Get scala
shell: wget http://www.scala-lang.org/files/archive/scala-2.10.1.tgz
when: ansible_distribution == "CentOS" or ansible_distribution == "openSUSE Leap"
- name: Exact source code of scala
shell: tar xvf scala-2.10.1.tgz
when: ansible_distribution == "CentOS" or ansible_distribution == "openSUSE Leap"
- name: Move scala to /usr/lib
become: True
shell: mv scala-2.10.1 /usr/lib
when: ansible_distribution == "CentOS" or ansible_distribution == "openSUSE Leap"
- name: create link for scala
become: True
shell: ln -s /usr/lib/scala-2.10.1/ /usr/lib/scala
when: ansible_distribution == "CentOS" or ansible_distribution == "openSUSE Leap"
- name: touch .bashrc
shell: touch ~/.bashrc
when: ansible_distribution == "CentOS" or ansible_distribution == "openSUSE Leap"
- name: export $PATH to .bashrc
lineinfile: dest=~/.bashrc line="export PATH=$PATH:/usr/lib/scala/bin"
when: ansible_distribution == "CentOS" or ansible_distribution == "openSUSE Leap"
# ----------------------------------------------------------
# 由於沒有在 general option 使用 sudo, 這邊在使用 shell module 抓下來就是使用者自己的身份額非 root
# 這邊用 spark 1.4.0 版本
- name: Download spark with pre-build hadoop
shell: wget http://archive.apache.org/dist/spark/spark-{{ spark_version }}/spark-{{ spark_version }}-bin-hadoop2.6.tgz
# 解開 source code
- name: Exact pre-build source
shell: tar zxvf spark-*.tgz
- name: Rename spark folder
shell: mv ~/spark*/ ~/spark
# 測試 jps 指令
- name: Testing jps command
command: jps
register: jps
- debug: var=jps.stdout_lines
# zypper --gpg-auto-import-keys addrepo -f http://download.opensuse.org/repositories/devel:/languages:/python/openSUSE_Leap_42.1/devel:languages:python.repo
搜尋套件# zypper search -s sphere
Loading repository data...
Reading installed packages...
S | Name | Type | Version | Arch | Repository
--+-----------------+------------+-----------+--------+------------------------------------
| python-pysphere | package | 0.1.8-1.1 | noarch | Python Modules (openSUSE_Leap_42.1)
# zypper install python-pysphere
Loading repository data...
Reading installed packages...
Resolving package dependencies...
The following NEW package is going to be installed:
python-pysphere
1 new package to install.
Overall download size: 891.9 KiB. Already cached: 0 B. After the operation, additional 13.8 MiB will be used.
Continue? [y/n/? shows all options] (y): Y
建立測試 playbook- name: Testing vsphere_guest module in VMware
# 這邊自己習慣控管 VMware 的是用本機
# 配合 hosts 內的 ansible_connection=local
hosts: localhost
# 設定使用 local connection
connection: local
# 這種方式還不錯, 透過提示來輸入相關變數
vars_prompt:
# 這邊的 name 就會是變數的名稱
- name: "vcenter_hostname"
# 設定提示文字
prompt: "Enter vcenter hostname"
# private 設定為 no 會顯示輸入的內容
private: no
# 這邊可以設定預設值
default: "vcsa"
- name: "vcenter_user"
prompt: "Enter vCenter username"
private: no
default: root
- name: "vcenter_pass"
prompt: "Enter vcenter password"
# private 設定為 yes 會顯示輸入的內容
private: yes
- name: "guest_name"
prompt: "Enter guest vm name"
private: no
tasks:
- name: Testing gater facts from vSphere
# 使用 vsphere_guest 來設定相關工作
vsphere_guest:
# vCenter 的 IP 或是 FQDN, 這邊用上面的變數讓使用者輸入
vcenter_hostname: "{{ vcenter_hostname }}"
# Ansible 2.1 以後的版本可以設定 validate_certs, 預設會檢查 SSL certs, 如果是自己架設的vC, 可以設定no
validate_certs: no
# vCenter 上面的使用者名稱, 對應上面的變數
username: "{{ vcenter_user }}"
# vCenter 使用者的密碼, 對應上面的變數
password: "{{ vcenter_pass }}"
# 要針對的 VMware guest 名稱
guest: "{{ guest_name }}"
# 是否要取回 guest 資訊
vmware_guest_facts: yes
> ansible-playbook -v test_vsphere_guest_get_facts.yml
Enter vcenter hostname [vcsa]: YOURIP_FQDN
Enter vCenter username [root]:
Enter vcenter password:
Enter guest vm name: openSUSELeap_SA_Docker
PLAY [Testing vsphere_guest module in VMware] **********************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [Testing gater facts from vSphere] ****************************************
ok: [localhost] => {"ansible_facts": {"hw_eth0": {"addresstype": "manual", "ipaddresses": [“w.x.y.z", "fe80::220:5fff:fe11:1111"], "label": "Network adapter 1", "macaddress": "00:50:56:11:11:11", "macaddress_dash": "00-50-56-11-11-11", "summary": "VM Net"}, "hw_guest_full_name": "Other Linux (32-bit)", "hw_guest_id": "otherLinuxGuest", "hw_interfaces": ["eth0"], "hw_memtotal_mb": 3976, "hw_name": "openSUSELeap_SA_Docker", "hw_power_status": "POWERED ON", "hw_processor_count": 2, "hw_product_uuid": "4222b4d6-ef7b-dc72-8603-9f7cbbfc2737", "module_hw": true}, "changed": false}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
- name: Testing vsphere_guest module in VMware
# 這邊自己習慣控管 VMware 的是用本機
# 配合 hosts 內的 ansible_connection=local
hosts: localhost
# 設定使用 local connection
connection: local
# 這種方式還不錯, 透過提示來輸入相關變數
vars_prompt:
# 這邊的 name 就會是變數的名稱
- name: "vcenter_hostname"
# 設定提示文字
prompt: "Enter vcenter hostname"
# private 設定為 no 會顯示輸入的內容
private: no
# 這邊可以設定預設值
default: "vcsa"
- name: "vcenter_user"
prompt: "Enter vCenter username"
private: no
default: root
- name: "vcenter_pass"
prompt: "Enter vcenter password"
# private 設定為 yes 會顯示輸入的內容
private: yes
- name: "guest_name"
prompt: "Enter guest VM name your want to delete"
private: no
tasks:
- name: Delete VM
# 使用 vsphere_guest 來設定相關工作
vsphere_guest:
# vCenter 的 IP 或是 FQDN, 這邊用上面的變數讓使用者輸入
vcenter_hostname: "{{ vcenter_hostname }}"
# Ansible 2.1 以後的版本可以設定 validate_certs, 預設會檢查 SSL certs, 如果是自己架設的vC, 可以設定no
validate_certs: no
# vCenter 上面的使用者名稱, 對應上面的變數
username: "{{ vcenter_user }}"
# vCenter 使用者的密碼, 對應上面的變數
password: "{{ vcenter_pass }}"
# 要針對的 VMware guest 名稱
guest: "{{ guest_name }}"
#
state: absent
# 如果有使用 force: yes, 就是虛擬機不需要先關機就刪除
# 實際上觀察, 是會先關機然後刪除
force: yes
這樣可以用來刪除指定的 VM- name: Testing vsphere_guest module in VMware
# 這邊自己習慣控管 VMware 的是用本機
# 配合 hosts 內的 ansible_connection=local
hosts: localhost
connection: local
# 這種方式還不錯, 透過提示來輸入相關變數
vars_prompt:
# 這邊的 name 就會是變數的名稱
- name: "vcenter_hostname"
# 設定提示文字
prompt: "Enter vcenter hostname"
# private 設定為 no 會顯示輸入的內容
private: no
# 這邊可以設定預設值
default: "vcsa"
- name: "vcenter_user"
prompt: "Enter vCenter username"
private: no
default: root
- name: "vcenter_pass"
prompt: "Enter vcenter password"
# private 設定為 yes 會顯示輸入的內容
private: yes
- name: "esxi_hostname"
prompt: "Enter esxi hostname"
private: no
- name: "template_name"
prompt: "Enter Template name"
private: no
default: openSUSELeap42.1_Training_Template
tasks:
- name: Create VM from template
# 可以用 with_sequence 方式, %0x 為序號, x 為16進制, 所以我用 10進制 u
# 如果是 %02x 就是 2位數, 例如 server00
# 如果只設定 server%0x , 但是超出1位數, 例如 1 to 10, 最後一個會變成 servera
# stride=2 代表間隔是 2
with_sequence: start=1 end=10 stride=1 format=server%02u
# 用 with_item 正面表列
# with_items:
# - server01
# - server02
# 使用 vsphere_guest 來設定相關工作
vsphere_guest:
# vCenter 的 IP 或是 FQDN, 這邊用上面的變數讓使用者輸入
vcenter_hostname: "{{ vcenter_hostname }}"
# Ansible 2.1 以後的版本可以設定 validate_certs, 預設會檢查 SSL certs, 如果是自己架設的vC, 可以設定no
validate_certs: no
# vCenter 上面的使用者名稱, 對應上面的變數
username: "{{ vcenter_user }}"
# vCenter 使用者的密碼, 對應上面的變數
password: "{{ vcenter_pass }}"
# 要針對的 VMware guest 名稱
guest: "{{ item }}"
# 從範本複製
# 如果有 from_template 就不能設定 state
from_template: yes
# 範本名稱
template_src: "{{ template_name }}"
# 複製範本完是否要開機, 預設是 yes
power_on_after_clone: no
# 要佈署的 esxi 主機
esxi:
datacenter: Lab
hostname: "{{ esxi_hostname }}"
# 目前測試 resource_pool 沒有成功
# resource_pool: "/Resources"
在 VMware 上面的結果- name: Testing vsphere_guest module in VMware
# 這邊自己習慣控管 VMware 的是用本機
# 配合 hosts 內的 ansible_connection=local
hosts: localhost
connection: local
# 這種方式還不錯, 透過提示來輸入相關變數
vars_prompt:
# 這邊的 name 就會是變數的名稱
- name: "vcenter_hostname"
# 設定提示文字
prompt: "Enter vcenter hostname"
# private 設定為 no 會顯示輸入的內容
private: no
# 這邊可以設定預設值
default: "vcsa"
- name: "vcenter_user"
prompt: "Enter vCenter username"
private: no
default: root
- name: "vcenter_pass"
prompt: "Enter vcenter password"
# private 設定為 yes 會顯示輸入的內容
private: yes
- name: "esxi_hostname"
prompt: "Enter esxi hostname"
private: no
tasks:
- name: Reconfig VM
# 'reconfigured' only applies changes to 'vm_cdrom', 'memory_mb', and 'num_cpus' in vm_hardware parameter.
#
# 可以用 with_sequence 方式, %0x 為序號, 10進制是用 u, 所以用 %02u
# 如果是 %02x 就是 2位數, 例如 server00
# 如果只設定 server%0x , 但是超出1位數, 例如 1 to 10, 最後一個會變成 servera
with_sequence: start=1 end=5 stride=1 format=server%02u
# 用 with_item 正面表列
# with_items:
# - server01
# - server02
# 使用 vsphere_guest 來設定相關工作
vsphere_guest:
# vCenter 的 IP 或是 FQDN, 這邊用上面的變數讓使用者輸入
vcenter_hostname: "{{ vcenter_hostname }}"
# Ansible 2.1 以後的版本可以設定 validate_certs, 預設會檢查 SSL certs, 如果是自己架設的vC, 可以設定no
validate_certs: no
# vCenter 上面的使用者名稱, 對應上面的變數
username: "{{ vcenter_user }}"
# vCenter 使用者的密碼, 對應上面的變數
password: "{{ vcenter_pass }}"
# 要針對的 VMware guest 名稱
guest: "{{ item }}"
# 如果有 from_template 就不能設定 state
state: reconfigured
vm_hardware:
memory_mb: 2048
# num_cpus: 4
# 如果設定 force: yes, 就會先關機然後設定再開機
# 有些設定必須在 VM 關機的狀況才能實施
force: yes
# 要佈署的 esxi 主機
esxi:
datacenter: Lab
hostname: "{{ esxi_hostname }}"
另外就是建立 VM 設定檔- name: Testing vsphere_guest module in VMware
# 這邊自己習慣控管 VMware 的是用本機
# 配合 hosts 內的 ansible_connection=local
hosts: localhost
connection: local
# 這種方式還不錯, 透過提示來輸入相關變數
vars_prompt:
# 這邊的 name 就會是變數的名稱
- name: "vcenter_hostname"
# 設定提示文字
prompt: "Enter vcenter hostname"
# private 設定為 no 會顯示輸入的內容
private: no
# 這邊可以設定預設值
default: "vcsa"
- name: "vcenter_user"
prompt: "Enter vCenter username"
private: no
default: root
- name: "vcenter_pass"
prompt: "Enter vcenter password"
# private 設定為 yes 會顯示輸入的內容
private: yes
- name: "esxi_hostname"
prompt: "Enter esxi hostname"
private: no
tasks:
- name: Create New VM
# 可以用 with_sequence 方式, %0x 為序號
# 如果是 %02x 就是 2位數, 例如 server00
# 如果只設定 server%0x , 但是超出1位數, 例如 1 to 10, 最後一個會變成 servera
# stride=2 代表間隔是 2
with_sequence: start=0 end=1 stride=1 format=server%02x
# 用 with_item 正面表列
# with_items:
# - server01
# - server02
# 使用 vsphere_guest 來設定相關工作
vsphere_guest:
# vCenter 的 IP 或是 FQDN, 這邊用上面的變數讓使用者輸入
vcenter_hostname: "{{ vcenter_hostname }}"
# Ansible 2.1 以後的版本可以設定 validate_certs, 預設會檢查 SSL certs, 如果是自己架設的vC, 可以設定no
validate_certs: no
# vCenter 上面的使用者名稱, 對應上面的變數
username: "{{ vcenter_user }}"
# vCenter 使用者的密碼, 對應上面的變數
password: "{{ vcenter_pass }}"
# 要針對的 VMware guest 名稱
guest: "{{ item }}"
#
# 如果有 from_template 就不能設定 state
state: powered_off
#
vm_extra_config:
vcpu.hotadd: yes
mem.hotadd: yes
notes: Create by Ansible
# 這邊的 Folder 必須是在虛擬機器與範本下的 folder, 不是主機與叢集下的 folder
folder: Ansible
resource_pool: "Resources"
vm_disk:
disk1:
size_gb: 10
type: thin
datastore: Local_30.3
#
vm_nic:
nic1:
type: vmxnet3
network: Lab-192-168-100
network_type: standard
#
vm_hardware:
memory_mb: 2048
num_cpus: 2
# osid 可以參考 https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html
osid: sles11_64Guest
scsi: paravirtual
vm_cdrom:
type: "iso"
iso_path: "Local_30.3/ISO/SLE-12-SP1-Server-DVD-x86_64-GM-DVD1.iso"
# VMware 硬體版本
vm_hw_version: vmx-08
# 要佈署的 esxi 主機
esxi:
datacenter: Lab
hostname: "{{ esxi_hostname }}"
#useradd -m consul
建立相關目錄#mkdir -p /opt/consul/bin /opt/consul/data /opt/consul/config
設定權限以及更改擁有人為使用者 consul#chown -R consul /opt/consul/
#chmod -R 700 /opt/consul/
#wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip
#unzip consul_0.6.4_linux_amd64.zip
將 consul 複製到指定的目錄#cp consul /opt/consul/bin
切換使用者 consul#su - consul
編寫設定檔 ( 這邊我們是先試用官方的服務 https://atlas.hashicorp.com/consul ){
"bind_addr": "請填入機器IP",
"atlas_infrastructure": "請填入帳號/請填入名稱",
"atlas_join": true,
"atlas_token": "請填入自己的token",
"bootstrap_expect": 3,
"datacenter": "Hsinchu-NCHC",
"data_dir": "/opt/consul/data",
"log_level": "INFO",
"node_name": "請填入主機名稱",
"encrypt": "請填入加密的資訊",
"server": true
}
> /opt/consul/bin/consul agent -config-dir /opt/consul/config/
目前是用 nohup 方式在背景執行, 然後離開> nohup /opt/consul/bin/consul agent -config-dir /opt/consul/config/ &
停止 consul 的方式( 這樣不會產生 error )/opt/consul/bin/consul leave
---
#########################################################
# Install consul and setup boot
- name: use when conditionals and setup module (facts)
hosts: all
tasks:
# 使用 setup moudule 列出 OS 種類
- name: use setup module to list os distribution
# setup moudle 可以使用 filter 過濾相關內容
setup: filter=ansible_distribution
#########################################################
- name: Install consul , add user and setting up env
# use group
hosts: ConsulHost
sudo: True
tasks:
- name: Add user consul
user: name=consul
- name: Create folder for consul
file: path=/opt/consul/{{ item }} state=directory owner=consul mode=700
with_items:
- bin
- data
- config
- name: Install wget with openSUSE Leap
zypper: name={{ item }}
with_items:
- wget
when: ansible_distribution == "openSUSE Leap"
- name: Install wget with CentOS
yum: name={{ item }}
with_items:
- wget
when: ansible_distribution == "CentOS"
- name: Install wget with Ubuntu
apt: name={{ item }} update_cache=yes
with_items:
- wget
when: ansible_distribution == "Ubuntu"
#-------------------------------------------------------
- name: Get consul 0.6.4 zip
shell: wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip
#-------------------------------------------------------
- name: Unzip consul 0.6.4 zip file
shell: unzip consul_0.6.4_linux_amd64.zip
#-------------------------------------------------------
- name: Copy consul to path
shell: cp consul /opt/consul/bin
#########################################################
# Copy config files with user consul
- name: copy config files with user consul
hosts: ConsulHost
tasks:
- name: use setup module to list os distribution
become_user: consul
become: yes
template: src=templates/atlas_consul.json dest=/opt/consul/config/atlas_consul.json
# 這個部份有待討論如何在背景執行 可能要寫成服務的方式, 目前會斷
# - name: running consul at backgroud
# become_user: consul
# become: yes
# shell: nohup /opt/consul/bin/consul agent -config-dir /opt/consul/config &
{% for host in groups['ConsulHost'] %}
{
"bind_addr": "{{ hostvars[host].ansible_default_ipv4.address }}",
"atlas_infrastructure": "請填入帳號/請填入名稱",
"atlas_join": true,
"atlas_token": "請填入自己的token",
"bootstrap_expect": 3,
"datacenter": "Hsinchu-NCHC",
"data_dir": "/opt/consul/data",
"log_level": "INFO",
"node_name": "{{ hostvars[host].ansible_hostname }}",
"encrypt": "請填入加密的資訊",
"server": true
}
{% endfor %}
# zypper search -s --match-exact docker
Loading repository data...# zypper --gpg-auto-import-keys addrepo -f http://download.opensuse.org/repositories/Virtualization:/containers/openSUSE_Leap_42.1/Virtualization:containers.repo
--gpg-auto-import-keys 是自動匯入 repo的 key# zypper repos
# | Alias | Name | Enabled | GPG Check | Refresh# zypper -n install docker
Loading repository data...# systemctl status docker
docker.service - Docker Application Container Engine# systemctl start docker
確認目前狀態# systemctl status docker
docker.service - Docker Application Container Engine# systemctl is-enabled docker
disabled# systemctl enable docker
確認目前狀態# systemctl is-enabled docker
enabled---
#########################################################
# Install docker package and setup boot with unstable repo in openSUSE Leap 42.1
- name: use when conditionals and setup module (facts)
hosts: all
tasks:
# 使用 setup moudule 列出 OS 種類
- name: use setup module to list os distribution
# setup moudle 可以使用 filter 過濾相關內容
setup: filter=ansible_distribution
#########################################################
- name: Install docker and run service
# use [dockerUnstable] group to install
hosts: dockerUnstable
sudo: True
tasks:
# Add Virtualization:container project repo
- name: Add Virutalization:containers repo
shell: zypper --gpg-auto-import-keys addrepo -f http://download.opensuse.org/repositories/Virtualization:/containers/openSUSE_Leap_42.1/Virtualization:containers.repo
- name: Install docker with openSUSE Leap
zypper: name={{ item }}
with_items:
- docker
- curl
when: ansible_distribution == "openSUSE Leap"
#-------------------------------------------------------
- name: Set docker enable and run
service: name=docker state=started enabled=yes
呵呵~淺談,聽了之後,感覺是深談....一向都是淺談, 這其中一定有啥誤會 :p
cenic_master | success | rc=0 >>
ii ftp 0.17-28 amd64 classical file transfer client
- name: Test shell module
hosts: cenic_master
tasks:
- name: Test shell module
shell: dpkg -l | grep ftp
register: dpkg
- debug: var=dpkg.stdout_lines
PLAY [Test shell module] ******************************************************
GATHERING FACTS ***************************************************************
ok: [cenic_master]
TASK: [Test shell module] *****************************************************
changed: [cenic_master]
TASK: [debug var=dpkg.stdout_lines] *******************************************
ok: [cenic_master] => {
"var": {
"dpkg.stdout_lines": [
"ii ftp 0.17-28 amd64 classical file transfer client",
"ii lftp 4.4.13-1 amd64 Sophisticated command-line FTP/HTTP client programs",
"ii openssh-sftp-server 1:6.6p1-2ubuntu2 amd64 secure shell (SSH) sftp server module, for SFTP access from remote machines"
]
}
}
PLAY RECAP ********************************************************************
cenic_master : ok=3 changed=1 unreachable=0 failed=0
- name: Using Iteration (with_item) to install multiple packages
hosts: cenic_master
# sudo: True 這邊學到 sudo 不一定要放在一般選項內, 也可以放在 task 內
tasks:
- name: install apt packages
# 於 apt moudule 使用 sudo
sudo: True
# 使用 pkg 加上 {{ item }} 配合後面的 with_item 來安裝多個套件
apt: pkg={{ item }} update_cache=yes cache_valid_time=3600
# 配合前面的 {{ item }} 來安裝多套件
with_items:
- openjdk-7-jdk
- wget
- scala
PLAY [Using Iteration (with_item) to install multiple packages] ***************
GATHERING FACTS ***************************************************************
ok: [cenic_master]
TASK: [install apt packages] **************************************************
changed: [cenic_master] => (item=openjdk-7-jdk,wget,scala)
PLAY RECAP ********************************************************************
cenic_master : ok=2 changed=1 unreachable=0 failed=0