Ansible基本命令

Ansible安裝完成之后就自帶很多命令,其中較常用的有7個:

  • ansible
  • ansible-doc
  • ansible-galaxy
  • ansible-init
  • ansible-playbook
  • ansible-pull
  • ansible-vault

ansible

ansible -h
Usage: ansible <host-pattern> [options]

  對本機執行一個命令:

ansible 127.0.0.1 -a "date"127.0.0.1 | SUCCESS | rc=0 >>
Fri Apr  6 18:45:01 CST 2018

  在遠程主機上執行命令;

ansible test -a "date"172.16.1.10 | UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.16.1.10' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).\r\n", "unreachable": true
}

由上可知:在遠程主機上執行命令的時候沒有權限(密碼不知道),所以我們可以在上述命令的基礎上加上-k參數:

ansible test -a "date" -kSSH password: 
172.16.1.10 | SUCCESS | rc=0 >>
Fri Apr  6 18:56:52 CST 2018

  ansible命令的常用參數;

-a MODULE_ARGS, --args=MODULE_ARGS module arguments  命令行參數
--ask-vault-pass      ask for vault password
-B SECONDS, --background=SECONDS
-C, --check           don't make any changes; instead, try to predict some
-D, --diff            when changing (small) files and templates, show the
-e EXTRA_VARS, --extra-vars=EXTRA_VARS
-f FORKS, --forks=FORKS     # 指定執行任務時并發數量
-i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY  #指定庫存主機文件的路徑,默認為/etc/ansible/hosts                
-l SUBSET, --limit=SUBSET
--list-hosts          outputs a list of matching hosts; does not execute
-m MODULE_NAME, --module-name=MODULE_NAME module name to execute (default=command)  # 執行模塊的名稱,默認使用command模塊
-M MODULE_PATH, --module-path=MODULE_PATH
-u REMOTE_USER, --user=REMOTE_USER      # 執行用戶,使用這個遠程用戶而不是當前用戶
-U SUDO_USER, --sudo-user=SUDO_USER     # sudo到哪個用戶,默認為root
-k, --ask-pass  ask for connection password     # 登陸密碼,提示輸入ssh密碼
-K, --ask-become-pass       # 提示密碼使用sudo -s --sudo sudo運行
--new-vault-id=NEW_VAULT_ID the new vault identity to use for rekey
--new-vault-password-file=NEW_VAULT_PASSWORD_FILES new vault password file for rekey
-o, --one-line        condense output
-P POLL_INTERVAL, --poll=POLL_INTERVAL set the poll interval if using -B (default=15)
--syntax-check        perform a syntax check on the playbook, but do not execute it
-t TREE, --tree=TREE  log output to this directory
--vault-id=VAULT_IDS  the vault identity to use
--vault-password-file=VAULT_PASSWORD_FILES vault password file
-v, --verbose         verbose mode (-vvv for more, -vvvv to enable connection debugging)
--version             show program's version number and exit
--private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
-c CONNECTION, --connection=CONNECTION              
-T TIMEOUT, --timeout=TIMEOUT               
--ssh-common-args=SSH_COMMON_ARGS          
--sftp-extra-args=SFTP_EXTRA_ARGS                    
--scp-extra-args=SCP_EXTRA_ARGS       
--ssh-extra-args=SSH_EXTRA_ARGS
-s, --sudo          run operations with sudo (nopasswd) (deprecated, use become)  
-S, --su            run operations with su (deprecated, use become)
-R SU_USER, --su-user=SU_USER
-b, --become        run operations with become (does not imply password prompting)
--become-method=BECOME_METHOD privilege escalation method to use (default=sudo), valid choices: [ sudo | su | pbrun | pfexec | doas | dzdo | ksu | runas | pmrun ]
--become-user=BECOME_USER run operations as this user (default=root)
--ask-sudo-pass     ask for sudo password (deprecated, use become)
--ask-su-pass       ask for su password (deprecated, use become)

ansible-doc

ansible-doc -h
Usage: ansible-doc [-l|-s] [options] [-t <plugin type] [plugin]plugin documentation toolOptions:
-a, --all             **For internal testing only** Show documentation forall plugins.
-h, --help            show this help message and exit
-l, --list            List available plugins
-M MODULE_PATH, --module-path=MODULE_PATHprepend colon-separated path(s) to module library(default=[u'/root/.ansible/plugins/modules',u'/usr/share/ansible/plugins/modules'])
-s, --snippet         Show playbook snippet for specified plugin(s)
-t TYPE, --type=TYPE  Choose which plugin type (defaults to "module")
-v, --verbose         verbose mode (-vvv for more, -vvvv to enableconnection debugging)
--version             show program's version number and exitSee man pages for Ansible CLI options or website for tutorials
https://docs.ansible.com

ansible-doc 相當于一個幫助文檔,結合-s參數

ansible-doc -s file- name: Sets attributes of files
file:attributes:            # Attributes the file or directory should have. To get supported flags look at the man page for `chattr' on thetarget system. This string should contain the attributes in the same order as theone displayed by `lsattr'.follow:                # This flag indicates that filesystem links, if they exist, should be followed.force:                 # force the creation of the symlinks in two cases: the source file does not exist (but will appear later); thedestination exists and is a file (so, we need to unlink the "path" file and createsymlink to the "src" file in place of it).group:                 # Name of the group that should own the file/directory, as would be fed to `chown'.mode:                  # Mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually octalnumbers (like 0644). Leaving off the leading zero will likely have unexpectedresults. As of version 1.8, the mode may be specified as a symbolic mode (forexample, `u+rwx' or `u=rw,g=r,o=r').owner:                 # Name of the user that should own the file/directory, as would be fed to `chown'.path:                  # (required) path to the file being managed.  Aliases: `dest', `name'recurse:               # recursively set the specified file attributes (applies only to state=directory)selevel:               # Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'. `_default'feature works as for `seuser'.serole:                # Role part of SELinux file context, `_default' feature works as for `seuser'.setype:                # Type part of SELinux file context, `_default' feature works as for `seuser'.seuser:                # User part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it will usethe `user' portion of the policy if available.src:                   # path of the file to link to (applies only to `state=link'). Will accept absolute, relative and nonexisting paths.Relative paths are not expanded.state:                 # If `directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be createdwith the supplied permissions. If `file', the file will NOT be created if it doesnot exist, see the [copy] or [template] module if you want that behavior.  If`link', the symbolic link will be created or changed. Use `hard' for hardlinks. If`absent', directories will be recursively deleted, and files or symlinks will beunlinked. Note that `absent' will not cause `file' to fail if the `path' does notexist as the state did not change. If `touch' (new in 1.4), an empty file will becreated if the `path' does not exist, while an existing file or directory willreceive updated file access and modification times (similar to the way `touch`works from the command line).unsafe_writes:         # Normally this module uses atomic operations to prevent data corruption or inconsistent reads from the targetfiles, sometimes systems are configured or just broken in ways that prevent this.One example are docker mounted files, they cannot be updated atomically and canonly be done in an unsafe manner. This boolean option allows ansible to fall backto unsafe methods of updating files for those cases in which you do not have anyother choice. Be aware that this is subject to race conditions and can lead to datacorruption.

  ansible新版已經添加了操作docker的相關模塊:

ansible-doc -l |grep dockerdocker                                    manage docker containers                                                                           
docker_container                          manage docker containers                                                                           
docker_image                              Manage docker images.                                                                              
docker_image_facts                        Inspect docker images                                                                              
docker_login                              Log into a Docker registry.                                                                        
docker_network                            Manage Docker networks                                                                             
docker_secret                             Manage docker secrets.                                                                             
docker_service                            Manage docker services and containers.                                                             
docker_volume                             Manage Docker volumes 

ansible-galaxy

  從官網下載其他第三方模塊和插件。

ansible-galaxy install username.rolename

ansible-init

  ansible-init是對Ansible的playbook進行語法檢查的一個工具。

ansible-playbook

  該命令時使用最多的命令,其通過讀取playbook文件后,執行相應的動作。

ansible-pull

  ansible的另一種模式-- pull模式。跟平常使用的push模式剛好相反,適用于以下場景;

  • 操作巨大數量的主機,即使使用非常高的線程還是需要很長時間;
  • 在沒有網絡連接的主機上運行Ansible時;

ansible-vault

  ansible-vault主要應用于配置文件中含有敏感信息的時候,不希望別人看到,這時使用這個命令可以將某些配置文件加密,屬于高級用法。主要對于playbook里涉及到配置密碼或者其他敏感信息時,可以使用該命令加密。在使用類似cat等查看命令查看加密后的配置文件時,需要輸入密碼才可以查看。這種playbook執行時,需要加上--ask-vault-pass參數,同樣需要輸入密碼之后才能執行。

轉載于:https://www.cnblogs.com/jie-fang/p/10293171.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249789.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249789.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249789.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java高并發高性能分布式框架從無到有微服務架構設計

Java高并發高性能分布式框架從無到有微服務架構設計

Makefile中幾種賦值

延時變量&#xff0c;只有被使用時才展開定義 :?立即變量&#xff0c;定義時的賦值立即有效 ??條件變量&#xff0c;當變量為空時才賦值 追加賦值轉載于:https://www.cnblogs.com/smzd/p/10695962.html

線程的基本協作和生產者消費者

協作基礎&#xff08;wait/notify&#xff09; Java的根父類是Object&#xff0c;Java在Object類而非Thread類中&#xff0c;定義了一些線程協作的基本方法&#xff0c;使得每個對象都可以調用這些方法&#xff0c;這些方法有兩類&#xff0c;一類是wait&#xff0c;另一類是no…

L1-016 查驗身份證

L1-016 查驗身份證 &#xff08;15 分&#xff09;一個合法的身份證號碼由17位地區、日期編號和順序編號加1位校驗碼組成。校驗碼的計算規則如下&#xff1a; 首先對前17位數字加權求和&#xff0c;權重分配為&#xff1a;{7&#xff0c;9&#xff0c;10&#xff0c;5&#xff…

什么是高并發,如何避免高并發

之前我將高并發的解決方法誤認為是線程或者是隊列可以解決&#xff0c;因為高并發的時候是有很多用戶在訪問&#xff0c;導致出現系統數據不正確、丟失數據現象&#xff0c;所以想到 的是用隊列解決&#xff0c;其實隊列解決的方式也可以處理&#xff0c;比如我們在競拍商品、轉…

.sync 修飾符的理解

正常 子組件&#xff1a; this.$emit(update:title, newTitle)父組件&#xff1a; <text-documentv-bind:title"doc.title"v-on:update:title"doc.title $event" ></text-document>簡潔&#xff1a; <text-document v-bind:title.sync&quo…

L1-025 正整數A+B

題的目標很簡單&#xff0c;就是求兩個正整數A和B的和&#xff0c;其中A和B都在區間[1,1000]。稍微有點麻煩的是&#xff0c;輸入并不保證是兩個正整數。 輸入格式&#xff1a; 輸入在一行給出A和B&#xff0c;其間以空格分開。問題是A和B不一定是滿足要求的正整數&#xff0c;…

到底多大才算高并發?

一、什么是高并發 定義&#xff1a; 高并發(High Concurrency)是使用技術手段使系統可以并行處理很多請求。關鍵指標&#xff1a; -響應時間(Response Time) -吞吐量(Throughput) -每秒查詢率QPS(Query Per Second) -每秒事務處理量TPS(Transaction Per Second) -同時在…

eclipse安裝maven插件

1、在線安裝插件 a.打開eclipse&#xff0c;菜單“Help”-“Install New Software...” b.在Work with 地址欄輸入&#xff1a;http://download.eclipse.org/releases/對應eclipse版本名稱 c.在filter框中輸入maven d.選擇“Collaboration”-“m2e - Maven Integration for Ecl…

VS2017 網站打包發布生成的文件中包含.pdb文件,解決辦法

右鍵點擊項目屬性&#xff0c;選擇打包/發布 Web&#xff0c;勾選 排除生成的調試符號&#xff1a; 再次發布&#xff0c;就不會再生成.pdb文件 轉載于:https://www.cnblogs.com/JoinLet/p/10297254.html

java分布式學習路線

先理解為什么需要分布式&#xff0c;因為服務器處理的能力需要提升&#xff0c;這里有兩個方面&#xff0c;第一是縱向 也就是增加cpu的能力&#xff0c;或者加內存&#xff1b;另一個方向就是 橫向 &#xff0c;就是分布式。將本來一臺計算機的壓力分給多太計算機&#xff0c;…

深入Nginx之《常用參數配置技巧》

常見參配置實戰技巧 下面會講解實戰中應該怎么配置更為合理。 1.user 默認是nobody&#xff0c;如果使用nobody&#xff0c;Nginx在運行過程中會出現很多操作沒有權限&#xff0c;比如寫硬盤。一般都是用低于root級別的用戶&#xff0c;比如www&#xff0c;并且可以在linux下設…

transform、transition 動畫效果

transform 轉換&#xff0c;變形 origin 定義旋轉基點&#xff08;left top center right bottom 坐標值&#xff09; transform-origin: 50px 50px; transform-origin: left;。 rotate 旋轉 transform:rotate(50deg) 旋轉角度可以為負數&#xff0c;需要先定義…

eslint 規則

"off"或0- 關閉規則 "warn"或1- 將規則作為警告打開&#xff08;不影響退出代碼&#xff09; "error"或2- 將規則作為錯誤打開&#xff08;觸發時退出代碼為1&#xff09;轉載于:https://www.cnblogs.com/smzd/p/10844161.html

分布式學習路線

由于分布式系統所涉及到的領域眾多&#xff0c;知識龐雜&#xff0c;很多新人在最初往往找不到頭緒&#xff0c;不知道從何處下手來一步步學習分布式架構。 本文試圖通過一個最簡單的、常用的分布式系統&#xff0c;來闡述分布式系統中的一些基本問題。 負載均衡分布式緩存分…

洛谷p2704 炮兵陣地

典型的狀態壓縮題&#xff0c;只是要定義dp[i][j][k]&#xff0c; #include<bits/stdc.h>using namespace std;int n,m,state[1<<10],tot0;int dp[110][66][66],sum[1<<10],mp[110];int getsum(int x){ int cnt0; while(x){ if(x&1) cnt; …

Flask 框架app = Flask(__name__) 解析

1 #!/usr/local/bin/python2 # codingutf-83 4 from flask import Flask5 app Flask(__name__)6 7 app.route(/)8 def hello_world():9 return Hello World! 10 11 if __name__ __main__: 12 app.run(host0.0.0.0,port9000) 第4行&#xff0c;引入Flask類&#x…

發布訂閱

發布訂閱 在軟件架構中&#xff0c;發布訂閱是一種消息范式&#xff0c;消息的發送者&#xff08;稱為發布者&#xff09;不會將消息直接發送給特定的接收者&#xff08;稱為訂閱者&#xff09;。而是將發布的消息分為不同的類別&#xff0c;無需了解哪些訂閱者&#xff08;如果…

ps 命令

ps 為我們提供了進程的一次性的查看&#xff0c;它所提供的查看結果并不動態連續的&#xff1b;如果想對進程時間監控&#xff0c;應該用 top 工具。kill 命令用于殺死進程。linux上進程有5種狀態: 1. 運行(正在運行或在運行隊列中等待) 2. 中斷(休眠中, 受阻, 在等待某個條件的…

python練習題-day8

1、有如下文件&#xff0c;a1.txt&#xff0c;里面的內容為&#xff1a; 老男孩是最好的培訓機構&#xff0c; 全心全意為學生服務&#xff0c; 只為學生未來&#xff0c;不為牟利。 我說的都是真的。哈哈 分別完成以下的功能&#xff1a; a,將原文件全部讀出來并打印。 with o…