https://github.com/ansible/ansible-examples 示例
执行 ansible-playbook nginx.yml -f 10
-u REMOTE_USER:手工指定远程执行playbook的系统用户;
--syntax-check:检查playbook的语法;
--list-hosts playbooks:匹配到的主机列表;
--step:以单任务分步骤运行,方便做每一步的确认工作
nginx.yml yaml语法
---
- hosts: 192.168.8.22 #操作对象,之前定义的主机或主机组
vars: #定义变量
worker_processes: 4
num_cpus: 4
max_open_file: 65506
root: /data
remote_user: root #远程操作用户名,支持sudo方式运行,添加sudo:yes
tasks: #任务列表
- name: Install Nginx...... #运行时会输出,默认使用action(具体的执行动作)
yum: pkg=nginx state=latest
- name: write the nginx config file
template: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf
#参数使用key=value的格式,定义任务时也可以引用变量,/tmp/nginx.conf{{ root }}
notify: #触发handlers定义的程序
- restart nginx
- name: Nginx is running......
service: name=nginx state=started
handlers: #定义的处理程序在没有通知触发时是不会执行的,触发后也只会运行一次
- name: restart nginx
service: name=nginx state=restarted
cat /tmp/nginx.conf
......
user nginx;
worker_processes {{ worker_processes }};
{% if num_cpus == 2 %}
worker_cpu_affinity 01 10;
{% elif num_cpus == 4 %}
worker_cpu_affinity 1000 0100 0010 0001;
{% elif num_cpus >= 8 %}
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 0010000001000000 10000000;
{% else %}
worker_cpu_affinity 1000 0100 0010 0001;
{% endif %}
worker_rlimit_nofile {{ max_open_file }};
......
引用其他的playbook
tasks:
- include: tasks/foo.yml