ansible web -m setup
ansible_all_ipv4_addresses # ipv4的全部地址
ansible_all_ipv6_addresses # ipv6的全部地址
ansible_date_time # 获取到控制节点时间
ansible_default_ipv4 # 默认的ipv4地址
ansible_distribution # 系统
ansible_distribution_major_version # 系统的大版本
ansible_distribution_version # 系统的版本号
ansible_domain #系统所在的域
ansible_env #系统的环境变量
ansible_hostname #系统的主机名
ansible_fqdn #系统的全名
ansible_machine #系统的架构
ansible_memory_mb #系统的内存信息
ansible_os_family # 系统的家族
ansible_pkg_mgr # 系统的包管理工具
ansible_processor_cores #系统的cpu的核数(每颗)
ansible_processor_count #系统cp
u的颗数
ansible_processor_vcpus #系统cpu的总个数=cpu的颗数*CPU的核数
ansible_python # 系统上的pythoni
ansible cache -m setup -a 'filter=*processor*' # 用来搜索
处理器的数量表明CPU的颗数
每一个处理器的核心数量表明当前虚拟机可使用的内核数量
anxible cache -m setup -a 'filter=*processor*' #找关于processor的
a.*b和a*.b的区别
# . 表示匹配除换行以外的全部字符
# * 表示匹配量词以前的字符出现0次或屡次
# a.*b 能够匹配只要以a开头,以b结尾的字符串就能够
# a*.b 能够匹配a.b和.b
伪代码python
- hosts :wed
tasks:
- name: zzgbgn
by: zzdong
when: zz
- name: pzgbgn
by: pzdong
when: pz
若是a=="3",就将“大弦嘈嘈如急雨”,写入到web组下被管控机的/opt/.p2.yml中,若是a=="4",就将“小弦切切如私语”,写入到web组下被管控机的/opt/.p2.yml中nginx
- hosts: web
remote_user: root#表明用root用户执行,默认是root,能够省略
tasks:
- name: createfile
copy: content="大弦嘈嘈如急雨" dest=/opt/p2.yml
when: a=='3'
- name: createfile
copy: content="小弦切切如私语" dest=/opt/p2.yml
when: a=='4'
语法校验web
ansible-playbook --syntax-check p2.yml
执行redis
ansible-playbook -e 'a="3"' p2.yml #大弦嘈嘈如急雨
应用环境:不一样的系统,不一样的版本,不一样的环境,不一样的下载方式,使用的下载语句的不一样windows
伪代码架构
- hosts: web
tasks:
- name: wadong
tieqiao: wadong
- name: toukan
dong: toukan
tags: bianqian
只执行配置文件中的一个任务dom
- hosts: web
tasks:
- name: installnginx
yum: name=nginx
- name: copyfile
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
tags: copyfile
- name: start
service: name=nginx static=restarted
语法校验工具
ansible-playbook --syntax-check p3.yml
执行spa
ansible-playbook -t copyfile p3.yml
目的:一次性作多个类似的事情rest
- hosts :wed
tasks:
- name: qichuang
dong: {{ item }}
with_items:
- qichuang
- chifan
- shoushi
建立三个用户
- hosts: web
tasks:
- name: createruser
user: name={{ item }}
with_items:
- shy1
- shy2
- shy3
- name: creategroup
group: name={{ item }}
with_items:
- group1
- group2
- group3
语法校验
ansible-playbook --syntax-check p4.yml
执行
ansible-playbook p4.yml
用户shy1的属组是group1,用户shy2的属组是group2,用户shy3的属组是group3
- hosts: web
tasks:
- name: creategroup
group: name={{item}}
with_items:
- group3
- group4
- group5
- name: createuser
user: name={{item.user}} group={{item.group}}
with_items:
- {'user': shy3,'group': group3}
- {'user': shy4,'group': group4}
- {'user': shy5,'group': group5}
语法校验
ansible-playbook --syntax-check p5.yml
执行
ansible-playbook p5.yml
管控机上下载redis包
yum install -y redis
编写redis.conf配置文件
vi /etc/redis.conf
port 16379
bind {{ansible_default_ipv4.address}}
让web组的被管控机下载并启动redis
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=/etc/redis.conf dest=/etc/redis.conf
- name: start
service: name=redis state=started
语法校验
ansible-playbook --syntax-check p.yml
执行
ansible-playbook p6.yml
注:copy与template的区别
copy模块不替代参数,template模块替代参数
template的参数几乎与copy的参数彻底相同
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=redis.conf dest=/etc/redis.conf
- name: start
service: name=redis state=started
注:在当前目录(执行playbook的目录)下创建一个templates文件夹,将配置文件放在templates中,就可使用相对路径了
notify:触发
handlers:触发的动做
使用上场景:修改配置文件时
注:正常状况时handlers是不会执行的
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=redis.conf dest=/etc/redis.conf
tags: copyfile
notify: restart
- name: start
service: name=redis state=started
handlers:
- name: restart
service: name=redis
执行
ansible-playbook -t copyfile p7.yml
结果:复制文件并重启
经过规划角色来实现备份
目录结构特别清晰 能够重复调用别的用户建立的任务
注:通常状况下将roles写在/etc/ansible/roles中,也能够写在其余任意位置(写在其余位置要本身手动创建一个roles文件夹)
mkdir roles #建立一个roles文件夹 cd roles #进入该目录 mkdir web #分别建立三个与ansible组名相同的文件夹 mkdir db mkdir cache mkdir -pv web/{templates,files,tasks,vars} #templates:专门放置配置文件的文件夹,files:专门放置不须要传参的文件夹,tasks:用来保存任务,vars:用来保存变量 cd web/tasks #进入到tasks文件夹,编写任务(将多个任务拆开编写)
vi install.yml(具体任务的文件)
- name: install yum: name=redis
vi copyfile.yml(具体任务的文件)
- name: copyfile copy: src=redis.conf.j2 dest=/etc/redis.conf tags: copyfile notify: restart
注:jinjia2的后缀名是.j2
vi start.yml(具体任务的文件)
- name: start service: name=redis state=started
vi main.yml(执行以上三个文件的文件)
- import_tasks: install.yml - import_tasks: copyfile.yml - import_tasks: start.yml
准备copyfile.yml的配置文件(放到templates文件夹中)
cp /root/yaml/templates/redis.conf templates/redis.conf.j2 cd templates/redis.conf.j2
有触发事件的状况
cd web mkdir handlers vi handlers/main.yml
main.yml
- name: restart service: name=redis state=started
调用(web.yml在哪写均可以)
vi web.yml
- hosts: remote_user: root roles: - web
语法校验
ansible-playbook --syntax-check web.yml
执行
ansible-playbook web.yml
- roles - web - templates - redis.conf.j2 - files - tasks - main.yml(执行文件) - install.yml - copyfile.yml - start.yml - vars - handlers - main.yml(copyfile.yml的触发事件) - cache - db - tasks -createuser.yml - web.yml
准备任务
cd db mkdir tasks vi tasks/createuser.yml
createuser.yml
- name: createuser user: name=shy20
在web中执行db中的任务
cd web vi tasks/main.yml
main.yml
- import_tasks: install.yml - import_tasks: copyfile.yml - import_tasks: start.yml - import_tasks: roles/db/createuser.yml #加上这句话
语法校验
ansible-playbook --syntax-check web.yml
执行
ansible-playbook web.yml
####
同步时间的模块
yum install -y ntp ntpdate time.windows.com #同步时间 cp /user/share/zoneinfo/Asia/shanghai /etc/localtime #同步为上海时间
ansible web -m yum -a 'name=wget,lrzsz' #用逗号隔开