建設(shè)銀行網(wǎng)站是多少錢成都網(wǎng)站優(yōu)化排名
目錄
一.? 本地執(zhí)行
二.? 任務(wù)委托?
三.? 任務(wù)暫停
四.? 滾動執(zhí)行
五.? 只執(zhí)行一次
六.? 設(shè)置環(huán)境變量
七.? 交互提示?
一.? 本地執(zhí)行
? ? ? ? ?我們知道ansible的是操作被控端的,所有執(zhí)行的動作都是在被控端上完成的,當然在某些特定的時候我們想要有些tasks在本地(控制端)執(zhí)行,這時我們就需要使用local_action語句。
? ? ? ? 前面我們提到過lookup的用法就是在控制端執(zhí)行指令,將返回結(jié)果存儲到變量中。然而我們的本地執(zhí)行(local_action)也可以這樣做,其實這樣的用法也是最多的。
示例1: 通過local_action語句,獲取控制端的信息,并存儲為變量。
? ? ? ? ?local_action語句的用法是 connection: local 關(guān)鍵字
[root@clinet include]# cat local_action.yml
- hosts: testgather_facts: no tasks:- name: local action infoshell: cmd: cat /tmp/1.txtregister: system_info connection: local- name: debug info debug:msg: - "{{ system_info.stdout }}"- "{{ inventory_hostname }}"
[root@clinet include]#
[root@clinet include]#
?執(zhí)行結(jié)果:
[root@clinet ansible_2]# ansible-playbook yum_file/include/local_action.yml PLAY [test] *********************************************************************************************************************************************************************************************TASK [local action info] ********************************************************************************************************************************************************************************
changed: [192.168.194.129]TASK [debug info] ***************************************************************************************************************************************************************************************
ok: [192.168.194.129] => {"msg": ["client", "192.168.194.129"]
}PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@clinet ansible_2]#
二.? 任務(wù)委托?
????????在有些時候,我們希望運行與選定的主機或主機組相關(guān)聯(lián)的task,但是這個task又不需要在選定的主 機或主機組上執(zhí)行,而需要在另一臺服務(wù)器上執(zhí)行。
這種特性適用于以下場景:
·? 在告警系統(tǒng)中啟用基于主機的告警
·? 向負載均衡器中添加或移除一臺主機
·? 在dns上添加或修改針對某個主機的解析
·? 在存儲節(jié)點上創(chuàng)建一個存儲以用于主機掛載
·? 使用一個外部程序來檢測主機上的服務(wù)是否正常
使用delegate_to語句來在另一臺主機上運行task:
‐ name: enable alerts for web servershosts: 192.168.194.129tasks:‐ name: enable alertsnagios: action:enable_alerts service:web host:"{{ inventory_hostname }}"delegate_to: 192.168.194.130
?如果delegate_to: 127.0.0.1的時候,等價于local_action
三.? 任務(wù)暫停
????????有些情況下,一些任務(wù)的運行需要等待一些狀態(tài)的恢復(fù),比如某一臺主機或者應(yīng)用剛剛重啟,我們需 要需要等待它上面的某個端口開啟,此時就需要將正在運行的任務(wù)暫停,直到其狀態(tài)滿足要求。 Ansible提供了wait_for模塊以實現(xiàn)任務(wù)暫停的需求。
參數(shù) | 說明 |
connect_timeout | 在下一個任務(wù)執(zhí)行之前等待連接的超時時間 |
delay | 等待一個端口或者文件或者連接到指定的狀態(tài)時,默認超時時間為300秒,在這等待的 300s的時間里,wait_for模塊會一直輪詢指定的對象是否到達指定的狀態(tài),delay即為多長時間 輪詢一次狀態(tài)。 |
host | wait_for模塊等待的主機的地址,默認為127.0.0.1 |
port | wait_for模塊待待的主機的端口 |
path | 文件路徑,只有當這個文件存在時,下一任務(wù)才開始執(zhí)行,即等待該文件創(chuàng)建完成 |
state | 等待的狀態(tài),即等待的文件或端口或者連接狀態(tài)達到指定的狀態(tài)時,下一個任務(wù)開始執(zhí) 行。當?shù)鹊膶ο鬄槎丝跁r,狀態(tài)有started,stoped,即端口已經(jīng)監(jiān)聽或者端口已經(jīng)關(guān)閉;當?shù)?待的對象為文件時,狀態(tài)有present或者started,absent,即文件已創(chuàng)建或者刪除;當?shù)却膶?象為一個連接時,狀態(tài)有drained,即連接已建立。默認為started |
timeout | wait_for的等待的超時時間,默認為300秒 |
示例:
#等待8080端口已正常監(jiān)聽,才開始下一個任務(wù),直到超時,默認等待超時時間為300s
‐ wait_for:port: 8080state: started#等待8000端口正常監(jiān)聽,每隔10s檢查一次,直至等待超時
‐ wait_for:port: 8000delay: 10#等待8000端口直至有連接建立
‐ wait_for:host: 0.0.0.0port: 8000delay: 10state: drained#等待8000端口有連接建立,如果連接來自10.2.1.2或者10.2.1.3,則忽略。
‐ wait_for:host: 0.0.0.0port: 8000state: drainedexclude_hosts: 10.2.1.2,10.2.1.3# 等待/tmp/foo文件被創(chuàng)建在繼續(xù)執(zhí)行
- name: Wait until the file /tmp/foo is present before continuingansible.builtin.wait_for:path: /tmp/foo# 等待/tmp/foo文件被創(chuàng)建,且文件中包含completed,在繼續(xù)執(zhí)行
- name: Wait until the string "completed" is in the file /tmp/foo before continuingansible.builtin.wait_for:path: /tmp/foosearch_regex: completed# 等待/tmp/foo文件中能匹配到completed
- name: Wait until regex pattern matches in the file /tmp/foo and print the matched groupansible.builtin.wait_for:path: /tmp/foosearch_regex: completed (?P<task>\w+)register: waitfor
- ansible.builtin.debug:msg: Completed {{ waitfor['match_groupdict']['task'] }}# 等/var/lock/file.lock被刪除
- name: Wait until the lock file is removedansible.builtin.wait_for:path: /var/lock/file.lockstate: absent# 等進程結(jié)束
- name: Wait until the process is finished and pid was destroyedansible.builtin.wait_for:path: /proc/3466/statusstate: absent# 失敗時輸出自定義的msg信息
- name: Output customized message when failedansible.builtin.wait_for:path: /tmp/foostate: presentmsg: Timeout to find file /tmp/foo#在本地執(zhí)行,等host中的主機的openssh啟動,每10s檢查一次,300s超時
# Do not assume the inventory_hostname is resolvable and delay 10 seconds at start
- name: Wait 300 seconds for port 22 to become open and contain "OpenSSH"ansible.builtin.wait_for:port: 22host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'search_regex: OpenSSHdelay: 10connection: local# Same as above but you normally have ansible_connection set in inventory, which overrides 'connection'
- name: Wait 300 seconds for port 22 to become open and contain "OpenSSH"ansible.builtin.wait_for:port: 22host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'search_regex: OpenSSHdelay: 10vars:ansible_connection: local
四.? 滾動執(zhí)行
????????默認情況下,ansible會并行的在所有選定的主機或主機組上執(zhí)行每一個task,但有的時候,我們會希 望能夠逐臺運行。最典型的例子就是對負載均衡器后面的應(yīng)用服務(wù)器進行更新時。通常來講,我們會 將應(yīng)用服務(wù)器逐臺從負載均衡器上摘除,更新,然后再添加回去。我們可以在play中使用serial語句 來告訴ansible限制并行執(zhí)行play的主機數(shù)量。
- hosts: allgather_facts: noserial: 1
?在上述示例中,serial的值為1,即表示在某一個時間段內(nèi),play只在一臺主機上執(zhí)行。如果為2,則 同時有2臺主機運行play。
一般來講,當task失敗時,ansible會停止執(zhí)行失敗的那臺主機上的任務(wù),但是繼續(xù)對其他 主機執(zhí) 行。在負載均衡的場景中,我們會更希望ansible在所有主機執(zhí)行失敗之前就讓play停止,否則很可能 會面臨所有主機都從負載均衡器上摘除并且都執(zhí)行失敗導(dǎo)致服務(wù)不可用的場景。這個時候,我們可以 使用serial語句配合max_fail_percentage語句使用。 max_fail_percentage 表示當最大失敗主機的比 例達到多少時,ansible就讓整個play失敗。示例如下:
- hosts: allgather_facts: noserial: 1max_fail_percentage: 25
?假如負載均衡后面有4臺主機,并且有一臺主機執(zhí)行失敗,這時ansible還會繼續(xù)運行,要讓Play停止 運行,則必須超過25%,所以如果想一臺失敗就停止執(zhí)行,我們可以將max_fail_percentage的值設(shè) 為24。如果我們希望只要有執(zhí)行失敗,就放棄執(zhí)行,我們可以將max_fail_percentage的值設(shè)為0。
五.? 只執(zhí)行一次
某些時候,我們希望某個task只執(zhí)行一次,即使它被綁定到了多個主機上。例如在一個負載均衡器后 面有多臺應(yīng)用服務(wù)器,我們希望執(zhí)行一個數(shù)據(jù)庫遷移,只需要在一個應(yīng)用服務(wù)器上執(zhí)行操作即可。
可以使用run_once語句來處理:
‐ name: run the database migrateionscommand: /opt/run_migrateionsrun_once: true
還可以與local_action配合使用,如下:
‐ name: run the task locally, only oncecommand: /opt/my‐custom‐commandconnection: localrun_once: true
?還可以與delegate_to配合使用,讓這個只執(zhí)行一次的任務(wù)在指定的機器上運行:
‐ name: run the task locally, only oncecommand: /opt/my‐custom‐commandrun_once: truedelegate_to: app.a1‐61‐105.dev.unp
六.? 設(shè)置環(huán)境變量
????????在命令行下執(zhí)行某些命令的時候,這些命令可能會需要依賴環(huán)境變量。比如在安裝某些包的時 候,可能需要通過代理才能完成完裝?;蛘吣硞€腳本可能需要調(diào)用某個環(huán)境變量才能完成運行。 ansible 支持通過 environment 關(guān)鍵字來定義一些環(huán)境變量。
在如下場景中可能需要用到環(huán)境變量:
·? 運行shell的時候,需要設(shè)置path變量
·? 需要加載一些庫,這些庫不在系統(tǒng)的標準庫路徑當中
示例:
‐ hosts: testtasks:‐ name: install pipyum:name: python‐pipstate: installed‐ name: install the aws toolspip:name: awsclistate: present‐ name upload file to s3shell: aws s3 put‐object ‐‐bucket=my‐test‐bucket ‐‐key={{ ansible_hostname }}/fstab ‐‐body=/etc/fstab ‐‐region=eu‐west‐1environment:AWS_ACCESS_KEY_ID: xxxxxxAWS_SECRET_ACCESS_KEY: xxxxxx
?事實上,environment也可以存儲在變量當中:
‐ hosts: allremote_user: rootvars:proxy_env:http_proxy: http://proxy.example.com:8080https_proxy: http://proxy.bos.example.com:8080tasks:‐ name: infoapt: name:cobbler state:installedenvironment: proxy_env
七.? 交互提示?
?在少數(shù)情況下,ansible任務(wù)運行的過程中需要用戶輸入一些數(shù)據(jù),這些數(shù)據(jù)要么比較秘密不方便, 或者數(shù)據(jù)是動態(tài)的,不同的用戶有不同的需求,比如輸入用戶自己的賬戶和密碼或者輸入不同的版本 號會觸發(fā)不同的后續(xù)操作等。ansible的vars_prompt關(guān)鍵字就是用來處理上述這種與用戶交互的情況 的。
- hosts: testvars_prompt:- name: share_userprompt: "what is your network username?"private: yes- name: share_passprompt: "what is your network password"private: yestasks:- name: debug info1debug:var: share_user- name: debug info2debug:var: share_pass
?vars_prompt常用選項說明:
? ? ? ? ·? private: 默認為yes,表示用戶輸入的值在命令行不可見
? ? ? ? ·? default:定義默認值,當用戶未輸入時則使用默認值
? ? ? ? ·? confirm:如果設(shè)置為yes,則會要求用戶輸入兩次,適合輸入密碼的情況
?