Ansible架構:python
ansible是新出現的運維工具是基於Python研發的糅合了衆多老牌運維工具的優勢實現了批量操做系統配置、批量程序的部署、批量運行命令等功能。linux
和同類工具puppet和saltstack比起來優勢是更易於管理,不須要安裝客戶端(經過ssh鏈接通訊)web
ansible搭建以及配置:
1.ansible的安裝
#yum install ansible -y
[root@wy-pe1 ~]# rpm -ql ansible | head
/etc/ansible
/etc/ansible/ansible.cfg ansible的主配置文件
/etc/ansible/hosts ansible的Host Inventoy文件
/etc/ansible/roles
/usr/bin/ansible
/usr/bin/ansible-doc 模塊相關的命令
/usr/bin/ansible-galaxy
/usr/bin/ansible-playbook playbook相關命令
/usr/bin/ansible-pull
/usr/bin/ansible-vault
2.定義Host Inventory
# vim /etc/ansible/hosts
www.xxbandy.com 定義單個域名或主機
172.25.25.4
[webshosts] 定義一個服務羣組(能夠用來區分服務羣組)
10.45.249.119 ansible_ssh_user=root ansible_ssh_pass=wyadmin
10.45.249.121 ansible_ssh_user=root ansible_ssh_pass=wyadmin
注意:
ansible_ssh_user=root 定義ssh登陸用戶
ansible_ssh_pass=wyadmin 定義ssh登陸密碼
ansible_connection=ssh/local 定義主機鏈接類型
同時,必須主機必須記錄過一次ssh的密碼
經過隧道進行安全鏈接:
xxbandy ansible_ssh_port=55555 ansible_ssh_host=172.25.25.250
ansible中能夠使用range:
www[01:90].xxbandy.com
#ansible webshosts -a ‘/sbin/reboot’ -f 10 當即重啓webshosts中的主機
3.測試各個模塊
ansible命令最經常使用的用法:
ansible <Host-partten> -m MOD -a 'command'
所支持的模塊能夠使用ansible-doc -l查看
ansible簡單使用示例:
簡單使用copy模塊(注意,其中須要在各個主機上安裝libselinux-python包)
# ansible webshosts -m copy -a 'src=/root/command dest=/mnt'
10.45.249.119 | success >> {
"changed": false,
"checksum": "704ac4e439698bc9f1a314a9fce18fb658804d7c",
"dest": "/mnt/command",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/mnt/command",
"secontext": "system_u:object_r:mnt_t:s0",
"size": 79136,
"state": "file",
"uid": 0
}
10.45.249.121 | success >> {
"changed": true,
"checksum": "704ac4e439698bc9f1a314a9fce18fb658804d7c",
"dest": "/mnt/command",
"gid": 0,
"group": "root",
"md5sum": "b918049ba2652e9eb7bc1ad77e8cb8e4",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:mnt_t:s0",
"size": 79136,
"src": "/root/.ansible/tmp/ansible-tmp-1425651119.69-276531752760623/source",
"state": "file",
"uid": 0
}
表示文件已經向webshosts組內的主機同步成功
使用command或者shell模塊進行遠程調用系統命令:
[root@wy-pe1 mnt]# ansible webshosts -m command -a 'date'
10.45.249.119 | success | rc=0 >>
Fri Mar 6 22:15:09 CST 2015
10.45.249.121 | success | rc=0 >>
Fri Mar 6 21:56:03 CST 2015
[root@wy-pe1 mnt]# ansible webshosts -m command -a 'df -h'
10.45.249.121 | success | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 917M 16G 6% /
tmpfs 238M 0 238M 0% /dev/shm
/dev/sda1 477M 25M 427M 6% /boot
10.45.249.119 | success | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 936M 16G 6% /
tmpfs 238M 0 238M 0% /dev/shm
/dev/sda1 477M 25M 427M 6% /boot
在控制端添加用戶,user模塊:
查看user模塊的相關參數(ansible-doc user)
# ansible webshosts -m user -a 'name=andy comment="ansible add use" uid=1001 password=andyxxb'
參數詳解:name指定用戶名
comment指定描述
uid指定用戶ID(gid)
password指定用戶密碼
shell指定用戶所擁有的shell
state=absent用來刪除用戶(只會刪除本地用戶,不會刪除家目錄)
實現ssh祕鑰認證:(其實就是在ansible端進行key生成(ssh-keygen),而後使用copy模塊進行同步文件)
#ansible webshosts -m copy -a 'src=/root/.ssh/id_rsa.pub dest=/root'
# ansible webshosts -m shell -a 'cat /root/id_rsa.pub >> /root/.ssh/authorized_keys'
file模塊:用來改變文件的權限和所屬用戶組;同時還能夠建立目錄(沒有成功),刪除文件等
# ansible webshosts -m file -a 'dest=/mnt/command mode=600 owner=xxb group=xxb '
10.45.249.119 | success >> {
"changed": true,
"gid": 500,
"group": "xxb",
"mode": "0600",
"owner": "xxb",
"path": "/mnt/command",
"secontext": "system_u:object_r:mnt_t:s0",
"size": 79136,
"state": "file",
"uid": 500
}
10.45.249.121 | success >> {
"changed": true,
"gid": 502,
"group": "xxb",
"mode": "0600",
"owner": "xxb",
"path": "/mnt/command",
"secontext": "system_u:object_r:mnt_t:s0",
"size": 79136,
"state": "file",
"uid": 502
}
使用file模塊進行建立目錄:(mkdir -p)
# ansible webshosts -m file -a "dest=/mnt/hello mode=755 owner=xxb group=xxb state=directory"
使用file模塊進行刪除文件
# ansible webshosts -m file -a 'dest=/mnt/command state=absent'
使用yum模塊進行安裝軟件包,肯定包已經安裝,可是不進行udate操做:
# ansible webshosts -m yum -a 'name=vsftpd state=installed'
注意(state=latest用來確保軟件包是否爲最新版本)
使用service模塊進行肯定服務的運行狀態:
#ansible webshosts -m yum -a 'name=httpd state=installed'
# ansible webshosts -m service -a 'name=httpd state=restarted'
注意:state的狀態爲restarted,started,stoped
shell