介绍ansible自动化部署主要模块,包括command、shell、copy、fetch、file、archive、unarchive、cron、yum_repository、dnf、service、firewalld、user、group、lineinfile、replace、setup、debug等模块。
一、Command模块
ansible westos1 -m command -a “touch /mnt/westosfile” 建立文件
ansible westos1 -m command -a “rm -fr /mnt/westosfile” 删除文件
creates,removes
颜色代表信息:
二、Shell模块
常用参数:
脚本运行
vim hostname.sh
ansible westos1 -m script -a ‘./hostname.sh’ 脚本执行
三、Copy模块
主机向远程主机复制文件
ansible westos1 -m copy -a ‘src=test.yml dest=/mnt/test.yml mode=1777’ 复制本地文件到远程主机/mnt下 指定权限为1777
ansible westos1 -m copy -a ‘src=test.yml dest=/mnt/westosfile mode=1777 owner=westos backup=yes’ 如若重复则备份
****四、Fetch模块
将远程主机的文件复制到ansible主机中,但不支持目录
ansible westos1 -m fetch -a “src=/mnt/westos dest=~/.ansible” 会复制整个目录 以ip为目录名
flat=yes 只负责负责文件,但需要给定文件名称
ansible westos1 -m fetch -a ‘src=/mnt/westos dest=~/.ansible/westos flat=yes’ 只复制文件,需要给定文件名
五、File模块
常用参数:
ansible westos1 -m file -a ‘path=/mnt/westos state=touch’ 创建文件
ansible westos1 -m file -a ‘path=/mnt/westos state=directory’ 创建目录
ansible westos1 -m file -a ‘path=/mnt/westos.link state=link src=/mnt/westos’ 创建软连接
ansible westos1 -m file -a ‘path=/mnt/westos.hard state=hard src=/mnt/westos’ 创建硬链接
ansible westos1 -m file -a ‘path=/mnt/westosdir/test state=touch’
ansible westos1 -m file -a ‘path=/mnt/westos mode=777’ 修改目录权限
ansible westos1 -m file -a ‘path=/mnt/westos mode=777 recurse=yes’ 修改目录权限并且目录中子文件的权限
六、Archive与unarchive模块
archive压缩:
ansible westos1 -m archive -a ‘path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=777’
unarchive解压
ansible westos1 -m unarchive -a ‘src=/mnt/etc.tar.gz dest=/mnt copy=no’ copy=no 表示压缩文件在远程主机中,yes表示压缩文件在ansible主机中
七、hostname、Cron模块
hostname 修改主机名称
ansible westos1 -m hostname -a ‘name=westoshyl.westos.org’
cron模块 设定定时任务
常用参数:
ansible westos1 -m cron -a “job=‘echo hello westos’ name=westoscron weekday=5” 建立定时任务,每周五执行一次
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’ 查看定时任务目录
ansible westos1 -m cron -a “job=‘echo hello westos’ name=westoscron weekday=5 disabled=yes” 删除定时任务
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’ 删除定时任务的实质就是注释掉文件的相应行
删除定时任务的实质就是注释掉文件的相应行。
八、yum_repository模块
配置软件仓库文件 /etc/yum.repos.d/*.repo
常用参数:
ansible all -m yum_repository -a ‘name=“AppStream” baseurl=http://172.25.9.254/westos/AppStream description=AppStream file=westos enabled=yes gpgcheck=no’ 添加AppStream
ansible all -m yum_repository -a ‘name=“BaseOS” baseurl=http://172.25.9.254/westos/BaseOS description=BaseOS file=westos enabled=yes gpgcheck=no’ 添加BaseOS
九、dnf模块
常用参数:
ansible all -m dnf -a ‘name=httpd state=present’ present代表下载
ansible all -m dnf -a ‘name=httpd state=absent’ absent删除
十、service、firewalld模块
service常用参数:
firewalld常用参数:
ansible westos1 -m ping 查看ansible是否通畅
ansible westos1 -m dnf -a ‘name=httpd state=present’ 下载服务
ansible westos1 -m service -a ‘name=httpd state=started enabled=yes’ 开启服务
ansible westos1 -m firewalld -a ‘service=http permanent=yes state=enabled immediate=yes’ 开启火墙,immediate为立即开启
ansible westos1 -m copy -a ‘dest=/var/www/html/index.html content=“hello westos”’ 编辑默认发布页
十一、user、group 模块
user、group模块可以对远程受控用户进行用户和用户组的增删查改。
user常用参数:
group常用参数:
ansible westos1 -m user -a ‘name=westoslee state=present’ 创建用户
ansible westos1 -m user -a ‘name=westoslee group=1000 state=present’ 修改主组
ansible westos1 -m user -a ‘name=westoslee groups=1002 state=present’ 修改附加组
ansible westos1 -m user -a ‘name=westoslee groups=1001 state=present append=yes’ 添加附加组
ansible westos1 -m user -a ‘name=westoslee shell=/sbin/nologin’ 指定shell
ansible westos1 -m user -a ‘name=westoslee uid=1111’ 修改uid
ansible westos1 -m user -a ‘name=westoslee comment=“westoslee user”’ 修改注释
ansible westos1 -m user -a ‘name=westoslee state=absent remove=yes’ 删除用户并且删除家目录
ansible westos1 -m user -a ‘name=westoslee state=present’
openssl passwd -6 生成密钥
ansible westos1 -m user -a ‘name=westoslee password=“密钥”’ 修改密码
ansible westos1 -m user -a ‘name=westoslee generate_ssh_key=yes’ 用户生成ssh密钥在~/.ssh/
ansible westos1 -m group -a ‘name=test state=present’ 创建组,也可指定gid=8888
ansible westos1 -m group -a ‘name=test state=absent’ 删除组
十二、lineinfile 模块
常用参数:
ansible westos1 -m lineinfile -a ‘path=/mnt/test line=“westos” create=yes’ test插入westos create=yes表示如果没有test即创建
ansible westos1 -m lineinfile -a ‘path=/mnt/test line=“hello westos” regexp="^westos"’
匹配以westos开头的,并且修改添加hello,默认匹配不到也添加
ansible westos1 -m lineinfile -a ‘path=/mnt/test line=“hello test” regexp="^westos" backrefs=yes’ 匹配westos开头的行 添加hello test backrefs=yes匹配不到不添加
ansible westos1 -m lineinfile -a ‘path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=no’ \1代表regexp中的第一个字符串 如果backrefs=no则\1不生效
ansible westos1 -m lineinfile -a ‘path=/mnt/westos line=“end” insertafter=BOF’ 结尾插入
ansible westos1 -m lineinfile -a ‘path=/mnt/westos line=“start” insertafter=BOF’ 开头插入
nsible westos1 -m lineinfile -a ‘path=/mnt/westos line=“startaa” insertafter=BOF backup=yes’ 备份
按列排放: vim test.yml
ansible-playbook test.yml 执行剧本
十三、replace 模块
replace模块:字符替换
常用参数:
ansible westos1 -m replace -a “path=/mnt/westos regexp=‘westos’ replace=‘WESTOS’”
十四、setup 模块
setup 模块 系统服务查看,用于手机远程主机的基本信息
ansible westos1 -m setup -a ‘filter=“ansible_all_ipv4_addresses”’ 查看ip
剧本式查看: vim test.yml
ansible-playbook test.yml
十五、debug模块
debug 模块 相当于echo
常用参数:
ansible westos1 -m debug -a ‘msg=“hello westos”’