一、安装ansiblenode
rpm -ivh https://mirrors.aliyun.com/epel/6Server/x86_64/Packages/e/epel-release-6-8.noarch.rpmmysql
yum -y install ansibleweb
ssh-keygen -t rsa -P ''sql
ssh-copy-id -i /root/.ssh/id_rsa.pub root@node1.ha.comshell
ssh-copy-id -i /root/.ssh/id_rsa.pub root@node2.ha.comapache
ssh-copy-id -i /root/.ssh/id_rsa.pub root@node3.ha.comssh
ssh-copy-id -i /root/.ssh/id_rsa.pub root@node4.ha.comide
二、ansible主机配置ui
grep -v ^# /etc/ansible/hosts |grep -v ^$spa
[web]
node1.ha.com
node2.ha.com
[mysql]
node3.ha.com
node4.ha.com
三、ansible简单应用
查看命令帮助
ansible-doc -s group
检测主机是否在线
ansible all -m ping
在远程主机执行命令并返回结果[-m command]可省
ansible all [-m command]-a 'date'
复制文件
ansible mysql -m copy -a "src=/etc/sysconfig/network-scripts/ifcfg-eth0 dest=/tmp"
crontab计划任务
ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="usr/sbin/ntpdate ntp2.aliyun.com"'
建立用户组和用户
ansible all -m group -a "gid=306 system=yes name=mysql"
ansible all -m user -a "group=mysql name=mysql uid=306 shell=/sbin/nologin"
删除用户和用户组
ansible all -m user -a "state=absent name=mysql"
ansible all -m group -a "state=absent name=mysql"
yum安装软件
ansible all -m yum -a "state=present name=httpd"
启动服务
ansible all -m service -a "state=started name=httpd enabled=yes"
中止服务
ansible all -m service -a "state=stopped name=httpd enabled=no"
yum卸载软件
ansible web -m yum -a "state=removed name=httpd"
四、ansible高级应用ansible-playbook
安装httpd,经过控制机复制配置文件到被控制机,并重启被控制器上的httpd服务
cat httpd.yaml
- hosts : all
remote_user : root
tasks :
- name : ensure apache latest version
yum : state=latest name=httpd
- name : apache configure file
copy : src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf force=yes
notify :
- restart httpd
handlers :
- name : restart httpd
service : name=httpd state=restarted
ansible-playbook httpd.yaml
ansible all -a 'rpm -q httpd'