ansible 常用模組 續(三)

2021-10-11 02:58:54 字數 3787 閱讀 9688

用的不多,好像做時間同步的時候用了下

1. 建立週期任務

#每分鐘執行一次輸出

ansible all -m cron -a 'minute=* job="/usr/bin/wall hello world" name=test-cron-job'

#引數說明

minute=* #每分鐘執行一次

weekday=* #工作日,這裡可以寫1,2,3,4,5,6,7 ,表示週幾執行

job="/usr/bin/wall warning" #要執行的任務

name=test-cron-job #關於這個job任務的名稱

#檢視週期任務

[root@web1 ansible]# crontab -l

#ansible: test-cron-job

* * * * * /usr/bin/wall warning

2. 隔離週期任務

#停止,但不刪除週期任務

ansible all -m cron -a 'disabled=true job="/usr/bin/wall hello world" name=test-cron-job'

#檢視任務

[root@web1 ansible]# crontab -l

#ansible: test-cron-job

#* * * * * /usr/bin/wall hello world

3. 恢復週期任務

#啟動週期任務

ansible all -m cron -a 'disabled=false job="/usr/bin/wall hello world" name=test-cron-job'

[root@web1 ansible]# crontab -l

#ansible: test-cron-job

* * * * * /usr/bin/wall hello world

4. 刪除週期任務

ansible all -m cron -a 'job="/usr/bin/wall hello world" name=test-cron-job state=absent'
主機上必須要有yum源可用,用於主機上批量安裝服務

#環境yum不太全,沒用過,自己玩玩就好

#安裝httpd

#檢視是否執行成功

ansible -i hosts web -m shell -a "echo $?"

#檢視所有安裝的包

ansible -i hosts web -m yum -a "list=installed"

#解除安裝安裝的httpd服務

#檢視是否解除安裝

#引數說明

present #預設安裝

installed #檢視已安裝的包

latest #安裝最新的包吧

removed #刪除指定的包

其他案例

#安裝多個包(逗號隔開)

#解除安裝多個

#使用本地的乙個rpm包安裝到其他所有主機

mkdir /data

#將軟體包放入/data目錄

ansible all -m copy -a "src=/data/rpm包 dest=/root"

ansible all -m yum -a "name=/root/rpm包 disable_gpg_check=yes"

#這裡可以忽略gpg檢測

#更新快取

ansible all -m yum -a "update_cache=yes"

#安裝監控工具

ansible all -m yum -a "name=dstat update_cache=yes"

在部署服務的時候用來啟動、停止、重啟服務用的

#啟動服務(httpd) 並設定為開啟自啟

#檢視執行埠

ansible -i hosts web -m shell -a "ss -ntl | grep 80"

#停止服務

#重啟服務

ansible -i hosts web -m service -a "name=httpd state=restartd"

這還用說嗎 (~﹃~)~zz

#建立使用者

ansible -i hosts web -m user -a 'name=nginx shell=/sbin/nologin system=yes home=/var/nginx groups=root,bin uid=80 comment="nginx service"'

#引數說明

shell=/sbin/nologin #是否允許登陸

system=yes #是否為系統使用者

home=/var/nginx #指定家目錄

groups=root #指定使用者組為root,bin兩個組

uid=80 #uid值

comment="nginx service" #使用者描述資訊 通過cat /etc/passwd | grep nginx檢視

#檢視使用者id

ansible -i hosts web -a 'getent passwd nginx'

#刪除使用者(remove=yes 是否一起刪除家目錄)

ansible -i hosts web -m user -a "name=nginx state=absent remove=yes"

#建立組

ansible -i hosts web -m group -a "name=nginx system=yes gid=80"

#新增nginx使用者

ansible -i hosts web -a "getent group nginx"

#同時刪除使用者、組

ansible -i hosts web -m group -a "name=nginx state=absent"

Ansible常用模組

1.ping模組2.ansible command模組是ansible預設模組,主要用於執行linux基礎命令,可以執行遠端伺服器命令執行 任務執行等操作。但command模組不支援變數 重定向 管道符等,這些操作需要用shell模組執行 command模組使用詳解 chdir 執行命令前,切換到目...

ansible常用模組

1.setup 該模組主要用於收集資訊,是通過呼叫facts元件來實現的 ansible doc s setup ansible mysql m setup 檢視mysql伺服器上所有資訊檢視遠端主機基本資訊 ansible all m setup2.ping測試遠端主機執行狀態 ansible a...

Ansible 常用模組

ansible是基於python開發,集合了眾多運維工具 puppet cfengine chef func fabric 的優點 具有批量系統配置 批量程式部署 批量執行命令等功能。是基於模組工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所執行的模組,ansible只是提供一種...