ansible常用模块介绍及简单案例(笔记)

模块用法查看:

ansible-doc module_name 详细用法
ansible-doc -s module_name 简单用法

常用模块

ping:

测试模块,not icmp ping,测试ansible之间的ssh通信

command:

默认模块

chdir、argv、creates、remove

ansible all -a ‘chdir=/tmp pwd’

该模块中不支持 重定向、管道符、等特殊符号,所以该模块不常用。

shell:

代替command,支持各种符号没有幂等性

module_name = shell 配置文件中把shell改为默认模块,而不是command

ansible all -m shell -a 'cd /tmp;pwd'

script:

本机脚本在远程被管理主机上执行

ansible-doc -s script

ansible all -m script -a 'test.sh' 本机的脚本,会将脚本拷贝到远程主机上的 /root/.ansible/tmp/ 下,执行完成后自动删除,如果不是执行脚本,而是调用模块,则在该目录下则会有相应模块的py代码,模块调用完,也会被删除,如果不是正常退出,则文件会残留

copy:

复制本机文件到远程主机,可设置 src dest 所有者 权限等设置

拷贝文件夹会自动递归,/ 只拷贝内容,不带 / 内容+文件夹本身

ansible all -m copy -a 'src=/etc/issue dest=/tmp/issue.txt owner=li mode=600 backup=yes'

拷贝文件到目标机器,设置权限等,如果存在该文件,但是文件内容不同,先备份该文件,再进行覆盖。

即 目标主机已有issue.txt ,但是内容不同,那么会先备份原issue.txt,再用 /etc/issue 覆盖 issue.txt,如果issue.txt 和 /etc/issue内容相同,那么由于幂等性,则不会进行复制。

ansible all -m copy -a 'content="haha\nha" dest=/tmp/test.txt' 直接生成内容

ansible all -m copy -a 'src=/etc dest=/backup' 内容+目录

ansible all -m copy -a 'src=/etc/dest=/backup' 仅内容

fetch:

复制远程主机文件到本机

src是远程文件,且只能是文件不能是文件夹,且不支持通配符写法,可以打包解决,dest是本机

ansible all -m fetch -a 'src=/etc/issue dest=/etc/os' 把远程文件复制到本机os下,且以每个IP为一个目录分别存放

file:

创建、删除、文件 修改属性、软连接

ansible all -m file -a 'path=/data/test.txt state=touch' 创建

state=touch 创建文件 state=absent 删除文件 state=directory 创建文件夹

state=link 创建软连接 owner=li 所属者 mode=755 权限

'src=/data/testfile path|dest|name=/data/testfile-link state=link'

创建软连接,连接名 path dest name 效果相同

unarchive:

解包,且可以设置所有者所属组

1、将本机压缩包解压到远程主机 copy=yes,需要拷贝到远程,默认值

2、将远程主机压缩包解压 copy=no,远程机器已有不需要拷贝

ansible all -m unarchive -a 'src=xxx.gz dest=/usr/local owner=mysql group=mysql mode=xxx'

archive:

打包,指定格式 所有者 所属组等

ansible all -m archive -a 'path=/var/log dest=/data/log.tar.gz format=bz2 owner mode'

打包远程主机文件

hostname:

修改主机名

ansible all -m hostname -a 'name=www.xxx.com'

不利于批量执行

cron:

在远程机器创建计划任务

分时日月周

创建:

'hour=2 minute=20 weekday=1-5 name="backup mysql job=脚本"'

该脚本是远程主机上的脚本

禁用/启用:

disabled=yes disable=no

删除:

name=backup state=absent

yum/apt:

在目标主机安装软件

ansible all -m yum -a 'name=httpd,lrzsz state=present'

state=present 安装 state=absent 删除

service:

远程启动服务

ansible all -m service -a 'name=httpd state=started enabled=yes'

启动服务,且开机启动

user:

创建用户模块

-a 'name=nginx comment=注释 uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique'

设置 主组 从属组 shell类型 系统账号 是否创建默认家目录 家目录位置 是否唯一,要先创建组nginx

group:

ansible all -m group -a 'name=nginx gid=88 system=yes'

lineinfile:

单行修改远程主机文件

'path=/etc/httpd/conf/httpd.conf regexp="^Listen" line="Listen 8080"'

'path=/etc/selinux/config regrxp="^SELINUX=" line="SELINUX=disabled"'

'dest=/etc/fstab state=absent regexp="^#"'

replace:

多行修改文件

"path=/etc/fstab regexp='^(UUID.*)' replace='#\1' " 加注释

setup:

收集远程主机信息,jason格式
常用变量:
通常可在模板中引用setup模块的变量
ansible all -m setup -a "filter=ansible_nodename"
ansible all -m setup -a "filter=ansible_hostname"
ansible all -m setup -a "filter=ansible_domain"
ansible all -m setup -a "filter=ansible_memtotal_mb"
ansible all -m setup -a "filter=ansible_memory_mb"
ansible all -m setup -a "filter=ansible_memfree_mb"
ansible all -m setup -a "filter=ansible_os_family"
ansible all -m setup -a "filter=ansible_distribution_major_version"
ansible all -m setup -a "filter=ansible_distribution_version"
ansible all -m setup -a "filter=ansible_processor_vcpus"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
ansible all -m setup -a "filter=ansible_architecture"
ansible all -m setup -a "filter=ansible_processor*"

简单案例

创建删除mysql用户及组

创建

---
- hosts: all
  remote_user: root
  gather_facts: no
                                                                                            
  tasks:
  - name: create mysql group
    group: name=mysql system=yes gid=306
  - name: create mysql user
    user: name=mysql uid=306 shell=/sbin/nologin system=yes group=mysql create_home=no home=/data/mysql

删除

---
- hosts: all
  remote_user: root
  gather_facts: no

  tasks:
  - name: remove mysql user                                               
    user: name=mysql state=absent
  - name: remove mysql group
    group: name=mysql state=absent

安装及卸载nginx

安装

---
- hosts: all
  remote_user: root
  gather_facts: no

  tasks:
  - name: create group nginx
    group: name=nginx state=present
  - name: create user nginx
    user: name=nginx state=present group=nginx
  - name: install nginx
    yum: name=nginx state=present
  - name: web page
    copy: content='<h1>welcom to Nginx</h1>' dest=/usr/share/nginx/html/index.html            
  - name: start service
    service: name=nginx state=started

卸载

---
- hosts: all
  remote_user: root
  gather_facts: no

  tasks:
  - name: stop service
    service: name=nginx state=stopped
  - name: remove user nginx
    user: name=nginx state=absent
  - name: remove group nginx
    group: name=nginx state=absent
  - name: uninstall nginx
    yum: name=nginx state=absent
  - name: remove web page
    file: path=/usr/share/nginx/ state=absent
posted @ 2021-02-18 17:14  windman  阅读(157)  评论(0编辑  收藏  举报