前一篇我們提到了一些 Ad-Hoc 指令,而今天的重點會如下:
首先是使用者/使用者群組的管理
ansible app -b -m group -a "name=admin state=present"
ansible app -b -m group -a "name=admin state=present" -i hosts -u vagrant
vagrant ssh app1
cat /etc/group
ansible app -b -m group -a "name=admin state=absent" -i hosts -u vagrant
ansible app -b -m user -a "name=johndoe group=admin createhome=yes" -i hosts -u vagrant
再來是檔案/目錄的操作
檔案目錄有什麼好講的,不就是複製、搬移而已嗎?
我們將針對檔案目錄說明以下的幾個功能:
取得檔案相關的資訊
ansible multi -m stat -a "path=/etc/environment" -i hosts -u vagrant
複製檔案到 Server 上
ansible multi -m copy -a "src=/etc/hosts dest=/tmp/hosts" -i hosts -u vagrant
從 Server 上取得檔案
ansible app -m fetch -a "src=/tmp/hosts dest=/tmp" -i hosts -u vagrant
建立檔案與目錄
主要是會透過 file 這個 module,比方說建立目錄的指令就會如下:
ansible multi -m file -a "dest=/tmp/test mode=644 state=directory" -i hosts -u vagrant
如上圖,其實就是在建立 /tmp/test 這個目錄,而且設定只有自己可以讀寫,其他人只能讀
這部份的觀念如果不熟悉的,可以再翻翻鳥哥的網站或書
刪除檔案與目錄
ansible multi -m file -a "dest=/tmp/test state=absent" -i hosts -u vagrant
也許你會想說為什麼這些東西看起來內容不是很多
以上就是今天小知識的部份,下次將繼續介紹