Ansible 常用模塊介紹


Ansible 常用模塊介紹

簡介:模塊也稱為task,是在ansible中時間在執行的。

ping 模塊

檢查指定節點機器是否能連通,用法很簡單,不涉及參數。如果被檢測的主機在線,則返回pong

[root@localhost ~]# ansible 192.168.137.102 -m ping
192.168.137.102 | SUCCESS => {
   "changed": false,
   "ping": "pong"
}

 

setup模塊

setup模塊用於收集遠程主機的一些基本信息。

常用參數:

filter :用於進行條件過濾。如果設置,僅返回匹配過濾條件的信息。

[root@localhost ~]# ansible 192.168.137.102 -m setup
# 不加選項會返回所有的信息。信息太多了 這里就不貼出來了。

#獲取ip地址
[root@localhost ~]# ansible 192.168.137.102 -m setup -a "filter=ansible_all_ipv4_addresses"
192.168.137.102 | SUCCESS => {
   "ansible_facts": {
       "ansible_all_ipv4_addresses": [
           "192.168.137.102"
      ]
  },
   "changed": false
}

#獲取是什么系統
[root@localhost ~]# ansible 192.168.137.102 -m setup -a "filter=ansible_distribution"
192.168.137.102 | SUCCESS => {
   "ansible_facts": {
       "ansible_distribution": "CentOS"
  },
   "changed": false
}

#其他常用的信息。
ansible_all_ipv4_addresses:僅顯示ipv4的信息。
ansible_devices:僅顯示磁盤設備信息。
ansible_distribution:顯示是什么系統,例:centos,suse等。
ansible_distribution_major_version:顯示是系統主版本。
ansible_distribution_version:僅顯示系統版本。
ansible_machine:顯示系統類型,例:32位,還是64位。
ansible_eth0:僅顯示eth0的信息。
ansible_hostname:僅顯示主機名。
ansible_kernel:僅顯示內核版本。
ansible_lvm:顯示lvm相關信息。
ansible_memtotal_mb:顯示系統總內存。
ansible_memfree_mb:顯示可用系統內存。
ansible_memory_mb:詳細顯示內存情況。
ansible_swaptotal_mb:顯示總的swap內存。
ansible_swapfree_mb:顯示swap內存的可用內存。
ansible_mounts:顯示系統磁盤掛載情況。
ansible_processor:顯示cpu個數(具體顯示每個cpu的型號)。
ansible_processor_vcpus:顯示cpu個數(只顯示總的個數)。

 

command 模塊

command 模塊可以幫助我們在遠程主機上執行命令,使用的時候可以不用 加 -m 指定。command 是ansible 默認使用的模塊。 (可以在配置文件中修改默認模塊)

# default module name for /usr/bin/ansible
#module_name = command

注意:使用command在遠程主機執行命令的時候,不會經過shell處理。如果命令帶有重定向,管道符等會失效。

[root@localhost ~]# ansible 192.168.137.102 -a 'uptime'
192.168.137.102 | SUCCESS | rc=0 >>
15:44:41 up  1:33,  2 users, load average: 0.00, 0.01, 0.05

[root@localhost ~]# ansible 192.168.137.102 -a 'ls /root/'
192.168.137.102 | SUCCESS | rc=0 >>
anaconda-ks.cfg

 

shell 模塊

shell 模塊可以幫助我們在遠程主機上執行命令。與 command 模塊不同的是,shell 模塊在遠程主機中執行命令時,會經過遠程主機上的 /bin/sh 程序處理。

與command模塊使用方法類似,只不過支持管道,重定向,變量符等等。由於command比較安全有可預知性,所以我們平時用的時候最好用command。command無法滿足需求時,在使用shell。

[root@localhost ~]# ansible 192.168.137.102 -m shell -a 'netstat -lnpt|grep 3306'
192.168.137.102 | SUCCESS | rc=0 >>
tcp6       0      0 :::3306                 :::*                   LISTEN      1410/mysqld  

遠程執行腳本:

 首先創建一個shell腳本
vim /tmp/test.sh //加入內容
#!/bin/bash
echo `date` > /tmp/ansible_test.txt
然后把該腳本分發到各個機器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
最后是批量執行該shell腳本
ansible testhost -m shell -a "/tmp/test.sh"

 

file 模塊

file 模塊可以幫助我們完成一些對文件的基本操作。比如,創建文件或目錄、刪除文件或目錄、修改文件權限等。

包含如下選項:

force:需要在兩種情況下強制創建軟鏈接,一種是源文件不存在,但之后會建立的情況下;另一種是目標軟鏈接已存在,需要先取消之前的軟鏈,然后創建新的軟鏈,有兩個選項:yes|no

group:定義文件/目錄的屬組

mode:定義文件/目錄的權限

owner:定義文件/目錄的屬主

path:必選項,定義文件/目錄的路徑

recurse:遞歸設置文件的屬性,只對目錄有效

src:被鏈接的源文件路徑,只應用於state=link的情況

dest:被鏈接到的路徑,只應用於state=link的情況

state:

  =directory:如果目錄不存在,就創建目錄

 =file:即使文件不存在,也不會被創建

 = link:創建軟鏈接

 =hard:創建硬鏈接

 =touch:如果文件不存在,則會創建一個新的文件,如果文件或目錄已存在,則更新其最后修改時間

  =absent:刪除目錄、文件或者取消鏈接文件

#創建一個目錄,如果目錄存在,則不做任何操作。
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/ state=directory"

#創建一個文件,如果文件存在,則更新文件時間,與touch命令相同
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/testfile state=touch"

#創建軟鏈接 path=是新目標路徑,src 是源路徑
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_link state=link src=/root/test/testfile"

#創建硬鏈接
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_hard state=hard src=/root/test/testfile"

#創建軟鏈接時,如果重名 會強制覆蓋。
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_link state=link src=/root/test/testfile force=yes"

#刪除遠程文件或者是目錄
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_hard state=absent"

#創建文件時,指定屬主或者修改屬主
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_1 state=touch user=mysql"
#創建文件時,指定屬主或者修改屬組
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_1 state=touch group=mysql"

#創建文件或目錄時,指定權限 或者修改權限
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_2 state=touch mode=0644"

#遞歸修改屬主
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/ state=directory owner=mysql group=mysql recurse=yes"

 

copy 模塊

功能:實現主控端向目標主機copy文件。

參數:

#src  主控端文件位置
#dest 要將源文件復制到遠程機器的絕對路徑,必選項。
#owner 文件復制過去后的所有者
#group 文件復制過去后的所屬組
#mode   文件的權限設定,執行a+x這種方式
#backup 在覆蓋之前將源文件備份,備份文件包含時間信息。 如果與遠程主機下的文件不一致,才會備份
#directory_mode:遞歸設定目錄的權限。

 

拷貝文件:

ansible 192.168.137.102 -m copy -a 'src=/root/frs/hosts dest=/root/ '

拷貝文件,設置權限

[root@localhost ~]# ansible 192.168.137.102 -m copy -a 'src=/root/frs/hosts dest=/root/ mode=600'

拷貝目錄:

[root@localhost ~]# ls -l
總用量 766496
-rw-------. 1 root root      1616 10月  9 01:05 anaconda-ks.cfg
drwxr-xr-x. 2 root root        32 12月 19 15:43 ansible
drwxr-xr-x. 3 root root        70 12月 19 15:20 frs
-rw-r--r--. 1 root root 784882878 12月 18 14:13 frs.zip
drwxrwxr-x. 3 root root        17 12月 14 09:23 __MACOSX
-rw-r--r--. 1 root root      2139 12月 18 14:03 操作步驟.txt

[root@localhost ~]# ansible 192.168.137.102 -m copy -a 'src=/root/frs dest=/root/ '
192.168.137.102 | SUCCESS => {
   "changed": true,
   "dest": "/root/",
   "src": "/root/frs"
}

 

service 模塊

用於遠程機的服務管理。

該模塊包含如下選項:​

arguments:給命令提供一些選項

enabled:是否開機啟動 yes|no

name:必須選項,服務名稱

pattern: 定義一個模式,如果通過status指令來查看服務的狀態時,沒有響應,就會通過ps指令在進程中根據該模式進行查找,如果匹配到,則認為改服務依然在運行。

runlevel:運行級別

sleep:如果執行了restarted,則在stop和start之間沉睡幾秒鍾。

state:對當前服務執行啟動,停止,重啟,重新加載等操作(started,stopped,restarted,reload)

例子:

#關閉遠程機iptables
[root@ansible ~]# ansible zyos.com -m service -a "name=iptables state=stopped"

#關閉mysqld
[root@ansible ~]# ansible zy.com -m service -a "name=mysqld state=started"
[root@ansible ~]# ansible zy.com -m service -a "name=mysqld state=restarted sleep=20"

 

cron 計划任務模塊

Ansible cron模塊主要用於添加、刪除、更新操作系統的crontab任務計划

cron模塊使用詳解:

- name:任務計划名稱
- cron_file:替換客戶端該用戶的任務計划的文件
- minute:分(0-59, * ,*/2)
- hour:時(0-23, * ,*/2)
- day:日(1-31, * ,*/2)
- month:月(1-12, * , */2)
- weekday:周(0-6或1-7, *)
- job:任何計划執行的命令,state要等於present
- backup:是否備份之前的任務計划
- user:新建任務計划的用戶
- state:指定任務計划present、absent

例子:

ansible all -m cron -a "minute=0 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' backup=yes job='ntpdate time.windows.com'"

也可以簡寫
ansible all -m cron -a "minute=0 name='同步時間' job='ntpdate time.windows.com'"  #其他默認為*

[root@ansible ~]# ansible zy.com -m cron -a "name='a job for echo time' minute=*/2 job='sh /opt/echo.sh'"

刪除
[root@ansible ~]# ansible zy.com -m cron -a "name='a job for echo time' state=absent "

 

yum 模塊
name參數:必須參數,用於指定需要管理的軟件包,比如 nginx。
state參數:用於指定軟件包的最終狀態。present:表示安裝,默認值。 latest:安裝最新版本。absent:表示刪除。

例子:

安裝httpd
[root@localhost ~]# ansible 192.168.137.102 -m yum -a 'name=httpd state=latest'

卸載
[root@localhost ~]# ansible 192.168.137.102 -m yum -a 'name=httpd state=absent'

指定源安裝

[root@localhost ~]# ansible 192.168.137.102 -m yum -a 'enablerepo=c7-dvd name=httpd'

 

get_url 模塊

該模塊主要用於從http,ftp,https,服務器上下載文件(類似於wget)主要有如下選項:

常用選項:

timeout:下載超時時間,默認10S

url:下載URL

dest:下載文件存放的絕對路徑

url_password url_username 帳號密碼驗證。如何url_username 不指定,url_password不會使用。

use_proxy;使用代理,代理需事先在環境變更中定義。

[root@localhost ~]# ansible 192.168.137.102 -m get_url -a 'url=http://mirrors.sohu.com/nginx/nginx-1.15.5.tar.gz dest=/tmp' 

 


免責聲明!

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



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