ansible配置檔案介紹及命令介紹

2021-09-21 20:31:54 字數 3889 閱讀 3218

1、ansible的配置檔案 1

2

3

4

5

[root@vm ansible]#  rpm -ql ansible |grep etc

/etc/ansible

/etc/ansible/ansible.cfg

/etc/ansible/hosts

/etc/ansible/roles

ansible.cfg 檔案可以設定一些預設值,這樣我們就不需要對同樣的內容輸入很多遍。

我應該把ansible.cfg 檔案放到**呢

ansible 按照如下位置和順序來查詢ansible.cfg 檔案:

1.ansible_config 環境變數所指定的檔案。

2../ansible.cfg(當前目錄下的ansible.cfg)。

3.~/.ansible.cfg(主目錄下的.ansible.cfg)。

4./etc/ansible/ansible.cfg。

配置檔案ansible.cfg常見配置項:

1

2

3

4

5

6

7

8

9

10

11

12

13

[defaults]

# some basic default values...

inventory      =/etc/ansible/hosts

#library        = /usr/share/my_modules/

remote_tmp     = $home/.ansible/tmp

pattern        = *

forks          = 5

poll_interval  = 15

sudo_user      = root

#ask_sudo_pass = true

#ask_pass      = true

transport      = smart

#remote_port    = 22

也比較容易理解,就是我們需要操作的主機清單、預設ssh的埠、ansible程序數、sudo的使用者 等等

2、主機列表清單:

主機列表清單 hosts 檔案:(所有被管理的主機都必須放到hosts中,當然不一定要是此hosts,可以使用ansible的 -i 引數 指定) 1

2

[testhost]

172.16.162.130

這也是常見的格式,testhost表示組,172.16.162.130表示成員,可以有多個成員,比如 1

2

3

[testhost]

172.16.162.130

172.16.162.131

如果主機有規則的相似命名,可以使用列表形式: 1

2

[testwebservers]

web[01:10].lansgg.com

表示web01.lansgg.com web02.lansgg.com ------- web10.lansgg.com

主機變數:

在hosts定義主機新增變數,可以在playbook中引用 1

2

3

[testdbservers]

db1.lansgg.com db_port=3306 server-id=1

db2.lansgg.com db_port=3305 server-id=2

組變數:

指組內所有主機賦予相同變數,可以在playbook中引用

組巢狀:

組可以包含其他組,也可以向組中主機指定變數,可以在ansible-playbook中使用,ansible不可以 1

2

3

4

5

6

7

8

9

10

11

[web]

web01.lansgg.com

web02.lansgg.com

[db]

db01.lansgg.com

db02.lansgg.com

[all:children]

web

db

[all:vars]

ntp_server=172.16.100.100

也可以全部指定 也就是 all

3、ansible命令格式 1

2

synopsis

ansible [-f forks] [-m module_name] [-a args]

命令示例:

1

2

3

[root@vm ansible]# ansible testhost -m command -a 'date'

172.16.162.130 | success | rc=0 >>

tue feb 23 17:22:30 cst 2016

--- host-pattern 指定對哪些主機進行操作;也就是示例中的 testhost 主機組

--- forks 啟動的併發執行緒數

--- module_name 要使用的模組 示例中的 command

--- args 模組的引數   示例中的 date

Ansible配置檔案

ansible的配置檔案位於 etc ansible目錄下,主要有ansible.cfg hosts檔案。本節重點介紹主機與組定義檔案 etc ansible hosts.etc ansible hosts最簡單的格式如下 www.ixdba.net webservers ixdba1.net ix...

目錄介紹及配置檔案

目錄名 作用boot etc存放配置檔案 mnt一般用來設定掛載點 src sys proc 與系統核心相關,不懂勿碰 tmp臨時目錄,隨時刪除 dev裝置相關 linux中所有裝置都需要掛載才可使用 home 普通使用者的家目錄 root 管理員的家目錄 usr local 存放使用者編譯的原始碼...

ansible配置檔案詳解(2)

action plugins 行為 是 ansible中的一段 用來啟用一些事件,例如執行乙個模組,乙個模版,等等 這是乙個以開發者為中心的特性,使得一些底層模組可以從外部不同地方載入 action plugins ansible plugins action plugins usr share a...