一、ansible的配置文件介紹
版本:ansible 2.4.2.0
1、配置文件
默認路徑/etc/ansible/ansible.cfg,這些參數影響到我們后面的使用,我們這里簡單看幾個比較常用的參數。
1.1、這里可以參考http://www.51niux.com/?id=47這篇博客,樓主寫的還是很清楚。
1.2、官方文檔https://docs.ansible.com/ansible/latest/index.html
#inventory = /etc/ansible/hosts #被控制的主機配置文件 #library = /usr/share/my_modules/ ##Ansible默認搜尋模塊的位置 #remote_tmp = ~/.ansible/tmp #遠程主機緩存目錄 #local_tmp = ~/.ansible/tmp #本地緩存目錄 #forks = 5 ##這個選項設置在與主機通信時的默認並行進程數.從Ansible 1.3開始,fork數量默認自動設置為主機數量或者潛在的主機數量, 這將直接控制有多少網絡資源活着cpu可以被使用.很多用戶把這個設置為50,有些設置為500或者更多.如果你有很多的主機, 高數值將會使得跨主機行為變快.默認值比較保守. #poll_interval = 15 ##對於Ansible中的異步任務, 這個是設置定義,當具體的poll interval 沒有定義時,多少時間回查一下這些任務的狀態, 默認值是一個折中選擇15秒鍾.這個時間是個回查頻率和任務完成叫回頻率和當任務完成時的回轉頻率的時鍾。 #sudo_user = root #默認sudo用戶 #ask_sudo_pass = True #默認sudo用戶是否需要輸入密碼 #ask_pass = Fales #每次執行都需要詢問ssh密碼 #remote_port = 22 #被控主機默認端口 #module_lang = C #ansible默認語言 timeout = 10 #默認ssh嘗試連接超時時間
2、ansible的語法格式
標准格式:ansible <pattern_goes_here> -m <module_name> -a <arguments>
#pattern_goes_here: 可以是某一個主機IP(如192.168.1.128)或者是hosts中的組名(web.server),192.168.1.*代表所有但是前提是hosts文件里面已經做好了定義如192.168.1.[1:254]這樣就是執行的192.168.1.1-192.168.1.254,同時制定多個組或者多個ip使用;分割,用!表示非(如web.server:!data.server表示在web.server主機組里面但是不在data.server主機組里面的主機IP),用&表示交集部分,可以指定分組的下標或切片(如web.server[1],就是這個主機組第二個主機IP,需要主機的是超過了范圍就無法匹配了),可以用~開頭來使用正則(如:~(web|data).*\.server\.com)。需要注意的是如果系統用的是zsh,有這些類似於*,!,[等這些特殊符號需要用單引號包裹。
#module_name: 要執行的模塊名稱,當然 -m 模塊名稱 這里在語句中是可選的,默認是command,如ansible all -a "/bin/echo hello"
#arguments:要執行的命令語句
#其他主要參數:
-u username #指定ssh連接的用戶名,即執行后面命令的用戶
-k #如果是普通用戶運行ansible,當需要root權限執行的話,-k參數用來輸入密碼
-i inventory_file #指定所使用的用戶組所在的文件,默認是/etc/ansible/hosts
-f number #指定並發數,並發量大的時候,提高該數字值,如 -f 10,默認是5個。
-t #輸出結果保存路徑
-K #sudo密碼(如果執行命令需要使用sudo用戶的話,需要-u執行用戶,用-K執行輸入sudo密碼)
--private-key=key_name #如果是用非默認的id_rsa來當私鑰的話,可以指定一個私鑰文件。
3 ansible的遠程控制方式
3.1、ansible之所以不需要安裝客戶端,是因為它使用ssh,所以這里無非是做ssh信任或者執行輸入密碼。上篇我們說了如何ssh信任(免除每次輸入密碼),下面我們來說需要輸入密碼的方式,默認不輸入密碼提示拒絕訪問。這里只需要加一個-k的參數,就可以了,只要不重啟被控端,之心命令以后就不用加-k的參數了。
[root@localhost ansible]# ansible -i /etc/ansible/hosts test1 -a 'whoami'
172.16.5.239 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}
3.2、sudo使用方法
這里我們建了一個test用戶,並且設置了密碼,模擬沒有root權限的情況下,使用sudo權限。這里需要注意的是不是-k,而是-K,命令前加sudo。(特別提示,默認情況下普通用戶是沒有查看這個文件的權限)
[root@localhost ~]# ansible test1 -a 'sudo cat /etc/shadow' -u test -K SUDO password: [WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo 172.16.5.239 | SUCCESS | rc=0 >> root:$6$Dzqyob0uMKLCpruY$APjNbel3k2uJmLXF9v44eh8fHH6To7TMcFP0oVP9ftxpluU/d95SblLDypFaU85VDmL9vR3jj3vyB/gDNlE3f/::0:99999:7::: bin:*:17110:0:99999:7::: daemon:*:17110:0:99999:7::: adm:*:17110:0:99999:7::: lp:*:17110:0:99999:7::: sync:*:17110:0:99999:7::: shutdown:*:17110:0:99999:7::: halt:*:17110:0:99999:7::: mail:*:17110:0:99999:7::: operator:*:17110:0:99999:7::: games:*:17110:0:99999:7::: ftp:*:17110:0:99999:7::: nobody:*:17110:0:99999:7::: systemd-network:!!:17599:::::: dbus:!!:17599:::::: polkitd:!!:17599:::::: postfix:!!:17599:::::: sshd:!!:17599:::::: chrony:!!:17599:::::: test:$6$0ArITjlE$LBTf3a5bfIZcphKIFSMHFnd3e/ubAslekmA23deE/Pyw5fBajOCFui2oy02jLZgB8ZJ38qZh.wAuBUgROyLj00:17786:0:99999:7:::
4、ansible的常用命令
ansible --h #--h參數大家都知道了啊,就是查看有哪些參數解釋幫助說明
Usage: ansible <host-pattern> [options] Define and run a single task 'playbook' against a set of hosts Options: -a MODULE_ARGS, --args=MODULE_ARGS module arguments --ask-vault-pass ask for vault password -B SECONDS, --background=SECONDS run asynchronously, failing after X seconds (default=N/A) -C, --check don't make any changes; instead, try to predict some of the changes that may occur -D, --diff when changing (small) files and templates, show the differences in those files; works great with --check -e EXTRA_VARS, --extra-vars=EXTRA_VARS set additional variables as key=value or YAML/JSON, if filename prepend with @ -f FORKS, --forks=FORKS specify number of parallel processes to use (default=5) -h, --help show this help message and exit -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY specify inventory host path or comma separated host list. --inventory-file is deprecated -l SUBSET, --limit=SUBSET further limit selected hosts to an additional pattern --list-hosts outputs a list of matching hosts; does not execute anything else -m MODULE_NAME, --module-name=MODULE_NAME module name to execute (default=command) -M MODULE_PATH, --module-path=MODULE_PATH prepend colon-separated path(s) to module library (default=[u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']) --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 Connection Options: control as whom and how to connect to hosts -k, --ask-pass ask for connection password --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE use this file to authenticate the connection -u REMOTE_USER, --user=REMOTE_USER connect as this user (default=None) -c CONNECTION, --connection=CONNECTION connection type to use (default=smart) -T TIMEOUT, --timeout=TIMEOUT override the connection timeout in seconds (default=10) --ssh-common-args=SSH_COMMON_ARGS specify common arguments to pass to sftp/scp/ssh (e.g. ProxyCommand) --sftp-extra-args=SFTP_EXTRA_ARGS specify extra arguments to pass to sftp only (e.g. -f, -l) --scp-extra-args=SCP_EXTRA_ARGS specify extra arguments to pass to scp only (e.g. -l) --ssh-extra-args=SSH_EXTRA_ARGS specify extra arguments to pass to ssh only (e.g. -R) Privilege Escalation Options: control how and which user you become as on target hosts -s, --sudo run operations with sudo (nopasswd) (deprecated, use become) -U SUDO_USER, --sudo-user=SUDO_USER desired sudo user (default=root) (deprecated, use become) -S, --su run operations with su (deprecated, use become) -R SU_USER, --su-user=SU_USER run operations with su as this user (default=None) (deprecated, use become) -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) -K, --ask-become-pass ask for privilege escalation password
ansible-doc -h
Options:
-a, --all **For internal testing only** Show documentation for
all plugins.
-h, --help show this help message and exit
-l, --list 列出可用的模塊
-M MODULE_PATH, --module-path=MODULE_PATH
prepend colon-separated path(s) to module library
(default=[u'/root/.ansible/plugins/modules',
u'/usr/share/ansible/plugins/modules'])
-s, --snippet 顯示指定模塊的劇本片段,-s 模塊名稱 可以查看某個模塊詳細的使用方法
-t TYPE, --type=TYPE Choose which plugin type (defaults to "module")
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program's version number and exit
注意:這里需要特別注意不管是靜態inventory,還是動態生成的inventory,還是我們把重要信息寫入到的ansible.cfg,為了安全起見,一般情況下除了root用戶,其他用戶不設置任何權限。一旦你的服務器清單落在黑客的手里,那記得機器就直接相當於在互聯網上裸奔。
