ansible 默認提供了很多模塊來供我們使用。在 Linux 中,可以通過 ansible-doc -l 命令查看到當前 ansible 都支持哪些模塊,通過 ansible-doc -s 模塊名 又可以查看該模塊有哪些參數可以使用
常用模塊:
!所有示例以webserver為匹配目標主機。
1.ping
ansible all -m ping
!檢測機器是否可登錄,ping模塊不需要傳送參數
!注:這里的ping模塊並非調用了系統的ping命令,而是類似於登錄到遠程機器再echo出一個信息。
2.command
!可以執行任意命令,但不接受管道命令和重定向符號,如果想使用這些,則需要使用Shell模塊。
ansible all -m command -a "ls" #在所有匹配主機上執行ls命令
playbook:
- name: test for command
command: ls
3.copy
#復制一個文件到遠程服務器
ansible all -m copy -a "src=/tmp/text.txt dest=/home/tmp/"
#將本地text.txt文件復制到所有匹配主機的/home/tmp/路徑下。
playbook:
- name: test for copy
copy: src=/tmp/text.txt dest=/home/tmp/ force=yes
4.file
這個模塊這次構建系統中並沒有用到,官方的解釋是用來設置文件、文件夾或快鏈的屬性,或者刪除文件、快鏈、文件夾。
創建一個文件夾,如果目標不存在
ansible webservers -m file -a "path=/tmp/tdir state=directory mode=0755"
playbook
- name: create a directory if it doesn't exist
- file:
path: /etc/some_directory
state: directory
mode: 0755
5.get_url
!從服務器上下載一個文件到遠程主機指定的目錄
ansible webservers -m get_url -a "url='http://baidu.com dest=/tmp/ba.html"
playbook
- name: download foo.conf
get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
mode: 0440
6.yum
特別適用於批量安裝一些依賴包的時候
ansible webservers -m yum -a "name=httpd state=latest"
ansible webservers -m yum -a "name=httpd state=absent" !刪除包
playbook
- name: install the latest version of Apache
yum:
name: httpd
state: latest
7.service
控制遠程服務器上的服務
ansible webservers -m service -a "name=nginx state=restart"
playbook
- name: restart rmote nginx
service: name=nginx state=restart
8.setup
收集遠程服務器信息
ansible all -m setup
在playbook中,這項任務默認是執行的,不需要列出。
---------------------
參考:https://blog.csdn.net/kevin3101/article/details/69945516