Command 模块

**功能:**远程执行主机命令,此为默认模块,可忽略 -m 选项

**注意:**此模块不会支持特殊字符,如果有需要使用特殊字符,例如 & | ; 等,需要使用 shell 模块来实现

命令参数

  • creates
    • 如果目标文件已经存在则不会执行动作
  • chdir
    • 切换执行动作的目录
  • removes
    • 如果目标文件存在则会执行动作

范例

bash
1. 默认访问为家目录,而非被控制端所在目录
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m command -a "ls"
100.0.0.10 | CHANGED | rc=0 >>
anaconda-ks.cfg
mysql.sh

2. removes 与 creates 参数的区别
[root@centos7 ~]# touch 1.txt
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m command -a "removes=/root/1.txt chdir=/root/ echo hei"
100.0.0.10 | CHANGED | rc=0 >>
hei
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m command -a "removes=/root/2.txt chdir=/root/ echo hei"
100.0.0.10 | SUCCESS | rc=0 >>
skipped, since /root/2.txt does not exist
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m command -a "creates=/root/1.txt chdir=/root/ echo hei"
100.0.0.10 | SUCCESS | rc=0 >>
skipped, since /root/1.txt exists
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m command -a "creates=/root/2.txt chdir=/root/ echo hei"
100.0.0.10 | CHANGED | rc=0 >>
hei

Shell 模块

**功能:**执行 shell 命令的模块。可以弥补 script 模块的缺陷(不能运行远程主机上的脚本)

**注意:**有些命令执行时会有问题,例如 mysql 的临时密码,执行时会识别错误。

命令参数

  • chdir
    • 修改执行的目录
  • creates,removes
    • 参考模块 command 中的 creates 参数作用
  • args
    • 作为执行命令之前做的动作
  • cmd
    • 跟上要执行的命令

范例

bash
1. 如果 1.txt 文件已经存在,不会创建 2.txt
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m shell -a "touch 2.txt creates=1.txt chdir=/root"
100.0.0.10 | SUCCESS | rc=0 >>
skipped, since 1.txt exists

2. 如果文件已经存在,则创建 2.txt 并关闭 warnning 提示
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m shell -a "touch 2.txt removes=1.txt chdir=/root warn=no"
100.0.0.10 | CHANGED | rc=0 >>

3. 执行远程主机上的脚本
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m shell -a "/bin/bash /root/touch.sh"

剧本执行案例

bash
1. 如果 2.txt 文件已经存在则不会创建 2.txt
[root@DESKTOP-NHT2EP5 ~]#cat touch.yaml
---
- hosts: 100.0.0.10
  remote_user: root
  gather_facts: no
  tasks:
    - name: exec remote scripts
      shell: touch 2.txt    ---命令主体
      args:        ---属性信息
        chdir: /root
        creates: 2.txt
        warn: no
[root@DESKTOP-NHT2EP5 ~]#ansible-playbook -C touch.yaml
PLAY [100.0.0.10]
TASK [exec remote scripts] 
`changed:` [100.0.0.10]

2. 如果 2.txt 文件已经存在那么也会创建 2.txt
[root@DESKTOP-NHT2EP5 ~]#cat touch.yaml
---
- hosts: 100.0.0.10
  remote_user: root
  gather_facts: no
  tasks:
    - name: exec remote scripts
      shell: touch 2.txt
      args:
        chdir: /root
        removes: 2.txt
        warn: no
[root@DESKTOP-NHT2EP5 ~]#ansible-playbook -C touch.yaml
PLAY [100.0.0.10] 
TASK [exec remote scripts]
`ok:` [100.0.0.10]

Script 模块

**功能:**执行 Ansible 端的脚本,执行到被控制端

**注意:**不可以执行远程主机的脚本,只可以执行本地的脚本到远程主机(被控制端)

命令参数

  • args
    • 定义属性
  • creates,removes
    • 与 Command 中的 creates 和 removes 相同

范例

bash
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m script -a "/root/script.sh"

1. 如果 1.txt 存在则不执行脚本
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m script -a "/root/script.sh chdir=/root/ creates=1.txt"
100.0.0.10 | SKIPPED

2. 如果 1.txt 存在则执行脚本
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.10 -m script -a "/root/script.sh chdir=/root/ removes=1.txt"
100.0.0.10 | CHANGED => 

3. 使用 | 管道符或 >< 都可以
[root@DESKTOP-NHT2EP5 ~]#ansible 100.0.0.105 -m shell -a 'ls /root| grep sh.sh'
100.0.0.105 | CHANGED | rc=0 >>
sh.sh

剧本执行案例

- name: exec shell script
  script: /root/script.sh
  args:
    chdir: /root
    removes: 1.txt

- name: exec shell script
  script: /root/script.sh
  args:
    chdir: /root
    creates: 1.txt
    warn: no

Copy 模块

**功能:**复制 ansible 本地文件到远程主机中

**注意:**只可以将 ansible 本地主机的文件复制到远程主机,如果要复制远程主机文件到 ansible 本地那么就需要使用 fetch 模块

命令参数

  • backup
    • 如果目标文件以及存在那么会对该文件进行备份,然后再进行覆盖操作
  • group、mode、owner
    • 定义复制后的属主属组以及权限
  • content
    • 直接在命令行中定义内容,但是定义时要指定换行符或其他符号
  • follow
    • no 将复制过去的链接文件变成一个单独的文件
    • yes 将复制过去的文件保留链接指向的文件链接属性
  • directory_mdoe
    • 当对⽬录做递归拷贝时,设置了 directory_mode 将会使得只拷贝新建⽂件旧⽂件不会被拷贝。默认未设置

范例

bash
#如果目标文件已经存在则会先备份再覆盖
ansible test -m copy -a "src=/home/jasonchen/1.txt dest=/root/host.txt backup=yes"
---执行结果---
[root@DESKTOP-NHT2EP5 ~]#ansible test -m shell -a "ls /root"
100.0.0.10 | CHANGED | rc=0 >>
1.txt
1.txt.22119.2020-09-12@14:32:56~


#如目标存在,默认覆盖,此处指定先备份
ansible websrvs -m copy -a "src=/root/test1.sh dest=/tmp/test2.sh  owner=chen mode=600 backup=yes"

#指定内容,直接生成目标文件  
ansible websrvs -m copy -a "content='test line1\ntest line2' dest=/tmp/test.txt"

#复制/etc目录自身,注意/etc/后面没有/
ansible websrvs -m copy -a "src=/etc dest=/backup"

#复制/etc/下的文件,不包括/etc/目录自身,注意/etc/后面有/
ansible websrvs -m copy -a "src=/etc/ dest=/backup"

#############follow 参数#############
1. 当 follow 为 no 的时候,将不会保留链接,直接将该文件复制成为一个新的文件,且不会影响目标文件的内容
---执行环境---
-rw-r--r-- 1 jasonchen jasonchen    10 Oct 18 09:04 1.txt
lrwxrwxrwx 1 jasonchen jasonchen     5 Oct 18 08:54 2.txt -> 1.txt
---执行命令---
jasonchen@DESKTOP-NHT2EP5:~$ sudo ansible test -m copy -a "src=/home/jasonchen/2.txt dest=/root/ follow=no"
---执行结果---
[root@centos7 ~]# ll
total 8
-rw-r--r--. 1 root root 11 Sep 12 14:54 1.txt
-rw-r--r--. 1 root root 10 Sep 12 14:54 2.txt

2. 当 follow 为 yes 的时候,复制过去时将会保留权限,且会替换目标文件链接和软连接的内容
---执行命令---
jasonchen@DESKTOP-NHT2EP5:~$ sudo ansible test -m copy -a "src=/home/jasonchen/2.txt dest=/root/ follow=yes"
---执行结果---
[root@centos7 ~]# ls -l
total 4
-rw-r--r--. 1 root root 10 Sep 12 15:00 1.txt
lrwxrwxrwx. 1 root root  5 Sep 12 15:00 2.txt -> 1.txt

3. 当与 backup 参数结合时,那么目标要替换的软链接会自动生成一个单独的文件

剧本案例

yaml
jasonchen@DESKTOP-NHT2EP5:~$ sudo ansible-playbook copy.yaml -vv
jasonchen@DESKTOP-NHT2EP5:~$ cat copy.yaml
---
- hosts: test
  remote_user: root
  gather_facts: no
  tasks:
    - name: copy file link
      copy:
        src: /home/jasonchen/2.txt
        dest: /root/2.txt
        owner: root
        group: root
        mode: 655
        follow: yes        ---改为 no 则是不保留链接指向的地址属性

Fetch 模块

**功能:**从远程主机复制文件到 ansible 本地

命令参数

  • flat
    • 如果目标路径上的目录已经存在,并且后面没有 / 那么就不会创建,如果 flat 为 yes 并且目录后面有 / 那么就会在目录下创建一个同名的文件
    • 如果目标路径上的目录后面有 / 并且 flat 为 no 那么就会在目录下创建一个与对方 IP 相同的目录文件

范例

bash
ansible websrvs -m fetch -a 'src=/root/test.sh dest=/data/scripts'
[root@ansible ~]#ansible  all -m fetch -a 'src=/etc/redhat-release dest=/data/os'
[root@ansible ~]#tree /data/os/
/data/os/
├── 10.0.0.6
│  └── etc
│    └── redhat-release
├── 10.0.0.7
│  └── etc
│    └── redhat-release
└── 10.0.0.8
 └── etc
   └── redhat-release

剧本案例

bash
---
- hosts: test
  gather_facts: no
  tasks:
    - name: copy remote host file
      fetch:
        src: /root/dir/dir
        dest: /home/jasonchen/dir/
        flat: no        ---是否为 yes 决定了 dest 文件的存放文件名

File 模块

**功能:**设置文件属性,创建软连接等

命令参数

  • state
    • 决定创建的文件类型
    • absent 删除文件或目录
    • touch 创建文件
    • directory 创建目录
    • hard 创建硬链接
    • link 创建软连接
  • force
    • 强制创建
    • 软连接的源文件不存在但是稍后会出现
  • group owner mode
    • 设置权限
  • recurse
    • 递归设置权限等

范例

bash
#创建空文件
ansible all -m file  -a 'path=/data/test.txt state=touch'
ansible all -m file  -a 'path=/data/test.txt state=absent'
ansible all -m file -a "path=/root/test.sh owner=wang mode=755"
#创建目录
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"
#创建软链接
ansible all -m file -a 'src=/data/testfile path|dest|name=/data/testfile-link state=link'
#创建目录
ansible all -m file  -a 'path=/data/testdir state=directory'
#递归修改目录属性
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql recurse=yes"

剧本案例

bash
jasonchen@DESKTOP-NHT2EP5:~$ cat file.yaml
---
- hosts: test
  remote_user: root
  gather_facts: no
  tasks:
    - name: create directory
      file:
        state: directory
        path: /root/test/
    - name: create file
      file:
        state: touch
        path: /root/test/1.txt
    - name: create file link
      file:
        state: link
        src: /root/test/1.txt
        dest: /root/test/2.txt
    - name: flie mode
      file:
        state: directory
        path: /root/test/
        recurse: yes
        mode: 700
        owner: chen
        group: chen

Unarchive 模块

**作用:**解压压缩包

实现有两种用法:

  1. 将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes
  2. 将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no

范例

bash
ansible all -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo owner=wang group=bin'
ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777'
ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'
ansible websrvs -m unarchive -a
'src=https://releases.ansible.com/ansible/ansible-2.1.6.0-0.1.rc1.tar.gz dest=/data/  owner=mysql remote_src=yes'

Archive 模块

**作用:**打包本地的数据保存到被控制端

案例

bash
ansible websrvs -m archive  -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2 owner=wang mode=0600'

Hostname 模块

**作用:**管理主机名

案例

bash
ansible node1 -m hostname -a "name=websrv"
ansible 10.0.0.18 -m hostname -a 'name=node18.magedu.com'

Cron 模块

**作用:**计划任务

**支持时间:**minute,hour,day,month,weekday

范例

bash
#备份数据库脚本
[root@centos8 ~]#cat /root/mysql_backup.sh
#!/bin/bash
mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip >
/data/mysql_`date +%F_%T`.sql.gz
#创建任务
ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql"
job=/root/mysql_backup.sh'
ansible websrvs  -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com
&>/dev/null' name=Synctime"
#禁用计划任务
ansible websrvs  -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
&>/dev/null' name=Synctime disabled=yes"
#启用计划任务
ansible websrvs  -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
&>/dev/null' name=Synctime disabled=no"
#删除任务
ansible websrvs -m cron -a "name='backup mysql' state=absent"
ansible websrvs -m cron -a 'state=absent name=Synctime'

yum 和 apt 模块

功能:

yum 管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本 apt 模块管理 Debian 相关版本的软件包

范例

yum 例子

bash
ansible websrvs -m yum -a 'name=httpd state=present'  #安装
ansible websrvs -m yum -a 'name=httpd state=absent'  #删除

[root@ansible ~]#ansible websrvs -m yum -a 'name=sl,cowsay'

apt 例子

bash
[root@centos8 ~]#ansible 10.0.0.100 -m apt -a
'name=bb,sl,cowsay,cmatrix,oneko,hollywood,boxes,libaa-bin,x11-apps'
[root@centos8 ~]#ansible websrvs -m apt -a 'name=rsync,psmisc state=absent'

Service 模块

**作用:**管理服务

范例

bash
ansible all -m service -a 'name=httpd state=started enabled=yes'
ansible all -m service -a 'name=httpd state=stopped'
ansible all -m service -a 'name=httpd state=reloaded'
ansible all -m shell -a "sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf"
ansible all -m service -a 'name=httpd state=restarted'

User 模块

**作用:**管理用户

命令参数

  • comment
    • 用户的备注信息
  • uid
    • 设置用户 uid
  • home
    • 指定用户家目录
  • system
    • 是否创建系统用户
  • shell
    • 指定登录的 shell
  • group
    • 指定属组
  • gorups
    • 指定附加组
  • remove
    • 删除家目录等数据
  • non_unique
    • 不要用户 uid 的唯一性

范例

bash
#创建用户
ansible all -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root'
ansible all -m user -a 'name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes'
#remove=yes表示删除用户及家目录等数据,默认remove=no
ansible all -m user -a 'name=nginx state=absent remove=yes'

Group 模块

**作用:**管理用户组

范例

bash
#创建组
ansible websrvs -m group  -a 'name=nginx gid=88 system=yes'
#删除组
ansible websrvs -m group  -a 'name=nginx state=absent'

Lineinfile 模块

ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时, 存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可 以方便的进行替换 一般在ansible当中去修改某个文件的单行进行替换的时候需要使用lineinfile模块 regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最 后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被 删除。 如果想进行多行匹配进行替换需要使用replace模块 功能:相当于sed,可以修改文件内容

范例

bash
ansible websrvs -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 80'"
ansible all -m  lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'"
ansible all -m lineinfile  -a 'dest=/etc/fstab state=absent regexp="^#"'

Replace 模块

**作用:**进行多行匹配

范例

bash
ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'" 
ansible all -m replace -a "path=/etc/fstab regexp='^#(UUID.*)' replace='\1'"

Setup 模块

**作用:**setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机 较多,会影响执行速度,可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息

范例

bash
ansible all -m 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*"