無須部署agent,經過ssh進行管理
流行的自動化運維工具:https://github.con/ansible/ansible
ansible (so easy) 500如下服務器
saltstack (比較複雜) 1000到4萬服務器
puppet (超級複雜) 只有很老企業在用
可視化運維(主要用在可視化部署)
持續構建,能夠和git,svn結合(存放開發代碼的倉庫)
可結合ssh實現可視化運維
可結合ansible實現可視化運維
#禁止非root用戶查看Ansible管理服務器端/etc/hosts文件
[root@ansible ~]# ll /etc/hosts
-rw-r--r--. 1 root root 180 9月 9 00:38 /etc/hosts
[root@ansible ~]# chmod 600 /etc/hosts
#禁止非root用戶查看Ansible的主機清單配置文件
[root@ansible ~]# ll /etc/ansible/hosts
-rw-r--r-- 1 root root 87 9月 9 21:59 /etc/ansible/hosts
[root@ansible ~]# chmod 600 /etc/ansible/hosts
/usr/local/python/bin/ansible-doc -l(查看總幫助)
/usr/local/python/bin/ansible-doc -s shell(查看shell模塊的幫助)
/usr/local/python/bin/ansible-doc -s raw
須要安裝yun -y install net-toolsvimpython
關閉防火牆:systemctl stop firewalld 關閉防火牆開機啓動:systemctl disable fierwalldlinux
關閉selinuxnginx
7.5yum倉庫全能夠用,本地的須要本身手動打開c++
yum -y install epel-releasegit
yum -y install ansible (自動安裝sshpass軟件包)github
yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-devel unzip zlib-devel zlib openssl-devel openssl libffi-develweb
(須要雲yum)shell
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgzvim
tar xf Python-3.5.2.tgz -C /usr/srccentos
cd /usr/src/Python-3.5.2
./configure --prefix=/usr/local/python
make && make install
ln -s /usr/local/python/bin/python3 /usr/bin/python3(製做軟連接)
which python3(查看命令是否存在)
python3 -V(查詢python版本)
ln -s /usr/local/python/bin/ansible /usr/local/bin(製做軟連接)
which ansible(查看命令是否存在)
ansible --version(查看ansible版本)
經過pip安裝的ansible是沒有配置文件的
mkdir -p /etc/ansible(默認沒有,須要手動建立)
vim /etc/ansible/hosts(默認沒有,須要手動建立)
ansible -i 主機或主機組 -m 指定模塊 -a 命令 (-i指定配置文件,不寫就默認路徑下/etc/ansible/hosts,-m指定模塊,-a發佈命令)
(對方須要有python包,發佈命令)
ansible nginx -m command -a 'hostname -I'
(分發模塊內容格式,nginx是模塊名,-a是條件,-m command是調用ansible裏面的模塊發佈命令用)ansible client2 -m command -a 'hostname -I'
(分發單個主機格式,client2是主機名,-a是條件,-m command是調用ansible裏面的模塊發佈命令用)ansible client1 -m command -a 'hostname -I'
(分發單個主機格式,client1是主機名,-a是條件,-m command是調用ansible裏面的模塊發佈命令用)ansible client1:client2 -m command -a 'hostname -I'
(分發多個主機格式,client1:client2是主機名,-a是條件,-m command是調用ansible裏面的模塊發佈命令用)ansible all -m command -a 'hostname -I'
(all是分發全部主機格式,-a是條件,-m command是調用ansible裏面的模塊發佈命令用)
(對方須要有python包)
ping模塊檢查服務器是否鏈接正常,ping模塊不須要-a指定參數
ansible all -m ping (ansible的ping模塊格式)
(對方須要有python包)
shell模塊支持管道符格式
ansible all -m shell -a 'echo test | grep t'shell模塊支持重定向格式
ansible all -m shell -a "echo bb >> /tmp/testansible"shell模塊支持awk格式
ansible all -m shell -a "cat /etc/passwd | awk -F":" '{print $1}'" (若是遇到特殊符號須要加入\轉義)
(不依賴python包)
yum -y install libselinux-python(傳送失敗的話說明對方沒有這個支持包)
ansible all -m copy -a 'src=/root/xin dest=/tmp'
(src源文件,dest目標位置,對方沒有目錄模塊自動建立)
ansible all -m copy -a 'src=/root/xin dest=/tmp backup=yes'
(src源文件,dest目標位置,backup=yes覆蓋同時是否備份源文件)
ansible all -m copy -a 'src=/root/xin dest=/tmp owner=nobody group=nobody mode=0600'
(owner=屬主是誰,group=屬組是誰,mode=它權限)
ansible all -m script -a "/service/scripts/auto_nginx.sh" (6版本必須/bin/sh運行腳本,7版本直接絕對路徑啓動腳本便可)
Ansible中的cron模塊用於定義任務計劃。主要包括兩種狀態(state)
#添加定時任務計劃,在全部被管理的主機裏每十分鐘輸出hello字符串,定時任務描述爲test cron job
[root@ansible ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'
Web02 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test cron job"
]
}
Web01 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test cron job"
]
}
[root@ansible ~]# ansible all -m shell -a 'crontab -l'
Web01 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/echo hello
Web02 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/echo hello
#刪除描述爲test cron job的定時任務
[root@ansible ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent'
Web02 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
Web01 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
[root@ansible ~]# ansible all -m shell -a 'crontab -l'
Web02 | SUCCESS | rc=0 >>
Web01 | SUCCESS | rc=0 >>
#給Web01服務器上的普通用戶yunjisuan添加一個定時任務
[root@ansible ~]# ansible Web01 -m shell -a 'id yunjisuan'
Web01 | SUCCESS | rc=0 >>
uid=1000(yunjisuan) gid=1000(yunjisuan) 組=1000(yunjisuan)
[root@ansible ~]# ansible Web01 -m cron -a 'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan"'
Web01 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"yunjisuan cron job"
]
}
[root@ansible ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'
Web01 | SUCCESS | rc=0 >>
#Ansible: yunjisuan cron job
*/10 * * * * /bin/echo hello
[root@ansible ~]# ansible Web01 -m cron -a 'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan" state="absent"'
Web01 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
[root@ansible ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'
Web01 | SUCCESS | rc=0 >>
利用yum模塊安裝軟件包,雖然能被shell模塊替代,可是用yum模塊更顯專業一些
用戶管理模塊。管理用戶帳號
#在Web02上建立一個普通用戶yunjisuan,並設置用戶的密碼爲123123
[root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=123123 shell=/bin/bash home=/home/yunjisuan'
Web02 | SUCCESS => {
"changed": true,
"comment": "welcom to yunjisuan",
"create_home": true,
"group": 1066,
"groups": "wheel",
"home": "/home/yunjisuan",
"name": "yunjisuan",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1066
}
[root@ansible ~]# ansible Web02 -m shell -a 'tail -1 /etc/passwd'
Web02 | SUCCESS | rc=0 >>
yunjisuan:x:1066:1066:welcom to yunjisuan:/home/yunjisuan:/bin/bash
[root@ansible ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'
Web02 | SUCCESS | rc=0 >>
yunjisuan:123123:17783:0:99999:7::: #密碼竟然是明文!!!
利用ansible的user模塊狀態用戶時要注意在password參數的後邊添加密文,不然不能登錄用戶
經過Python的pip程序安裝passlib便可爲密碼加密
#安裝Python2的pip工具,並經過pip工具安裝Python的加密模塊來給密碼加密
[root@ansible ~]# yum -y install epel-release
[root@ansible ~]# yum -y install python2-pip
[root@ansible ~]# pip install passlib
#生成密文密碼
[root@ansible ~]# python -c "from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())"
Password: #輸入你想要加密的密碼
$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 #加密後的密碼
#刪除以前建立的yunjisuan用戶,並刪除它的家目錄
[root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan state=absent remove=true'
Web02 | SUCCESS => {
"changed": true,
"force": false,
"name": "yunjisuan",
"remove": true,
"state": "absent"
}
#繼續在Web02上建立yunjisuan用戶
[root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 shell=/bin/bash' home=/home/yunjisuan'
[root@ansible ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'
Web02 | SUCCESS | rc=0 >>
yunjisuan:$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1:17783:0:99999:7::: #終於密文了
Ansible中使用setup模塊收集,查看被管理主機的facts(facts是Ansible採集被管理主機設備信息的一個功能)。每一個被管理主機在接收並運行管理命令以前,都會將本身的相關信息(操做系統版本,IP地址等)發送給控制主機
#查看遠程主機的facts信息
[root@ansible ~]# ansible Web01 -m setup | head
Web01 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.200.184"
],
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:fe77:16ad"
],
"ansible_apparmor": {
"status": "disabled"
(playbook能夠把ansible的模塊進行組合)
ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin (優先製做軟連接)
shell模塊支持不少模式,copy模塊分發文件或目錄,register模塊輸出命令運行結果,nginx_conf配置下發並檢測,vars自定義變量,setupvars內置變量,filevars可變配置文件
vim test_shell[copy,register,nginx_conf,vars,setupvars,filevars].yaml (執行模板)
ansible-playbook test_shell[copy,register,nginx_conf,vars,setupvars,filevars].yaml (執行配置文件)
咱們能夠使用ansible all -m setup | less (查看ansible內置變量)
vim if.j2 (下發配置文件模板樣式)
vim /tmp/if.j2
{% if PORT %} #if PORT存在
ip=0.0.0.0:{{ PORT }}
{% else %} #不然的話
ip=0.0.0.0:80
{% endif %} #結尾
vim test_ifvars.yaml
---
- hosts: all
gather_facts: True #開啓系統內置變量
vars:
- PORT: 90 #自定義變量
tasks:
- name: jinja2 if test
template: src=/tmp/if.j2 dest=/root/test
ansible-playbook test_ifvars.yaml (下達分發命令)
若是咱們將變量PORT值爲空的話,就會進入else不然的80端口
---
- hosts: all
gather_facts: True
vars:
- PORT: #置空
tasks:
- name: jinja2 if test
template: src=/tmp/if.j2 dest=/root/test
---
- hosts: all
gather_facts: True #開啓系統變量
vars:
- myname: "yunjisuan" #自定義變量
tasks:
- name: template test
template: src=/tmp/test dest=/root/test #使用template下發可變配置文件
配置文件若是使用copy模塊去下發的話,那配置都是同樣的;
若是下發的配置文件裏有可變的配置,須要用到template模塊。
---
- hosts: all
tasks:
- name: test register
shell: echo "welcome to yunjisuan"
register: print_result #將以前命令的輸出結果保存在變量print_result裏
- debug: var=print_result #將變量的值做爲debug輸出出來。
咱們在用playbook進行ansible模塊操做的時候,並無命令的執行結果輸出,默認被隱藏了
咱們能夠經過register模塊追加輸出命令的執行結果
---
- hosts: all
vars: #定義變量
- name: "yunjisuan" #第一個name變量
age: "3" #第二個age變量
tasks:
- name: "{{ name }}" #{{}}兩對大括號引用變量,變量名兩頭空格
shell: echo "myname {{ name }},myage {{ age }}"
register: var_result
- debug: var=var_result
特別提示:
引用變量須要在雙引號中引用。
在使用自定義變量時,咱們要特別注意不要和系統的內置保留變量同名,容易引起問題。
Found variable using reserved name: name #name是一個保留的內置變量,咱們在自定義時不能用,會有警告
---
- hosts: all
gather_facts: True #使用ansible內置變量
tasks:
- name: setup var
shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}" >> /tmp/test
- name: setup var2
shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
register: var_result
- debug: var=var_result
#python取變量方法
a = [1,3,5] --> a [0]=1 #這樣取值就不會帶[]
a = {"sl":123123,"sl1"123321} --> a[sl]=123123 #這樣取值就不會帶[]
--- #開頭三個小-開頭
- hosts: webB
tasks:
- name: test
shell: echo "welcome to yunjisaun" >> /tmp/username
- name: test2
shell: echo "welcome to yunjisuan" >> /tmp/username
模板說明:
--- #開頭必須有三個小-,頂格寫
- hosts: #正文配置代碼的第一級,必須有兩個空格(-佔一個空格位)
- host: webB #webB是host參數的值,值和hosts:之間要有一個空格
tasks: #tasks:表示接下來要執行的具體任務
- name: #相對於tasks再多縮進兩個格(-佔一個空格位),表示屬於tasks的下一級
- name: test #test只是要執行的具體命令的名字能夠隨便寫。name:後仍是有一個空格要注意
shell: #表示調用shell模塊執行命令相對於tasks仍舊要多縮進兩個空格
shell: echo "xxx" >> xxx #shell:後邊仍是要有個空格,須要注意。
---
- hosts: all
tasks:
- name: test copy
copy: src=/tmp/copy_test dest=/tmp/
模板說明:
--- #開頭必須有三個小-,頂格寫
- hosts: #正文配置代碼的第一級,必須有兩個空格(-佔一個空格位)
tasks: #tasks:表示接下來要執行的具體任務
- name: test #test只是要執行的具體命令的名字能夠隨便寫。name:後仍是有一個空格要注意
copy: src=/tmp/copy_test dest=/tmp/ #把源文件拷貝到對方的位置
vim nginx.j2
worker_processes {{ ansible_processor_count }}; #可變的參數
#實戰下發可執行動做的可變的nginx配置文件
---------------------------------------------------------------------------
vim test_nginxvars.yaml
---
- hosts: all
gather_facts: True #開啓系統內置變量
tasks:
- name: nginx conf
template: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.conf
notify:
- reload nginx #下發通知給handlers模塊執行名字叫作reload nginx的動做
handlers: #定義動做
- name: reload nginx #動做的名字
shell: /usr/local/nginx/sbin/nginx -s reload
ansible-playbook test_nginxvars.yaml (執行分發命令)
---
- hosts: all
tasks:
- name: copy nginx.conf
copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
- name:
shell: /usr/local/nginx/sbin/nginx -t
register: nginx_result
- debug: var=nginx_result
#操做示例-->遠程批量分發並自動部署nginx
#全部被管理端須要掛載光盤,並建立本地yum配置文件
auto_nginx.sh #自動安裝nginx腳本
fenfa.sh #批量分發腳本
nginx-1.10.2.tar.gz #nginx源碼包
#!/bin/sh
#nginx install shell scripts
test -d /media/cdrom || mkdir -p /media/cdrom
mount /dev/sr0 /media/cdrom &>/dev/null
yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null
test -d /service/scripts || exit 3
cd /service/scripts/
tar xf nginx-1.10.2.tar.gz -C /usr/src/
cd /usr/src/nginx-1.10.2/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null
make &>/dev/null
make install &>/dev/null
exit 0
#源碼包和安裝腳本的批量分發腳本
#!/bin/sh
#批量分發腳本
Group=$1
ansible $Group -m copy -a "src=/service/scripts/ dest=/service/scripts/"
ansible $Group -m script -a "/service/scripts/auto_nginx.sh"
4.1 ansible all -m copy -a 'src=/root/xin.sh dest=/root'
4.2 ansible all -m user -a 'name=yunjisuan shell=/sbin/nologin createhome=no'
4.3 ansible all -m shell -a 'echo "123123" | pass --stdin root'
4.4 ansible all -m user -a 'name=benet password=321321 shell=/bin/bash'
4.5 ansible all -m cron -a 'job="/bin/echo xin > /tmp/sl" name="test cron job"'
4.6 ansible all -m shell -a 'vmstat'