1饭望、測試客戶端準(zhǔn)備
使用pxe安裝3臺主機悔政,分別命名為node1、node2桥言、node3
10.50.6.8 node1
10.50.6.9 node3 將ansible安裝到此主機上
10.50.6.10 node2
2萌踱、登錄到node3安裝ansible
# 下載epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
# 安裝ansible
yum install -y ansible
3、配置ssh-key后續(xù)批量管理的時候不需要輸入密碼
[root@node3]# ssh-keygen -t rsa -P "" #一路回車
[root@node3]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.50.6.8
[root@node3]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.50.6.10
4号阿、編輯/etc/ansible/hosts文件虫蝶,定義管理主機
vim /etc/ansible/hosts
[webserver] #名稱隨意,禁止中文
10.50.6.10
10.50.6.8
[dbservers]
10.50.6.11
#保存退出
5倦西、創(chuàng)建playbooks的yml文件
vim install_httpd.yml
---
- hosts: webserver
remote_user: root
tasks:
- name: install httpd
yum: name=httpd
- name: start httpd
service: name=httpd state=started enabled=yes
- name: enable port
shell: firewall-cmd --zone=public --add-port=80/tcp --permanent;systemctl restart firewalld.service
# 保存退出
#測試一下
ansible-playbook -C install_httpd.yml
PLAY [webserver] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
ok: [10.50.6.10]
ok: [10.50.6.8]
TASK [install httpd] *********************************************************************************************************************************************************************************************************************
ok: [10.50.6.10]
ok: [10.50.6.8]
TASK [start httpd] ***********************************************************************************************************************************************************************************************************************
ok: [10.50.6.8]
ok: [10.50.6.10]
TASK [enable port] ***********************************************************************************************************************************************************************************************************************
skipping: [10.50.6.10]
skipping: [10.50.6.8]
PLAY RECAP *******************************************************************************************************************************************************************************************************************************
10.50.6.10 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
10.50.6.8 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
#開始安裝
ansible-playbook install_httpd.yml
PLAY [webserver] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
ok: [10.50.6.8]
ok: [10.50.6.10]
TASK [install httpd] *********************************************************************************************************************************************************************************************************************
ok: [10.50.6.10]
ok: [10.50.6.8]
TASK [start httpd] ***********************************************************************************************************************************************************************************************************************
ok: [10.50.6.10]
ok: [10.50.6.8]
TASK [enable port] ***********************************************************************************************************************************************************************************************************************
changed: [10.50.6.10]
changed: [10.50.6.8]
PLAY RECAP *******************************************************************************************************************************************************************************************************************************
10.50.6.10 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.50.6.8 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
無報錯即成功