ansible(三)

一.setup模块(收集信息 )

1.ansible中的setup模块可以收集到的信息

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*' # 用来搜索

2.虚拟机的设置

 

处理器的数量代表CPU的颗数
每个处理器的核心数量代表当前虚拟机可以使用的内核数量

3.使用setup模块时可以使用正则过滤

anxible cache -m setup -a 'filter=*processor*' #找关于processor的
注:a.b和a.b的区别
a.*b和a*.b的区别

# . 表示匹配除换行之外的所有字符
# * 表示匹配量词之前的字符出现0次或多次

# a.*b 可以匹配只要以a开头,以b结尾的字符串就可以
# a*.b 可以匹配a.b和.b

 

二.ansible-playbook模块

1.用when 来表示的条件判断

伪代码

- 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中

- 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'

语法校验

ansible-playbook  --syntax-check p2.yml

执行

ansible-playbook -e 'a="3"' p2.yml #大弦嘈嘈如急雨

应用环境:不同的系统,不同的版本,不同的环境,不同的下载方式,使用的下载语句的不同

2.标签(只执行配置文件中的一个任务)

伪代码

- hosts: web
tasks:
- name: wadong
  tieqiao: wadong
- name: toukan
  dong: toukan
  tags: bianqian

只执行配置文件中的一个任务

- 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

执行

ansible-playbook -t copyfile p3.yml 

3.循环with_items

目的:一次性做多个相似的事情

- 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 

4.循环嵌套

用户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 

 

5.template模块(与copy模块相似)

(1)让web组的被管控机下载并启动redis

管控机上下载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的参数完全相同
(2)相对路径
- 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中,就可以使用相对路径了

6.handlers(触发事件)

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

1.作用

通过规划角色来实现备份

2.优点

目录结构特别清晰
可以重复调用别的用户创建的任务

3.使用

 

注:一般情况下将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 

4.目录结构

- 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  

5.通过roles调用别人的任务(实现相互之间的调用)

准备任务

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 #同步为上海时间
注:yum安装多个包
ansible web -m yum -a 'name=wget,lrzsz' #用逗号隔开

 

 

 

posted @ 2019-03-05 13:52  ★行者尚★  阅读(257)  评论(0编辑  收藏  举报