ansible使用script模塊在受控機上執行腳本(ansible2.9.5)


一,ansible的script模塊的用途

script 模塊用來在遠程主機上執行 ansible 管理主機上的腳本,

即:腳本一直存在於 ansible 管理主機本地,

不需要手動拷貝到遠程主機后再執行

 

說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest

         對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/

說明:作者:劉宏締 郵箱: 371125307@qq.com

 

二,script模塊的應用例子:

1,用腳本在受控機上切換目錄並查看當前目錄:

[liuhongdi@centos8 ~]$ vi /home/liuhongdi/ansible/chandpwd.sh

內容:

cd /data/web/think_www;
pwd;

執行:

[root@centos8 ~]# ansible yujian -m script -a '/home/liuhongdi/ansible/chandpwd.sh'

 

2,一個常用的例子:在受控機上發布git代碼:

編寫腳本

[liuhongdi@centos8 ~]$ vi /home/liuhongdi/ansible/gitpubwww.sh

內容:

cd /data/site/think_www;
echo "---------------------------------------git status:\n";
git status;
echo "---------------------------------------git pull:\n";
git pull origin master;
echo "---------------------------------------git status:\n";
git status;

執行:

[root@centos8 ~]# ansible yujian -m script -a '/home/liuhongdi/ansible/gitpubwww.sh'  --become  --become-method=sudo --become-user=root

 

3,根據文件判斷是否需要執行腳本?

creates參數 :使用此參數指定一個遠程主機中的文件,當指定的文件存在時,就不執行對應腳本

removes參數 :使用此參數指定一個遠程主機中的文件,當指定的文件不存在時,就不執行對應腳本

[root@centos8 ~]# ansible yujian -m script -a 'removes=/root/isgit.txt /home/liuhongdi/ansible/gitpubwww.sh'  --become  --become-method=sudo --become-user=root
121.122.123.47 | SKIPPED

因為刪除文件不成功,所以不執行

 

[root@centos8 ~]# ansible yujian -m script -a 'creates=/root/isgit.txt /home/liuhongdi/ansible/gitpubwww.sh'  --become  --become-method=sudo --become-user=root
121.122.123.47 | CHANGED => {
    "changed": true,
...

因為文件可以創建,所以成功執行

 

三,查看ansible的版本

[root@centos8 liuhongdi]# ansible --version
ansible 2.9.5

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM