掃二維碼與項目經理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯網交流
Ansible是一種新流行的自動化運維工具,基于python2-paramiko模塊開發(fā),集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優(yōu)點,實現了批量系統配置、批量程序部署、批量運行命令功能。

成都創(chuàng)新互聯始終堅持【策劃先行,效果至上】的經營理念,通過多達10年累計超上千家客戶的網站建設總結了一套系統有效的網絡營銷推廣解決方案,現已廣泛運用于各行各業(yè)的客戶,其中包括:玻璃鋼坐凳等企業(yè),備受客戶表揚。
測試主機是否是通的,用法很簡單,不涉及參數:
[root@361way ~]# ansible 10.212.52.252 -m ping
10.212.52.252 | success >> {
"changed": false,
"ping": "pong"
}
setup模塊,主要用于獲取主機信息,在playbooks里經常會用到的一個參數gather_facts就與該模塊相關。setup模塊下經常使用的一個參數是filter參數,具體使用示例如下(由于輸出結果較多,這里只列命令不寫結果):
[root@361way ~]# ansible 10.212.52.252 -m setup -a 'filter=ansible_*_mb'
//查看主機內存信息
[root@361way ~]# ansible 10.212.52.252 -m setup -a 'filter=ansible_eth[0-2]'
//查看地接口為eth0-2的網卡信息
[root@361way ~]# ansible all -m setup --tree /tmp/facts
//將所有主機的信息輸入到/tmp/facts目錄下,每臺主機的信息輸入到主機名文件中(/etc/ansible/hosts里的主機名)
file模塊主要用于遠程主機上的文件操作,file模塊包含如下選項:
使用示例:
ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"
ansible test -m file -a "path=/tmp/fstab state=absent"
ansible test -m file -a "path=/tmp/test state=touch"
復制文件到遠程主機,copy模塊包含如下選項:
示例如下:
ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"
ansible test -m copy -a "src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'"
用于管理服務 該模塊包含如下選項:
arguments:給命令行提供一些選項
enabled:是否開機啟動 yes|no
name:必選項,服務名稱
pattern:定義一個模式,如果通過status指令來查看服務的狀態(tài)時,沒有響應,就會通過ps指令在進程中根據該模式進行查找,如果匹配到,則認為該服務依然在運行
runlevel:運行級別
sleep:如果執(zhí)行了restarted,在則stop和start之間沉睡幾秒鐘
state:對當前服務執(zhí)行啟動,停止、重啟、重新加載等操作(started,stopped,restarted,reloaded)
使用示例:
# Example action to reload service httpd, in all cases
- service: name=httpd state=reloaded
# Example action to enable service httpd, and not touch the running state
- service: name=httpd enabled=yes
# Example action to start service foo, based on running process /usr/bin/foo
- service: name=foo pattern=/usr/bin/foo state=started
# Example action to restart network service for interface eth0
- service: name=network state=restarted args=eth0
示例:
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'
ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root ansible 10.212.52.252 -m cron -a 'backup="True" name="test" minute="0" hour="2" job="ls -alh > /dev/null"' ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
使用yum包管理器來管理軟件包,其選項有:
示例如下:
ansible test -m yum -a 'name=httpd state=latest'
ansible test -m yum -a 'name="@Development tools" state=present'
ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/ nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
user模塊是請求的是useradd, userdel, usermod三個指令,goup模塊請求的是groupadd, groupdel, groupmod 三個指令,具體參數這里不再細講,直接上示例。 1、user模塊示例:
- user: name=johnd comment="John Doe" uid=1040 group=admin
- user: name=james shell=/bin/bash groups=admins,developers append=yes
- user: name=johnd state=absent remove=yes
- user: name=james18 shell=/bin/zsh groups=developers expires=1422403387
#生成密鑰時,只會生成公鑰文件和私鑰文件,和直接使用ssh-keygen指令效果相同,不會生成authorized_keys文件。
- user: name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa
注:指定password參數時,不能使用后面這一串密碼會被直接傳送到被管理主機的/etc/shadow文件中,所以需要先將密碼字符串進行加密處理。然后將得到的字符串放到password中即可。
[root@361way ~]# openssl passwd -1 -salt $( Password: $1$YngB4z8s$atSVltYKnDxJmWZ3s.4/80 或者 [root@361way ~]# echo "123456" | openssl passwd -1 -salt $( | head -c 32) -stdin $1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0 #經驗證下面生成的密碼串也可以正常使用,不過與/etc/shadow的格式不統一,不建議使用 [root@361way ~]# openssl passwd -salt -1 "123456" -1yEWqqJQLC66 #使用上面的密碼創(chuàng)建用戶 [root@361way ~]#ansible all -m user -a 'name=foo password="$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0"'
不同的發(fā)行版默認使用的加密方式可能會有區(qū)別,具體可以查看/etc/login.defs文件確認,centos 6.5版本使用的是SHA512加密算法,生成密碼可以通過ansible官方給出的示例:
python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt (getpass.getpass())"
2、group示例
- group: name=somegroup state=present
使用rsync同步文件,其參數如下:
另外還有其他參數,這里不再一一說明。上幾個用法:
src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
src=some/relative/path dest=/some/absolute/path archive=no links=yes
src=some/relative/path dest=/some/absolute/path checksum=yes times=no
src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull
示例:
name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present
name=/srv/disk src='LABEL=SOME_LABEL' state=present
name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present
ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
ansible test -a 'losetup /dev/loop0 /disk.img'
ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0'
ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
該模塊主要用于從http、ftp、https服務器上下載文件(類似于wget),主要有如下選項:
sha256sum:下載完成后進行sha256 check;
timeout:下載超時時間,默認10s
url:下載的URL
url_password、url_username:主要用于需要用戶名密碼進行驗證的情況
use_proxy:是事使用代理,代理需事先在環(huán)境變更中定義
示例:
- name: download foo.conf
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440
- name: download file with sha256 check
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
模塊部分就先介紹到這里吧,官方提供的可能用到模塊有git、svn版本控制模塊,sysctl 、authorized_key_module系統模塊,apt、zypper、pip、gem包管理模塊,find、template文件模塊,mysql_db、redis數據庫模塊,url 網絡模塊等。具體可以參看官方手冊模塊部分

我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯網交流