Ansible的常用模块
ansible的执行
- ad-hoc
- playbook
ansible ad-hoc
ad-hoc:临时的命令,执行后结束,不会保存
使用场景
比如在多台机器上查看某个进程是否启动,或拷贝指定文件到本地,等等
ad-hoc的命令模式(语法)
语法:
ansible 主机名(主机清单中的主机名或者主机组) -m 模块名 [-a 动作]
ac-hoc返回结果的颜色
- 绿色:被管理端执行成功,并且结果不会发生改变
- 黄色:被管理端执行成功,但是结果是变化的
- 红色:执行失败,注意看报错
ansible帮助手册
ansible-doc 模块名
ansible模块:command
ansible web_group -m command -a '命令(不带特殊符号)'
ansible模块:shell
ansible web_group -m shell -a '命令'
ansible模块:script
#ansible远程执行脚本
ansible web_group -m script -a '/root/a.sh'
ansible软件管理模块:yum
yum
- name:
- 直接指定包名:从仓库安装 yum install httpd
- http:// 从指定URL安装 yum install http://mirrors.aliyun.com/epel/http-
1.1.rpm
- file:// 从本地安装 类似 yum localinstall /root/http-1.1.rpm
- state
- absent:卸载
- present:安装
- lastest:
- download_only:
- true:只下载不安装
- false:下载并安装
-list
- 包名
"yumstate": "available" 证明包可以安装但是没装
"yumstate": "installed" 已经安装了
exclude=kernel*,foo* #排除某些包
list=ansible #类似于yum list查看是否可以安装
disablerepo="epel,ol7_latest" #禁用指定的yum仓库
ansible软件管理模块:yum_repository
##创建新的仓库配置文件
ansible db01 -m yum_repository -a 'name=local description=xxx baseurl=file:///mnt
gpgcheck=no enabled=yes'
##创建新的仓库配置,并且文件名和仓库名不同
ansible web01 -m yum_repository -a 'name=local file=zls_local description=xxx
baseurl=file:///mnt gpgcheck=no enabled=yes'
##追加仓库
ansible lb01 -m yum_repository -a 'name=test2_ansible file=test_ansible
description=xxx baseurl=http://www.baidu.com'
##删除仓库
ansible lb01 -m yum_repository -a 'name=test2_ansible file=test_ansible state=absent'
yum_repository
- name:仓库名字(如果有file,只是仓库名,如果没有file,文件名和仓库名)
- file:指定仓库的文件名
- -description:仓库的描述(name)
- baseurl:仓库的url
- -gpgcheck:
- no:不开启 0
- yes:开启 1(默认)
- enabled:
- no:不开启 0
- yes:开启 1 (默认)
ansible文件管理模块:copy
##远程推送文件
ansible nginx -m copy -a 'src=/etc/passwd dest=/root'
copy
- src:指定源文件的路径
- dest:指定目标路径
- owner:指定属主
- group:指定属组
- mode:指定权限
- backup:
- yes:如果目标路径,存在同名文件,就将目标文件备份
- no:不备份直接覆盖(默认)
- content:将指定文本内容覆盖到目标文件中
- remote_src:将命令变成cp
- yes:将源文件,编程远端的源文件(ansible被管理端)
- no:源文件还是本地文件(ansible管理端 默认)
- follow:拷贝软连接
- yes:会将软连接一起拷贝
- no:会生成一个新的软连接文件