Ansible 系列之 Ad-Hoc介紹及使用

Ad-Hoc 介紹html

1、什麼是ad-hoc 命令?node

ad-hoc 命令是一種能夠快速輸入的命令,並且不須要保存起來的命令。就至關於bash中的一句話shell。這也是一個好的地方,在學習ansible playbooks時能夠先了解另一種ansible基本的快速用法,不必定非要寫一個palybook文件。python

通常來講,ansible的強大之處在於它的playbook 劇本。但爲何咱們還要使用這種臨時的命令呢?git

臨時命令適用於下面相似的場景,若是你想在聖誕節到來之時,關掉實驗室的電腦,只須要ansible 的一行命令便可,而沒必要編寫一個playbook文件來完成這個工做。github

不過,對於配置管理和應用部署這種工做,仍是須要使用「/usr/bin/ansible-playbook」命令。web

一、並行和Shell 命令docker

接上文,ansible 服務器已經配置好使用密鑰進行認證,管理主機,若是不想使用密鑰的話,那麼可使用--ask-pass (-k) 來用密碼管理。可是最好仍是用密鑰的方式。shell

以下:使用如下命令來查看webserver 組內主機的端口開放情況:vim

[root@docker ~]# ansible webserver -a 'netstat -ulntp'      
172.17.0.3 | SUCCESS | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   

web1 | SUCCESS | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:222             0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::222                  :::*                    LISTEN      -         

命令的最後也能夠加 -f number ,表示使用的併發進程數目,默認是5個,以下:bash

ansible webserver -a 'netstat -ulntp' -f 15

/usr/bin/ansible 默認使用當前ansible 服務器登錄的用戶來進行管理,若是你不喜歡這樣,也可使用 -u username 的方式來指定用戶,以下:

注:(zhangsan 這個用戶必須是被管理主機上真實存在的)

[root@docker ~]# ansible webserver -a "w" -u zhangsan -k

若是你不想使用當前的用戶來管理運行命令,也可使用 --become -K 命令提高權限.

 

以上是關於ansible 的基礎,ansible 有許多的模塊,以上的栗子中,沒有指定模塊,由於 默認的模塊是 command ,若是要想使用其它模塊,能夠用-m 模塊名 來指定。

注:command 模塊不支持擴展的shell語法,如使用管道和重定向。固然若是須要特殊的shell 語法,可使用shell模塊來完成任務。像下面這樣:

[root@docker ~]# ansible webserver -m shell -a 'echo $TERM'
web1 | SUCCESS | rc=0 >>
xterm-256color

172.17.0.3 | SUCCESS | rc=0 >>
xterm-256color

 

二、文件傳輸管理

這裏是/usr/bin/ansible 命令行的另一個用例,Ansible 能夠將多個文件併發的拷貝到多臺機器上。使用 copy 模塊,將文件直接傳輸到多個服務器上,以下:

[root@docker ~]# ansible webserver -m copy -a "src=/etc/hosts dest=/tmp/hosts"
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "checksum": "ba0ed35ca3f16342b883784ec7928491d359b8ab", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9e979f3a6509f8d829209613343f90b9", 
    "mode": "0644", 
    "owner": "root", 
    "size": 117, 
    "src": "/root/.ansible/tmp/ansible-tmp-1487773694.97-103709947729677/source", 
    "state": "file", 
    "uid": 0
}
web1 | SUCCESS => {
    "changed": true, 
    "checksum": "ba0ed35ca3f16342b883784ec7928491d359b8ab", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9e979f3a6509f8d829209613343f90b9", 
    "mode": "0644", 
    "owner": "root", 
    "size": 117, 
    "src": "/root/.ansible/tmp/ansible-tmp-1487773694.94-149872215856203/source", 
    "state": "file", 
    "uid": 0
}

檢查一下:

[root@docker ~]# ansible webserver -a 'stat /tmp/hosts'
web1 | SUCCESS | rc=0 >>
  File: '/tmp/hosts'
  Size: 117           Blocks: 8          IO Block: 4096   regular file
Device: fc03h/64515d    Inode: 25186117    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-02-22 22:28:58.946882890 +0800
Modify: 2017-02-22 22:28:15.001562188 +0800
Change: 2017-02-22 22:28:15.355564788 +0800
 Birth: -

172.17.0.3 | SUCCESS | rc=0 >>
  File: '/tmp/hosts'
  Size: 117           Blocks: 8          IO Block: 4096   regular file
Device: fc02h/64514d    Inode: 41950463    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-02-22 22:28:58.949882912 +0800
Modify: 2017-02-22 22:28:15.041562482 +0800
Change: 2017-02-22 22:28:15.349564744 +0800
 Birth: -

 

說下另一個模塊 file ,它容許更改文件的宿主以及權限,這些相同的選項一樣適用 copy 模塊,以下:

[root@docker ~]# ansible webserver -m file -a "dest=/tmp/hosts mode=600"
web1 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "root", 
    "path": "/tmp/hosts", 
    "size": 117, 
    "state": "file", 
    "uid": 0
}
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "root", 
    "path": "/tmp/hosts", 
    "size": 117, 
    "state": "file", 
    "uid": 0
}

更改文件的宿主以及屬組:

[root@docker ~]# ansible webserver -m file -a "dest=/tmp/hosts mode=600 owner=zhangsan group=zhangsan"
web1 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "zhangsan", 
    "mode": "0600", 
    "owner": "zhangsan", 
    "path": "/tmp/hosts", 
    "size": 117, 
    "state": "file", 
    "uid": 1000
}
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "zhangsan", 
    "mode": "0600", 
    "owner": "zhangsan", 
    "path": "/tmp/hosts", 
    "size": 117, 
    "state": "file", 
    "uid": 1000
}

 本文屬於做者原創,轉載請註明出處:飛走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html

使用file 模塊來建立目錄,相似於 mkdir -p,以下:

[root@docker ~]# ansible webserver -m file -a "dest=/tmp/zhangsan/pp/1 mode=755 owner=zhangsan group=zhangsan state=directory"
web1 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "zhangsan", 
    "mode": "0755", 
    "owner": "zhangsan", 
    "path": "/tmp/zhangsan/pp/1", 
    "size": 6, 
    "state": "directory", 
    "uid": 1000
}
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "zhangsan", 
    "mode": "0755", 
    "owner": "zhangsan", 
    "path": "/tmp/zhangsan/pp/1", 
    "size": 6, 
    "state": "directory", 
    "uid": 1000
}

以及刪除目錄(遞歸)和刪除文件,以下:

[root@docker ~]# ansible webserver -m file -a "dest=/tmp/zhangsan/pp/1 state=absent"
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/zhangsan/pp/1", 
    "state": "absent"
}
web1 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/zhangsan/pp/1", 
    "state": "absent"
}

 

3.軟件包管理

包括yum 和 apt,如下是一些yum 的示例。

確保該軟件包已經安裝,但不要更新它,至關於檢查改軟件是否安裝:

[root@docker ~]# ansible webserver -m yum -a "name=vim state=present"
172.17.0.3 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "vim-enhanced-2:7.4.160-1.el7_3.1.x86_64 providing vim is already installed"
    ]
}
web1 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "vim-enhanced-2:7.4.160-1.el7_3.1.x86_64 providing vim is already installed"
    ]
}

 

確保軟件安裝的是最新的版本,以下:

[root@docker ~]# ansible webserver -m yum -a "name=vim state=latest"
172.17.0.3 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "All packages providing vim are up to date", 
        ""
    ]
}
web1 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "All packages providing vim are up to date", 
        ""
    ]
}

 

確保軟件沒有被安裝:

[root@docker ~]# ansible webserver -m yum -a "name=vim state=absent"

 

4.用戶和組管理

"user" 模塊容許輕鬆的建立和管理現有的用戶帳號,以及刪除可能存在的用戶帳號,以下:

建立一個用戶,並設置密碼(這裏的密碼必須是加密後的。這裏有坑,若是你寫成了明文的密碼如如:123456,那麼系統的root密碼就是未知(/etc/shadow文件中,該用戶的密碼位置那就變成123456了,即誤搞成加密後的密碼是123456了!)

[root@docker ~]# ansible webserver -m user -a "name=xiaoming password=securitytext"
web1 | SUCCESS => {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 1001,
    "home": "/home/xiaoming",
    "name": "xiaoming",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
172.17.0.3 | SUCCESS => {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 1001,
    "home": "/home/xiaoming",
    "name": "xiaoming",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}

 

本文屬於做者原創,轉載請註明出處:飛走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html

建立用戶時使用加密後的密碼來設置,其它方法可參考這裏

先用python 的 crypt模塊來對密碼 進行加密,如:

[root@docker ~]# python -c 'import crypt; print crypt.crypt("123456", "hello")'
heepn6ZumUmSE

使用上述密碼,建立用戶:

[root@docker ~]# ansible webserver -m user -a "name=huahua shell=/bin/bash password=heepn6ZumUmSE update_password=always"
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 1003, 
    "home": "/home/huahua", 
    "name": "huahua", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1003
}
web1 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 1003, 
    "home": "/home/huahua", 
    "name": "huahua", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1003
}

 

刪除用戶並移除用戶家目錄(remove 要和 state 參數一塊兒使用,至關於userdel -r):

[root@docker ~]# ansible webserver -m user -a "name=xiaoming state=absent remove=yes"
172.17.0.3 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "xiaoming",
    "remove": true,
    "state": "absent"
}
web1 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "xiaoming",
    "remove": true,
    "state": "absent"
}

 

 5.從版本控制中部署程序

直接從git 上部署web 應用

使用 git模塊,要先保證遠程主機上有git軟件,以下所示,檢查git 已被安裝:

[root@docker ~]# ansible webserver -m yum -a "name=git state=present"
172.17.0.3 | SUCCESS => {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "git-1.8.3.1-6.el7_2.1.x86_64 providing git is already installed"
    ]
}
web1 | SUCCESS => {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "git-1.8.3.1-6.el7_2.1.x86_64 providing git is already installed"
    ]
}

確保已經安裝以後,再來從git上拉取源碼,以下:

[root@docker ~]# ansible webserver -m git -a "repo=git://github.com/aliasmee/hello.git dest=/usr/myapp version=HEAD"
web1 | SUCCESS => {
    "after": "f102d1927c4d42cfcca42aaa8e961be4c0b06e00",
    "before": null,
    "changed": true,
    "warnings": []
}
172.17.0.3 | SUCCESS => {
    "after": "f102d1927c4d42cfcca42aaa8e961be4c0b06e00",
    "before": null,
    "changed": true,
    "warnings": []
}

驗證一下:

[root@docker ~]# ansible webserver -a "ls /usr/myapp"
172.17.0.3 | SUCCESS | rc=0 >>
README.md
cpu_load.sh
diyHttpServer.py
look_IP.sh
one.py
two.txt

web1 | SUCCESS | rc=0 >>
README.md
cpu_load.sh
diyHttpServer.py
look_IP.sh
one.py
two.txt

 

6.服務管理
 
確保http服務是打開的狀態:
ansible webserver -m service -a "name=httpd state=started"

重啓webserver組內的 web服務器:

ansible webserver -m service -a "name=httpd state=restarted"

很遺憾,個人測試環境中,由於被管理機器都是docker 容器,並且 ansible 的 service 模塊,官方發文說如今還不支持容器的服務支持。詳見此頁面:https://github.com/ansible/ansible-modules-core/issues/4024

 

7.收集信息

Facts就是主機上已經發現的變量,在playbooks中有描述。能夠用於實現指定的任務的條件或者獲取特定的信息,能夠經過下面來得到全部 facts:

[root@docker ~]# ansible all -m setup

 

8.腳本模塊

scripts 腳本模塊採用腳本名稱,後面跟空格分隔的參數列表組成,以下所示:

[root@docker ~]# ansible webserver -m script -a "/tmp/myapp/cpu_load.sh"

上面栗子中,位於本地路徑的腳本將被傳輸到遠程主機上並執行,適合本地寫好的安裝程序腳本,或其它自定義腳本。

 好吧,模塊還有不少不少,具體的只有等用到時仔細研究了,下一篇開始進入playbooks 的學習了。新手上路,文中若是有錯誤的地方,還請大牛們多多指教。

 

 

本文屬於做者原創,轉載請註明出處:飛走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html

 

參考資料連接:http://docs.ansible.com/ansible/intro_adhoc.html

相關文章
相關標籤/搜索