IP
用途
备注
192.168.10.11 ansible-server  
192.168.10.12 ansible-client  
192.168.10.13 ansible-client  

通过ansible-playbook 安装nginx服务

#tree  ansible

./
├── hosts                   #定义主机文件
├── nginx.conf.j2       #nginx配置文件
└── nginx.yaml          #ansible-playbook配置文件

主机配置文件

#cat hosts

[web]
192.168.10.12 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=xxxxxx
192.168.10.13 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=xxxxxx
[web:vars]
ansible_python_interpreter=/usr/bin/python2.6       #定义web组python路径

#nginx.conf.j2 配置文件,略过......

PS:worker_processes {{ ansible_processor_cores }};  #可根据CPU实际核心,获取取实际核数值

可根据nginx.conf配置修改

 

#nginx.yaml 配置文件

#cat nginx.yaml

---
- hosts: all
  tasks:
   - name: Install Nginx Package
     yum: name=nginx state=present

   - name: Copy Nginx.conf
     template: src=./nginx.conf.j2 dest=/etc/nginx/nginx.conf
           owner=root group=root mode=0644 validate='nginx -t -c %s'
     notify:
       - ReStart Nginx Service

  handlers:
   - name: ReStart Nginx Service
     service: name=nginx state=restarted

ansible-playbook 应用

检查语法

#ansible-playbook -i hosts nginx.yaml --syntax-check

查看目标主机

#ansible-playbook -i hosts  nginx.yaml --list-hosts

playbook: nginx.yaml

play #1 (all): host count=2
192.168.10.12
192.168.10.13

PS:-i 参数指定hosts文件,默认调用/etc/ansible/hosts 

安装 nginx

#ansible-playbook -i hosts nginx.yaml -f 2

PLAY [all] ********************************************************************

GATHERING FACTS *************************************************************** 
ok: [192.168.10.12]
ok: [192.168.10.13]

TASK: [Install Nginx Package] ************************************************* 
ok: [192.168.10.12]
ok: [192.168.10.13]

TASK: [Copy Nginx.conf] ******************************************************* 
ok: [192.168.10.12]
ok: [192.168.10.13]

PLAY RECAP ******************************************************************** 
192.168.10.12 : ok=3 changed=0 unreachable=0 failed=0 
192.168.10.13 : ok=3 changed=0 unreachable=0 failed=0

PS:-f 并发进程数,默认是5

指定task运行

#ansible-playbook -i hosts nginx.yaml -f 2 --start-at-task='Copy Nginx.conf'

PLAY [all] ********************************************************************

GATHERING FACTS *************************************************************** 
ok: [192.168.10.12]
ok: [192.168.10.13]

TASK: [Copy Nginx.conf] ******************************************************* 
ok: [192.168.10.12]
ok: [192.168.10.13]

PLAY RECAP ******************************************************************** 
192.168.10.12 : ok=2 changed=0 unreachable=0 failed=0
192.168.10.13 : ok=2 changed=0 unreachable=0 failed=0

检查80端口是否启动成功

#ansible web -i /root/ansible/hosts -m shell -a "netstat -ntlp | grep :80"   -o

192.168.10.12 | success | rc=0 | (stdout) tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 31416/nginx 
192.168.10.13 | success | rc=0 | (stdout) tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 30096/nginx

PS:ansible web -i /root/ansible/hosts 调用/root/ansible/hosts主机文件中的web组