ansible_流程控制和roles

ansible_third

知识回顾

模块
yum 安装 yum模块

  • name @ 安装包组

pip 安装python相关模块

  • name
  • requirements 用来指定文件安装

service 启动服务

  • name 服务名称
  • state
    • started
    • stopped
    • restarted
    • reloaded
  • enabled 设置开机自启动

cron

  • minute
  • hour
  • day
  • mouth
  • weekday
  • job state
  • disable = yes

不能都是* 那也会导致每分钟都执行相同的命令 会撑爆服务器

user

  • group

  • groups

  • state

  • name

  • home

  • shell

  • uid

  • password

  • remove = yes

group 创建组

  • name
  • gid
  • system

user 用户

  • 超级用户 0
  • 普通用户
    • 系统用户 centos7 1-999 centos6 1-499
    • 系统用户 centos7 1000-65535 centos 500-65535

AD-hoc 这些模块是这样称呼的

yaml

  • 字典 key: value 冒号后面必须有空格
  • 列表 - []
  • 后缀名 yaml yml

传参 动态的

  • -e
  • host文件[groupname: vars]
  • playbook register 注册
  • playbook vars
  • hosts 文件里面 主机地址后面写
  • -e > playbook > hosts

yaml

  • 冒号后面必须有空格
  • 等号后面不能有空格
  • -后面也要有空格
  • 严格对齐

new_content

setup

ansible all -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_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
# 系统cpu的颗数

ansible_processor_vcpus
# 系统cpu的总个数= core * count 核数乘颗数

ansible_python 
# 系统上的python


ansible cache -m setup -a 'filter=*processor*'
# 用来搜索filter后面的字段

*匹配数量,表示(0,+%)

? 匹配数量,便是 0 | 1 二者之一

.除了换行符以外的所有字符

+至少一次(1,+%)

[12365] 匹配内容,存在即可 or 关系

() 分组

{m,}次数,出现m次

{m,}至少m次

{m,n}出现m-n次

a*.b 可以匹配db

a.*匹配不了db

条件判断

  • 不同的系统
  • 不同的版本
  • 不同的环境
  • 不同的用户
- hosts: db
  remote_user: root
  tasks:
  - name: createfile
  copy: content='风萧萧兮易水寒' dest=/tmp/a.txt
  when: a=='3'
  - name: createfile
  copy: content='壮士一去兮不复还' dest=/tmp/a.txt
  when: a=='4'
  
  

Tips: Ubuntu安装包是apt-get

tags

- hosts: db
  tasks:
  - name: wadong
    tieqiao: wadong
  - name: tk
    dong: tk
    tags: tk
- hosts: web
  tasks: 
  - name: install_nginx
  	yum: name=nginx
  - name: copyfile
  	copy: src=/etc/nginx/nignx.conf dest=/etc/nginx/nginx.conf
  	tags: copyfile
  - name: start
  	service: mame=nginx state=started
  
  
ansible-playbook -t copyfile p1.py
# 只执行标签的动作 其余的不执行

循环 with_item

一次性创建多个

- hosts: gb
  tasks:
  - name: gbqc
    dong: {{ item }}
    with_items:
    - qbqc
    - cyf
    - ssfj
- hosts: web
  task:
  - name: createuser
  	user: name={{item}}
  	with_items:
  	- max20
  	- max21
  	- max22
- host: web
  tasks:
  - name: createuser
  	user: name={{ item }}
  	with_items:
  	- eral11
  	- eral12
  	- eral13

循环嵌套

- hosts: web
  tasks:
  - name: crategroup
    group: name={{ item }}
    with_items:
    - max30
    - max31
    - max32
   - name: createuser
   	 user: name={{ item.name}} group={{ item.group }}
   	 with_items:
   	 - {'name':eral11,'group':sex11}
   	 - {'name':eral12,'group':sex12}
   	 - {'name':eral13,'group':sex13}

template

jinja2

- 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
  
  
 配置文件:bind {{ansible_default_ipv4.address }}

copy和template的区别

  • copy模块不替代参数
  • template 模块替代参数
- 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
  	
 

tips: 可以写相对路径,在当下目录新建一个templates目录,

然后把文件放在templates目录中

handlers

修改配置文件

- hosts: web
  tasks:
  - name: installredis
  	yum: name=redis
  - name: copyfile
  	template: src=redis.conf dest=/etc/redis.conf
  	tags:copyfile
  	nofity:restart
  - name:start
  	service: name=redis state=restarted
  handlers:
  - name: restart
  	service: name=redis state=restarted
  	

roles

  • 目录清晰
  • 可以相互调用

roles 文件夹

文件夹里面是要创建的每一个角色,每一个文件夹

每一个角色里面都要有tasks(must),templates,files,handlers,

vars目录

每个目录都要有main.yml文件,通过import_tasks来调用

其中templates文件夹中的文件可以通过相对路径来调用

其中files文件夹是否可以通过相对路径来调用

hodoop 大数据

setenforce 0
# 用来临时关闭selinux
iptables -F
# 临时关闭防火墙
# 这个需要yum install iptables
/etc/seliunx/config 
# 永久关闭
posted @ 2019-02-21 20:38  玩蛇少年7372  阅读(237)  评论(0编辑  收藏  举报