标签:通知 文件的 文件中 color 名称 系统版本 item mysq package
实体机管理:pxe, cobbler虚拟机管理:
不能直接把服务器直接down掉,因为当前主机的服务还有其他主机进行连接
ansible:命令直接执行ansible-playbook:提前定义好流程,使用剧本
ansible all -m group -a "name=test state=present system=1 gid=600"
ansible all -m group -a "name=test2 state=present gid=1010"
ansible facts:系统变量可以直接调用ansible-playbook -e "var=value":自己定义变量,传递变量host variable: host iventory:定义主机变量,在hosts文件中添加group variable:组变量定义方式。定义的变量对于组内所有主机生效[groupname:vars]var=valueroles
字面量:
字符串:使用单引号或双引号;数字:整数,浮点数;列表:[item1, item2, ...],值可变数据类型元组:(item1, item2, ...),值不可变字典:{key1:value1, key2:value2, ...}布尔型:true/false
算术运算:+, -, *(乘), /(除), //(整除), %(取余), **(次方)比较操作:==(等值比较), !=(不等值比较), >, >=, <, <=逻辑运算:and, or, not
worker_processes {{ ansible_processor_vcpus }};
#或者worker_processes {{ ansible_processor_vcpus-1 }}; #使用减法运算
lsiten {{ http_port }}; #设置监听端口,也可以使用我们自定义的变量(在hosts文件中对每一个分别设置)
- hosts: websrvs
remote_user: root
tasks:
- name: install nginx
yum: name=nginx state=present
- name: install conf file
template: src=files/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
tags: instconf
- name: start nginx service
service: name=nginx state=started
handlers:
- name: restart nginx
service: name=nginx state=restarted
ansible-playbook nginx.conf.yaml
ansible all -m setup | grep ansible_os_family #查看系统家族,CentOS显示RedHat
tasks:
- name: install conf file to centos7
template: src=files/nginx.conf.c7.j2
when: ansible_distribution_major_version == "7" #如果是CentOS7的时候
- name: install conf file to centos6
template: src=files/nginx.conf.c6.j2
when: ansible_distribution_major_version == "6" #如果是CentOS6时
scp root@172.18.100.69:/etc/nginx/nginx.conf files/nginx.conf.c6.j2 #把远程主机(CentOS6)上的文件拉取到本地
- hosts: all
remote_user: root
tasks:
- name: install some packages
yum: name={{ item }} state=present
with_items:
- nginx
- memcached
- php-fpm
ansible-playbook item.yaml #如果没有定义好yum源,出现问题就会终止
- hosts: all
remote_user: root
tasks:
- name: add some groups
group: name={{ item }} state=present
with_items:
- group11
- group12
- group13
- name: add some users
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: ‘user11‘, group: ‘group11‘ } #键值与键值之间用“:”隔开
- { name: ‘user12‘, group: ‘group12‘ }
- { name: ‘user13‘, group: ‘group13‘ }
ansible-playbook useradd.yaml
mkdir -pv roles/{httpd,nginx,mysql}/{files,templates,tasks,handlers,vars,meta,default}
mkdir /etc/ansible/roles/nginx
cp /root/working/files/nginx.conf.j2 templates/ #复制准备好的配置文件/templates文件夹中
#在roles/nginx/tasks/main.yaml中配置
- name: install nginx packages
yum: name=nginx state=present
- name: install conf file
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: start nginx
service: name=nginx state=started enabled=true
#创建执行文件nginx.yaml
- hosts: all
remote_user: root
roles:
- nginx
ansible-playbook --check nginx.yaml
ansible-playbook nginx.yaml
#创建roles/nginx/handlers/main.yaml
- name: restart nginx
service: name=nginx state=restarted
#在tasks/main.yaml中重新定义
- name: install nginx packagesyum: name=nginx state=presenttags:install- name: install conf filetemplate: src=nginx.conf.j2 dest=/etc/nginx/nginx.confnotify: restart nginx #定义通知机制,当模版发生改变时就通知handlers模块,重启服务tags:conf- name: start nginxservice: name=nginx state=started enabled=truetags:nginxst
ansible-playbook -t install,conf nginx.yaml
#编辑user.yaml
- hosts: all
romote_user: root
vars:
- username: testuser1
- groupname: testgroup1
tasks:
- name: creat group
group: name={{ groupname }} state=present
- name: creat user
user: name={{ username }} state=present
ansible-playbook -e "groupname=mygrp1" -e "username=myuser1" myuser.yaml
#在要传给远程主机的nginx配置文件中修改
user {{ username }};
#在roles/nginx/vars/main.yaml
- username: daemon #一定要确保定义的用户在远程主机上存在(前面不需要再添加横线“-”)
ansible-playbook --check nginx.yaml
ansible-playbook nginx.yaml
ansibel-playbook -e "username=adm" --check nginx.yaml
ansibel-playbook -t conf -e "username=adm" nginx.yaml #直接修改
- hosts: all
remote_user: root
roles:
- { role: nginx, username: nginx }
- hosts: all
remote_user: root
roles:
- { role: nginx, username: nginx, when: "ansible_distribution_major_version == ‘7‘ " }
mkdir -pv /etc/ansible/roles/memcached/tasks
vim tasks/main.yaml
#下面是main.yaml的内容
- name: install package
yum: name=memcached state=present
- name: start memcached
service: name=memcached state=started enabled=true
vim mem.yaml
#下面是mem.yaml的内容
- hosts: all
remote_user: root
roles:
- { role: nginx, when: ansible_distribution_major_version == ‘7‘ }
- { role: memcached, when: ansible_hostname == ‘memcached‘ }
- ansible-playbook mem.yaml
mkdir roles/memcached/templates
scp root@172.18.100.69:/etcsysconfig/memcached ./roles/memcached/templates #从远程主机上下载配置文件
ansible all -m setup
vim ./roles/memcached/templates/memcached
#将CACHESIZE="64"改为
CACHESIZE="{{ ansible_memtotal_mb //4 }}"
mv ./roles/memcached/templates/memcached{,.j2} #改后缀名j2
vim ./roles/memcached/tasks/main.yaml
#以下是main.yaml中的内容
- name: install package
yum: name=memcached state=present
- name: install conf file
templates: src=/etc/ansible/roles/templates/memcached.j2 dest=/etc/sysconfig/memcached
notify: restart memcached
tags: memconf
- name: start memcached
service: name=memcached state=started enabled=true
mkdir memcached/handlers
vim ./handlers/main.yaml
#main.yaml
- name: restart memcached
service: name=memcached state=restarted
ansible-playbook /etc/ansible/roles/mem.yaml
- 或者ansible-playbook -t memconf /etc/ansible/roles/mem.yaml
标签:通知 文件的 文件中 color 名称 系统版本 item mysq package
原文地址:http://www.cnblogs.com/zhangpf/p/7407088.html