天天看點

ansible--基本使用(一)

關于 ansible的基本使用

     官方的話不會說,基本都是自己了解的。有誤請指出 謝謝 O(∩_∩)O

     ansible工具,原理就是通過ssh協定遠端連接配接到對方的主機上,将本地的腳本(子產品) 拷貝過去并執行你寫好的指令的一個工具

 考慮到伺服器上yum源大部分不能使用,這裡rpm安裝

連結:https://pan.baidu.com/s/1YfEiWwB-NOXnCG0aXHYEHg

提取碼:kp9l

複制這段内容後打開百度網盤手機App,操作更友善哦--來自百度網盤超級會員V5的分享

主機規劃

    192.168.1.20      centos7.6   主要端

    192.168.1.21      centos7.6   被控端

    192.168.1.22      centos7.6   被控端

環境初始化(所有主機)

#關防護牆
systemctl stop firewalld && systemctl disable firewalld


#檢測虛拟記憶體是否關閉,因為防止容器運作再虛拟記憶體
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config


#不使用本地源,改用阿裡源(雖然慢了點,但不是很缺東西)
cd /etc/yum.repos.d/ && mkdir backup && mv * backup

#拉取阿裡源
wget -O/etc/yum.repos.d/aliyun-yilai.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#(沒有設定虛拟機聯通外網的可能會做不了這步),後續會寫個專門設定網絡的

#清除阿裡雲yum源内網域名
sed -i '/mirrors.cloud.aliyuncs.com/d' /etc/yum.repos.d/aliyun-yilai.repo 
#阿裡雲為了節省外網流量,切換為阿裡雲編輯好的centos鏡像源時會帶有内網鏡像源域名
#外網使用者雖然功能不會受到影響,但是會報Couldn't resolve host 'mirrors.cloud.aliyuncs.com'
#影響速度,故去掉


#重建立立緩存
yum clean all 
yum makecache   #這個會有點慢

           

1. 安裝 ansible (版本2.9.9)

#上傳軟體包
[root@k8s-master01 ~]# ls
ansible.zip

[root@k8s-master01 ~]# pwd
/root

#解壓ansible.zip
unzip ansible.zip

#本地安裝
yum -y localinstall *
           
ansible--基本使用(一)
#登陸配置目錄
[root@k8s-master01 ansible]# cd /etc/ansible/
[root@k8s-master01 ansible]# ls
ansible.cfg  hosts  roles

#檔案含義
ansible.cfg   #ansible配置檔案,預設基本不用動,全注釋
hosts         #主機清單檔案,存放被控端主機資訊, 預設指定的hosts檔案
roles         #自帶的角色目錄,目前為空
           

修改ansible配置

[root@k8s-master01 ansible]# vim ansible.cfg 
#修改
71 host_key_checking = False    #将注釋去掉

#作用: 類似于,在ssh對方主機的時候需要輸入yes,這裡開啟後不需要輸入yes           
ansible--基本使用(一)

添加主機清單清單

[root@k8s-master01 ansible]# vim hosts 
#添加
[web]
192.168.1.20 ansible_ssh_user=root ansible_ssh_pass=123
192.168.1.21 ansible_ssh_user=root ansible_ssh_pass=123
192.168.1.22 ansible_ssh_user=root ansible_ssh_pass=123

#含義
192.168.1.20           #被控端主機ip
ansible_ssh_user=root  #ssh中setup子產品的系統變量,指定為遠端主機的使用者為root
ansible_ssh_pass=123   #同上,為遠端主機的root的密碼
            

1. 使用ping子產品檢測所有主機是否存活

#執行
ansible -i hosts web -m ping


#語句含義
-i hosts web     #指定我們ansible工具針對的是 hosts主機檔案中的  web組下的節點進行操作
-m ping          #-m  指定使用的子產品為ping,用于檢測主機,傳回pong           
ansible--基本使用(一)

列出主機清單

[root@k8s-master01 ansible]# ansible all --list
  hosts (3):
    192.168.1.20
    192.168.1.21
    192.168.1.22
           

#檢視子產品幫助

#檢視詳細幫助
ansible-doc ping

#檢視簡單幫助資訊
ansible-doc -s ping           

2. ansible指令執行的過程說明

1. ansible會先加載配置/etc/ansible/ansible.cfg檔案

2. 然後加載子產品檔案(如上面的ping子產品)

3. ansible會将我們執行的指令或子產品,生成一個臨時的python的腳本檔案,将這個腳本檔案發送到被控端的家目錄/.ansible/tmp/目錄下,并授權執行

4. 執行完成後傳回執行結果,并清除臨時檔案

 我們來抓一下看看

ansible  web -a "sleep 10"           
ansible--基本使用(一)

3. 常用子產品

1. command 子產品

說白了,這個子產品就是相當于我們平時使用指令的習慣,比如ls、cd、pwd、cp、mv操作等等都可以實作

并且該子產品是預設附加的,也就是說。即使你不加-m command 也可以使用

案例 1

#檢視web組所有主機下,/root目錄内容
ansible -i hosts web -a "ls /root"

#-a 是代表要執行的指令,相當于在所有主機上執行的           

傳回

[root@k8s-master01 ansible]# ansible -i hosts web -a "ls /root"
192.168.1.22 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg

192.168.1.20 | CHANGED | rc=0 >>
ansible
ansible.zip
anaconda-ks.cfg
initial-setup-ks.cfg

192.168.1.21 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
           

案例2

[root@k8s-master01 ansible]# ansible -i hosts web -m command -a "ls /root"
192.168.1.22 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg

192.168.1.21 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg

192.168.1.20 | CHANGED | rc=0 >>
ansible
ansible.zip
anaconda-ks.cfg
initial-setup-ks.cfg

#可以看到,效果是一樣的,但是為了養成習慣我們還是加上-m command來使用           

錯誤案例

#command子產品是不支援正則及特殊符号的
[root@k8s-master01 ansible]# ansible -i hosts web -m command -a "echo 11 > /mnt/1.txt"
192.168.1.21 | CHANGED | rc=0 >>
11 > /mnt/1.txt
192.168.1.22 | CHANGED | rc=0 >>
11 > /mnt/1.txt
192.168.1.20 | CHANGED | rc=0 >>
11 > /mnt/1.txt

#好像看起來執行成功了,我們來看看是否成功
[root@k8s-master01 ansible]# ansible -i hosts web -m command -a "ls /mnt"
192.168.1.21 | CHANGED | rc=0 >>

192.168.1.20 | CHANGED | rc=0 >>

192.168.1.22 | CHANGED | rc=0 >>

#可以看到是空的,沒有建立出檔案.因為他不支援特殊符号的使用
有一個解決方法shell子產品           

2. shell 子產品

  shell子產品和command子產品類似,但是卻支援正則及特殊符号。非常好用,一度讓我不再使用command  2333~
[root@k8s-master01 ansible]# ansible -i hosts web -m shell -a "echo 11 > /mnt/1.txt"
192.168.1.22 | CHANGED | rc=0 >>

192.168.1.21 | CHANGED | rc=0 >>

192.168.1.20 | CHANGED | rc=0 >>

[root@k8s-master01 ansible]# ansible -i hosts web -m shell -a "ls /mnt"
192.168.1.21 | CHANGED | rc=0 >>
1.txt
192.168.1.22 | CHANGED | rc=0 >>
1.txt
192.168.1.20 | CHANGED | rc=0 >>
1.txt


#可以看到,很好用哦~           

3.  copy子產品

     一聽名字就知道,肯定是拷貝檔案用的啦。 

     沒啥說的,就是從ansible主機上把檔案發送到被控端主機上

#建立檔案
touch test

#執行指令
ansible -i hosts web -m copy -a "src=test dest=/mnt"

#含義
src=    #本地檔案路徑, 路徑是基于你執行ansible的路徑來的
dest=   #對方主機路徑


#如果你切換了目錄,指令就很長喽(絕對路徑)
#ansible -i /etc/ansible/hosts web -m copy -a "src=/etc/ansible/test dest=/mnt"
           
#執行
ansible -i hosts web -m shell -a "ls /mnt"


#傳回
192.168.1.21 | CHANGED | rc=0 >>
1.txt
test

192.168.1.20 | CHANGED | rc=0 >>
1.txt
test

192.168.1.22 | CHANGED | rc=0 >>
1.txt
test
           

繼續閱讀