ansible遠端控制

2022-06-30 06:30:14 字數 3062 閱讀 4513

環境:一台centos7,一台centos6.5

實驗步驟:

centos7的伺服器上:

.準備工作

1.關閉selinux ,清空缺省防火牆策略,橋接物理網路。

setenforce 0

ifconfig

systemctl restart sshd

iptables -f

2.安裝遠端登陸的依賴關係包。

yum -y install openssl openssl-devel

3.安裝ansible。(確保有離線的ansible軟體包)

mkdir /yum

mv /etc/yum.repos.d/* /yum/

vim /etc/yum.repos.d/yum.repo

[yum]

name=yum

baseurl=file:///root/ansiblerpm

enabled=1

gpgcheck=0

cd ansiblerpm/

rm -rf repodata/

createrepo /root/ansiblerpm/

cdyum -y install ansible

4.刪除衝突環境python2包

rpm -qa | grep python

rpm -e python2-cryptography –nodeps

5.檢視ansible版本,確認安裝成功

ansible --version

6.修改ansible伺服器的配置檔案,鏈結被控伺服器

vim /etc/ansible/hosts

在最後一行新增:

[webserver] ——對方伺服器名稱

192.168.6.112  ——對方ip位址

7.設定金鑰ssh遠端登陸,因為ansible是通過22埠對其他伺服器進行控制的,所有必須要進行金鑰認證,要不然當執行的時候還的進行密碼驗證,而金鑰鏈結是不需要驗證密碼的。

ssh-keygen

ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

ssh 192.168.6.112

8.測試是否可以鏈結被控伺服器。

ansible -m ping 'webserver'

.正式開始

注意:與上邊的題的編號想對應

1.設定客戶機每天晚上11點重啟httpd服務

2.設定客戶機每天凌晨3點清空/tmp目錄

ansible webserver -m cron -a 'hour=3 job="/bin/rm -rf /tmp/*"'

3.設定客戶機建立使用者nginx,並且禁止登入系統

ansible webserver -m user -a 'name=nginx  shell=/sbin/nologin"'

4. 設定客戶機建立賬戶zhangsan,密碼為123.com,加入wheel組

ansible webserver -m user -a 'name=zhangsan password="$1$7dzx5yex$7p1qu9jythoxkevwwbaof0" groups=wheel'

5. 刪除客戶機上的mysql使用者

ansible webserver -m user -a 'name="mysql" state=absent'

6. 為客戶機建立郵件組postfix,gid為1500

ansible webserver -m group -a 'name=postfix gid=1500'

7. 將zhangsan加入郵件組

ansible webserver -m user -a 'name=zhangsan group=postfix'

8. 將客戶機/etc/yum.repos.d/下的所有移動到客戶機的/root目錄下 shell模組實現

ansible webserver -m shell -a 'mv /etc/yum.repos.d/* /root/'

9. 將ansible主機上的docker.repo(自行建立)複製到客戶機的/etc/yum.repos.d目錄下

ansible webserver -m copy -a 'src=/etc/yum.repos.d/yum.repo dest=/etc/yum.repos.d/ mode=0750 owner=root group=root'

10. 修改客戶機/root/install.log的許可權為777

ansible webserver -m file -a 'path=/root/install.log mode=777'

11. 設定客戶機/etc/httpd/conf/httpd.conf軟連線到/root/目錄下

12. 設定客戶機的httpd服務隨系統啟動,且開啟

13. 編寫乙個打包/var/logs目錄的shell指令碼,然後為客戶機執行它。

vim haha.sh

新增;#!/bin/bash

tar zcf /root/rizhi.tar.gz /var/log

ansible webserver -m script -a '/root/haha.sh'

14. 重定向客戶機磁碟空間佔用量到ansible機器的/root/disk.log檔案內

ansible webserver -a 'df -ht' > /root/disk.log

cat disk.log

Ansible流程控制

在開發中,經常會編寫一些流程控制相關的邏輯,如條件判斷 迴圈等,ansible也提供了對條件判斷 迴圈等的操作。ansible的模板配置是基於jinja2的,如變數的定義 關於when的更多示例 hosts proxy tasks name deploying to development.debu...

ansible 流程控制和roles

模組 yum 安裝 yum模組 pip 安裝python相關模組 service 啟動服務 enabled 設定開機自啟動 不能都是 那也會導致每分鐘都執行相同的命令 會撐爆伺服器 group 建立組 user 使用者 ad hoc 這些模組是這樣稱呼的 yaml 傳參 動態的 yaml ansib...

TeamViewer 遠端控制

因為公司的電腦有加密系統,因此不好傳檔案,而且傳檔案也是亂碼的,因此想要在家裡也能工作,只能遠端登入公司的電腦進行工作了!因此下了個teamviewer。teamviewer是乙個遠端控制軟體,它最大的優點就是不需要控制和接受控制的任何一方擁有外網ip。而且還能進行傳檔案,還有會議室等強大的功能!3...