ansible經常使用命令

  ansible 默認提供了不少模塊來供咱們使用。在 Linux 中,能夠經過 ansible-doc -l 命令查看到當前 ansible 都支持哪些模塊,經過 ansible-doc  -s  模塊名  又能夠查看該模塊有哪些參數能夠使用html

經常使用模塊:
!全部示例以webserver爲匹配目標主機。
1.ping
ansible all -m ping
!檢測機器是否可登陸,ping模塊不須要傳送參數
!注:這裏的ping模塊並不是調用了系統的ping命令,而是相似於登陸到遠程機器再echo出一個信息。nginx

 

2.command
!能夠執行任意命令,但不接受管道命令和重定向符號,若是想使用這些,則須要使用Shell模塊。
ansible all -m command -a "ls" #在全部匹配主機上執行ls命令web

playbook:
- name: test for command
command: ls服務器

 

3.copy
#複製一個文件到遠程服務器url

ansible all -m copy -a "src=/tmp/text.txt dest=/home/tmp/"
#將本地text.txt文件複製到全部匹配主機的/home/tmp/路徑下。.net

playbook:
- name: test for copy
copy: src=/tmp/text.txt dest=/home/tmp/ force=yesrest

 

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: 0755server


5.get_url
!從服務器上下載一個文件到遠程主機指定的目錄
ansible webservers -m get_url -a "url='http://baidu.com dest=/tmp/ba.html"htm

playbook
- name: download foo.conf
get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
mode: 0440blog

 

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

相關文章
相關標籤/搜索