ansible学习记录

ansible学习记录,第1张

ansible学习记录
  • ansible内容记录
    • 下载
    • 查看版本
    • 描述
    • 语法
    • playbook文件结构
    • 子分组
    • 常用模块
    • YAML语言

ansible内容记录 下载
mkdir  mypackage
yum install https://dl.fedoraproject.org/pub/epel-release-latest-7.noarch.rpm

yumdownloader --resolve --destdir /mypackage/  ansible   下载rpm包到指定位置
-------------------------------------------------------------------------
ansible仓库默认不在yum仓库中,需要启用epel仓库

yum install epel-release -y 

yum install ansible 
查看版本
ansible -version
描述
host inventory  用来管理设备列表   
	role
	
playbook    ansible的剧本,可以想象为将多个任务放置在一起,一块执行
	role
	
core modules   核心模块

custom modules 自定义模块

connection plugins   连接插件,用于与被控主机之间基于SSH建立连接关系

plugins 其他插件,包括记录日志等
[root@localhost ~]# grep -v ^# /etc/ansible/hosts |grep -v ^$
[web-servers]
192.168.5.134 ansible_ssh_port=22  ansible_ssh_user=root ansible_ssh_pass=123456

[webserver:var]  组:变量
ansible_ssh_port=22  
ansible_ssh_user=root
语法
ansible [-i 主机文件] [-f 批次] [组名] [-m 模块名称] [-a 模块参数]

-i 指定host文件路径,默认在/etc/ansible/hosts
playbook文件结构
ansible ip -m ping
 
简洁输出   ansible ip -m ping -o    (一行输出)

ansible ip -m ping -o -u root -k   (密码交互)
子分组
[apache]
host[1:2]

[nginx]
host[3:4]

[webserver:children]
apache
nginx

[webserver:var]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_ssh_port=22222

调用
ansible webserver -m ping -o
常用模块
### Ad-Hoc-点对点模式
	复制模块
	--
	常用参数解释
	src  指定拷贝文件按的源地址
	dest 指定拷贝文件的目的地址
	backup 拷贝文件前,若原始文件发生改变,则对目标文件进行备份
	woner 指定新拷贝文件的所有者
	group 指定新拷贝文件的所有组
	mode  指定新拷贝文件的权限
	
	copy  
	例子	ansible webserver -m copy -a 'src=/workspace/nginx.tar.gz dest=/tmp/ owner=root group=bin mode=700 backup=yes'
用户模块
--
user
例子 创建用户	ansible webserver -m user -a 'name=sunc state=present(创建)'
加密
echo "123456" | openssl passwd -1 stdin
修改密码	   ansible webserver -m user -a 'name=sunc password="sdfdgfhjmjmhgbmkdflvmdklv"'
修改shell   ansible webserver -m user -a 'name=sunc shell=/sbin/nologin append=yes'
删除用户	   ansible webserver -m user -a 'name=sunc state=absent'
软件包管理模块
--
yum 
例子  安装apache   ansible webserver -m yum -a 'name=httpd state=latest'
文件模块
--
path  定义文件或目录的路径
	
file
例子 
创建文件    ansible webserver -m file -a 'path=/tmp/8.txt mode=777 state=touch'
创建文件    ansible webserver -m file -a 'path=/tmp/8.txt mode=777 state=file'
删除文件    ansible webserver -m file -a 'path=/tmp/8.txt mode=777 state=abset'
创建文件夹   ansible webserver -m file -a 'path=/tmp/workspace mode=755 state=directory'
收集模块
--
setup
例子   ansible ip -m setup -a 'filter=ansible_all_ipv4-addresses'     filter过滤
shell模块
--
shell
例子     
ansible webserver -m shell -a 'hostname' -o
ansible webserver -m shell -a 'yum install nginx -y' -o
ansible webserver -m shell -a 'yum remove nginx -y' -o
script模块
--
例子    ansible webserver -i hosts -m script -a "/root/a.sh"
cron模块
--
注意:使用ansible创建的计划任务,不能使用本地 crontab -e 去编辑,否则ansible 无法再次 *** 作此计划任务了
常用参数:
name  指定cron job的名字
minute  分钟  |  hour 小时  | day  天 |  month 月份 | weekday 星期 | job  指定要执行的内容 | state 状态(present新增   absent删除)
template模块
--
使用了jinjia2格式作为文件模板,可以进行文档内变量的替换,文件以.j2结尾
常用参数:
src 指定ansible控制端的 文件路径
dest   指定ansible被控端的 文件路径
owner  指定文件属主
group  指定文件属组
mode   指定文件权限
例子   
cat  hello_world.j2   ///   Hello {{var}} !
ansible all -i hosts -m template -a 'src=hello_world.j2  dest=/tmp/hell_world.word' -e 'var=world'
YAML语言
---
- name: 分发nginx安装包
  copy:
    src: 'nginx-1.20.1.tar.gz'
    dest: '{{ source_dir }}'
- name: XXXXXX
.......
目录结构
├── group_vars  		//全局变量
    │   └── all
    ├── hosts				//硬件清单
    ├── roles				//产品目录
    │   ├── docker			//docker
    │       ├── defaults	//产品私有变量
    │       ├── files		//要拷贝的文件
    │       ├── tasks		//产品部署过程
    │       └── templates	//要修改的配置      
    └── site.yml			//主控脚本	

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/716667.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存