一、安裝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'