Ansible 快速上手

2021-10-04 02:11:10 字數 3969 閱讀 2754

ansible 快速上手

ansible 命令

ansible 主機清單配置檔案

ansible playbook 劇本語法

ansible 是 paramiko 開發的一種自動化運維工具,基於模組化工作,集合了眾多運維工具的優點,實現了批量系統配置,批量程式部署、批量執行命令等功能。

ansible 它本身沒有批量部署的能力,真正執行這些操作的是 ansible 的模組,它只是提供了一種框架。直到目前為止,ansible 已有 800 多個模組可以使用。

ansible 的另乙個優勢在於,它不需要在遠端主機上安裝任何東西,因為它是基於 ssh 來和遠端主機進行通訊的。

在 ansible 裡,有兩種角色 control machine 與 managed node,它們通過 ssh 和 python 進行溝通:

在一般情況下,我們只需在 control machine 裡安裝 ansible 即可,因為 linux 和 macos 系統早已預載了 python 2.7 以上的版本,且支援 ssh 連線。

# 請先安裝 homebrew,已安裝者請略過

/usr/bin/ruby -e "$(curl -fssl "

# 安裝 ansible

brew install ansible

linux 安裝 ansiblecentos(yum)

# 新增 epel-release 第三方套件**。

sudo yum install -y epel-

release

# 安裝 ansible

sudo yum install -y ansible

ubuntu(apt)

# 安裝 add-apt-repository 必要套件

sudo apt-get install -y python-software-properties software-properties-common

# 使用 ansible 官方的 ppa 套件**

sudo add

-apt-repository -y ppa:ansible/ansible; sudo apt-get update

# 安裝 ansible

sudo apt-get install -y ansible

使用 pip 安裝 ansible
# 1、首先安裝 pip

# debian, ubuntu

$ sudo apt-get install -y python-pip

# centos

$ sudo yum install -y python-pip

# macos

$ sudo easy_install pip

# 2、公升級 pip

sudo pip install -u pip

# 3、安裝 ansible

sudo pip install ansible

在安裝 ansible 之後,你可以訪問以下 ansible 檔案:

第一種方式是通過向 ansible 傳送 ad-hoc commands(指令)來操作 managed node。

以常見的 ping 和 echo 操作為例:

ansible all

-m ping -k

server1 | success =

>

ansible all

-m command -a "echo hello world"

server1 | success | rc=

0>>

hello world

另外一種方式即通過 playbook (劇本)讓各個 managed node 進行指定的動作(plays)和任務(tasks),你可以理解為 shell script。playbook 支援兩種寫法 :在乙份 playbook 中,可以有多個 play、task 與 module:下面是乙個 hello world 的 playbook 劇本,劇本字尾名應為 .yml:

- name: say 'hello world'

hosts: all

tasks:

- name: echo 'hello world'

command: echo 'hello world'

register: result

- name: print stdout

debug:

msg: ""

執行劇本

ansible-playbook hello_world.yml
模組是 ansible 的核心,也是操作真正的執行者,只要掌握了如何使用模組就可以快速上手 ansible,其餘都只是延伸使用罷了。例如我們經常使用的 command modules 模組,你可以進入 ansible 的 module 幫助文件中找到它的使用文件。

文件中 options 選項表會列出模組引數,引數的預設值等資訊。

在使用 playbook 時,ansible 會自動執行 setup modules 以收集各個 managed node 的 facts(系統資訊),如 ip、作業系統、cpu 資訊等。

我們也可以通過 ad-hoc commands 指令模式使用 setup,例如:

ansible all

-m setup | less

--- 結果如下

server1 | success =

> ,

"changed": false

}

通常在 playbook 中我們會將主機資訊結合條件判斷 when 使用,例如下面針對 debian, ubuntu, centos 安裝 vim 的 playbook:

- name: setup the vim 

hosts: all

become: true

tasks:

# debian, ubuntu.

- name: install apt packages

apt: name=vim state=present

when: ansible_pkg_mgr =

="apt"

# centos.

- name: install yum packages

yum: name=vim-minimal state=present

when: ansible_pkg_mgr =

="yum"

ibatis 快速上手

簡介 例子 現在我們我們通過乙個簡單的案例,了解如何通過ibatis解決資料訪問問題。現在有有乙個資料庫,資料庫裡面有一張人員資訊表 需求是這樣 通過乙個web應用程式顯示人員資訊表裡面的資訊,並且可以新增 修改 刪除人員記錄 乙個並不複雜的案例,但是它覆蓋所有開發首先要學習的內容。第1步 the ...

Pro C快速上手

條件 winxp vc 6 裝在c program files microsoft visual studio vc98 oracle 9 裝在e oracle ora90 步驟 1 dos視窗設定環境變數,預編譯 oralce 給的關於pro c 例子 2 設定vc 環境 3 編譯執行 c程式。具...

Apache James快速上手

2,配置 2.1,環境配置 apache james執行需要jvm,保證所在主機安裝jdk即可 2.2,埠配置 apache james 使用到的埠包括 pop3埠 110 smtp埠 25 2.3,啟動應用 配置完畢james後,就可以啟動了,啟動命令很簡單,只需要執行bin run.sh即可 也...