ansible 入門與進階

ansible 入門與進階

Configuration、Command and Control

是什么 ? SSH-based configuration management, deployment, and task execution system

運維工具的分類:

agent:基于專用的agent程序完成管理功能,puppet, func, zabbix, …
agentless:基于ssh服務完成管理,ansible, fabric, …

架構:

Ansible Core

Modules:

Core Modules
Customed Modules
Host Iventory
Files
CMDB
PlayBooks
Hosts
roles
Connection Plugins:

特性:

模塊化:調用特定的模塊,完成特定的任務;
基于Python語言研發,由Paramiko, PyYAML和Jinja2三個核心庫實現;
部署簡單:agentless;
支持自定義模塊,使用任意編程語言;
強大的playbook機制;
冪等性;

安裝及程序環境:

程序:

ansible
ansible-playbook
ansible-doc

配置文件:

/etc/ansible/ansible.cfg

主機清單:

/etc/ansible/hosts

插件目錄:

/usr/share/ansible_plugins/

首先安裝ansible : 這里我使用的是openSUSE 直接安裝的

201611052155001

安裝完成后配置一下/etc/ansible/hosts 下的主機列表

201611052157001采用的是test組中的的四臺主機, 這四臺主機已經提前通過ssh-copy-id 命令分發ssh的公鑰

201611052158001使用ansible 的ping模塊確認主機是否在線.

! 注意 : 在這里我之前給自己挖了個坑, 在 /root/.bashrc 文件中加入了一段命令用于登陸時顯示當前主機的IP地址, 造成了ansible 命令執行時總是顯示主機不在線, 注釋掉就執行正常.

命令語法

ansible <host-pattern> [options]

模塊簡介 :

ping:探測目標主機是否存活;

命令示例 :

ansible test -m ping

201611052158001

command:在遠程主機執行命令;

命令示例 :

ansible test -m command -a “ls -l /tmp”

201611052206001

shell:在遠程主機上調用shell解釋器運行命令,支持shell的各種功能,例如管道等 ;[ 推薦使用! ]

注意

command和shell模塊的核心參數直接為命令本身;而其它模塊的參數通常為“key=value”格式;

參數幫助中參數后面有等于號的通常是此選項的必須模塊

使用示例 :

ansible test -m shell -a “hostnamectl | head -1”

201611052207001

copy: C o p i e s   f i l e s   t o   r e m o t e   l o c a t i o n s .

用法:

(1) 復制文件

-a “src=  dest=  “

(2) 給定內容生成文件

-a “content=  dest=  “

其它參數:mode, owner, group, …

201611061305001201611061307001

file:S e t s   a t t r i b u t e s   o f   f i l e s

用法:
(1) 創建目錄:
-a “path=  state=directory”
(2) 創建鏈接文件:
-a “path=  src=  state=link”
(3) 刪除文件:
-a “path=  state=absent“

201611061308001 201611061309001

fetch:F e t c h e s   a   f i l e   f r o m   r e m o t e   n o d e s

將遠程主機上的 文件通過復制保存到本地, 自動按照主機IP進行分類

使用示例 :

ansible test -m fetch -a “src=/root/.bashrc dest=/root/test1”

201611061312001

cron:M a n a g e   c r o n . d   a n d   c r o n t a b   e n t r i e s .

為遠程主機設定計劃任務
-a “”
minute=
hour=
day=
month=
weekday=
job=
name=
user=

201611061321001 201611061321002

state={present|absent} present 為默認的選項, absent顯示聲明

201611061323002201611061323001

hostname:M a n a g e   h o s t n a m e

為遠程主機設定主機名
name=

yum:M a n a g e s   p a c k a g e s   w i t h   t h e   I ( y u m )   p a c k a g e   m a n a g e r
-a “”
(1) name=  state= (present|latest|absent)
(2) name=  state=absent

為遠程主機進行軟件包管理

service:M a n a g e   s e r v i c e s .
-a “”
name= 軟件包名
state=

started
stopped
restarted

enabled=
runlevel=

group: A d d   o r   r e m o v e   g r o u p s

增加或刪除一個用戶組
-a “”
name=
state=
system=
gid=

user:M a n a g e   u s e r   a c c o u n t s

管理用戶

-a “”
name=
group=
groups=
comment=
uid=
system=
shell=
expires=
home=

setup:G a t h e r s   f a c t s   a b o u t   r e m o t e   h o s t s

YAML:
YAML is a data serialization format designed for human readability and  interaction with scripting languages.

數據結構:
key:value

– item1
– item2
– item3

{name:jerry, age:21}

PlayBook:

核心元素:

Tasks:任務,由模塊定義的操作的列表;
Variables:變量
Templates:模板,即使用了模板語法的文本文件;
Handlers:由特定條件觸發的Tasks;
Roles:角色;

playbook的基礎組件:

Hosts:運行指定任務的目標主機;
remote_user:在遠程主機以哪個用戶身份執行;
sudo_user:非管理員需要擁有sudo權限;
tasks:任務列表

模塊,模塊參數:

格式:

(1) action: module arguments
(2) module: arguments

Playbook準備 :(不知道為什么, 在openSUSE出現了playbook總是不能正確運行的錯誤)

201611061445002

201611061445001

201611061447001

原創文章,作者:M20-石俊,如若轉載,請注明出處:http://www.www58058.com/58695

(0)
M20-石俊M20-石俊
上一篇 2016-11-09 14:07
下一篇 2016-11-09 15:06

相關推薦

  • 20160804課堂練習

    grep正則表達式課堂練習 1、找出ifconfig命令結果中本機的所有IPv4地址 # ifconfig | head -2 | cut -dt -f2 | cut -dn -f1 | tail -1 2、  查出分區空間使用率的最大百分比值 # df | cut -c56-58 | sort -n | tail -1 3、  查出用戶…

    Linux干貨 2016-08-05
  • 學習Linux的第一周 之screen

         1.來到馬哥的感想:      已經接觸Linux已經一周,在緊張的而又條理的學習當中,我學到了很多東西單說量我就很佩服自己,在大學的兩年當中已經快把自己學習新知識的渴望忘記了!來到馬哥這短短一周我感覺我真的長大了很多,我把手機王者榮耀卸載了。只想對自己說加油堅持下去。…

    Linux干貨 2017-07-16
  • 你收到來自一個來自noob的blog,請注意查收~

                  本人較為懶,部分內容為直接cp,勿怪                 圖片借鑒余知乎某位深藏不漏的用戶 關于ls命令輸出結果的詳解:     1 如何知道自己當前…

    2017-09-03
  • python高階函數與裝飾器

    ##**高階函數**– 函數是python中的一等公民– 函數也是對象,可調用對象– 函數可以作為普通變量、參數、返回值等等– 數學概念y = g(f(x))– 高階函數滿足以下至少一個條件:1.接收一個或多個函數作為參數 2.輸出一個函數 ##**舉例(計數器)**def counter(base…

    Linux干貨 2017-10-23
  • 命令行歷史和調用命令參數

    history顯示當前終端的歷史記錄 !! 重復執行上一條命令 !-n 重復執行倒數第n條命令 !n 重復執行第n條命令 ctrl+p 等于向上方向鍵,翻出上一條命令。 ctrl+n 等于向下方向鍵,翻出下一條命令。 ctrl+j 等于enter 執行當前命令 !:0 執行前一條命令(去除參數) !string 重復前一個以“string”開頭的命令(只限于…

    2017-07-14
  • week3

    一,列出當前系統上所有已經登錄的用戶的用戶名,注意,同一個用戶登錄多次只顯示一次即可 who | cut -d' ' -f1 | sort -u 二,取出最后登錄到當前系統的用戶相關信息 who | cut -d'&nb…

    Linux干貨 2016-11-15
欧美性久久久久