ansible常用命令

常用模塊:
!所有示例以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中,這項任務默認是執行的,不需要列出。
發佈了90 篇原創文章 · 獲贊 25 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章