Ansible流程控制

2021-10-10 20:06:08 字數 1107 閱讀 9129

在開發中,經常會編寫一些流程控制相關的邏輯,如條件判斷、迴圈等,ansible也提供了對條件判斷、迴圈等的操作。

ansible的模板配置是基於jinja2的,如變數的定義:}

關於when的更多示例

---- hosts: proxy

tasks:

- name: deploying to development...

debug:

msg: "deploy to development."

when: dev|bool

- name: deploying to production...

debug:

msg: "deploy to production."

when: prod|bool

ansible的迴圈通常是以with_開頭的,如:

# cat test_vars.yml

---- hosts: proxy

tasks:

- name: test vars

debug:

msg: "}"

with_items:

- 1- 2

- 3

更多迴圈的使用方式,請參照loops

# cat install_pkgs.yml

---- hosts: proxy

tasks:

- name: install packages.

package:

name: "}"

state: present

with_items:

- vim

- gcc

- wget

# cat copy_files.yml

---- hosts: proxy

tasks:

- name: copy files

copy:

src: "}"

dest: "}"

owner: root

group: root

with_items:

- -

ansible 流程控制和roles

模組 yum 安裝 yum模組 pip 安裝python相關模組 service 啟動服務 enabled 設定開機自啟動 不能都是 那也會導致每分鐘都執行相同的命令 會撐爆伺服器 group 建立組 user 使用者 ad hoc 這些模組是這樣稱呼的 yaml 傳參 動態的 yaml ansib...

ansible遠端控制

環境 一台centos7,一台centos6.5 實驗步驟 centos7的伺服器上 一.準備工作 1.關閉selinux 清空缺省防火牆策略,橋接物理網路。setenforce 0 ifconfig systemctl restart sshd iptables f 2.安裝遠端登陸的依賴關係包。...

mysql流程控制 MySQL 流程控制

流程控制 1 順序結構 程式從上往下依次執行 2 分支結構 多條路徑選擇一條 3 迴圈結構 在規定範圍內重複執行 一 分支結構 1 if函式 功能 實現分支流 語法 if 表示式1,表示式2,表示式3 執行順序 執行表示式1,成立返回表示式2的值,不成立則返回表示式3的值 應用 任何地方 例 if ...