衔接上篇博文,这一节我们将学习file、copy、unarchive、lineinfile、script等模块的使用。还是和之前章节中提到了,所有模块的用法都可以使用ansible-doc命令查看。
1、file模块
通常使用file模块对被管理主机上的文件进行操作(包括创建和删除),其工作原理和touch命令类似。同时还支持在创建文件时指定文件的属主属组及文件权限。更详细的参数可通过命令帮助查看。我们可以通过几个例子很直观的进行了解。
参数 | 选项/默认值 | 含义 |
path/dest | 目标主机的路径信息 | |
group | 指定属组 | |
owner | 指定属主 | |
mode | 指定权限 | |
state | absent | 删除 |
state | touch | 创建文件(不存在即创建新的空文件) |
state | directory | 创建目录(目录不存在即递归创建) |
state | hard/link | 设置文件链接 |
state | file | 判断指定文件是否存在 |
backup | no | 是否备份原文件,默认不备份 |
- 在目标主机/opt/目录下创建一个user1的文件,属主为user1,属组为root,权限为755。
[root@k8s-master-01 ansible]# ansible client -i hosts -m file -a "path=/opt/user1 state=touch owner=user1 group=root mode=0755"
- 递归创建目录/opt/a/b,并设置属主为user1,属组为root,权限755
[root@k8s-master-01 ansible]# ansible client -i hosts -m file -a "path=/opt/a/b state=directory owner=user1 group=root mode=0755"
- 删除文件(删除/opt/a/b)
[root@k8s-master-01 ansible]# ansible client -i hosts -m file -a 'path=/opt/a/b state=absent'
- 软链接(将/etc/passwd文件软链接到/opt)
[root@k8s-master-01 ansible]# ansible client -i hosts -m file -a 'src=/etc/passwd dest=/opt/passwd state=link'
2、copy模块
主要用于将管理主机上的数据信息传送给多台主机。可以复制本地文件到远程主机,也可以复制远程主机上的文件到本地。
以下是我整理出来的常用选项
参数 | 选项/默认值 | 含义 |
backup | bool(default:no) | 覆盖前,是否备份远程主机上的源文件 |
src | 指定将Ansible管理主机的待拷贝文件 | |
remote_src | bool(default:no) | 设置为yes,表示将远程主机上的数据进行移动操作 |
dest | 目标主机上文件目录 | |
owner | 设置文件属主 | |
group | 设置文件属组 | |
mode | 设置权限 |
比如我们需要将已经配置号的hosts文件批量推送到各个受控节点:
# 推送kubernetes官方yum源至各个work节点
[root@k8s-master-01 ansible]# ansible client -i hosts -m copy -a "src=/etc/yum.repos.d/kubernetes.repo dest=/etc/yum.repos.d/kubernetes.repo"
# 推送配置好的hosts文件至各个work节点,并设置属主属组及权限
[root@k8s-master-01 ansible]# ansible client -i hosts -m copy -a "src=/etc/hosts dest=/etc/hosts owner=root group=root mode=755"
# 推送配置好的resolv.conf文件至各个work节点,并在覆盖前进行备份
[root@k8s-master-01 ansible]# ansible client -i hosts -m copy -a "src=/etc/resolv.conf dest=/etc/resolv.conf backup=yes"
3、archive/unarchive模块
archive/unarchive分别为用来对文件进行解压和压缩的模块
语法格式
# unarchive 模块
- name: Extract foo.tgz into /var/lib/foo
unarchive:
src: foo.tgz
dest: /var/lib/foo
- name: Unarchive a file that is already on the remote machine
unarchive:
src: /tmp/foo.zip
dest: /usr/local/bin
remote_src: yes
- name: Unarchive a file that needs to be downloaded (added in 2.0)
unarchive:
src: https://example.com/example.zip
dest: /usr/local/bin
remote_src: yes
- name: Unarchive a file with extra options
unarchive:
src: /tmp/foo.zip
dest: /usr/local/bin
extra_opts:
- --transform
- s/^xxx/yyy/
# archive 模块
- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
archive:
path: /path/to/foo
dest: /path/to/foo.tgz
- name: Compress regular file /path/to/foo into /path/to/foo.gz and remove it
archive:
path: /path/to/foo
remove: yes
- name: Create a zip archive of /path/to/foo
archive:
path: /path/to/foo
format: zip
- name: Create a bz2 archive of multiple files, rooted at /path
archive:
path:
- /path/to/foo
- /path/wong/foo
dest: /path/file.tar.bz2
format: bz2
- name: Create a bz2 archive of a globbed path, while excluding specific dirnames
archive:
path:
- /path/to/foo/*
dest: /path/file.tar.bz2
exclude_path:
- /path/to/foo/bar
- /path/to/foo/baz
format: bz2
- name: Create a bz2 archive of a globbed path, while excluding a glob of dirnames
archive:
path:
- /path/to/foo/*
dest: /path/file.tar.bz2
exclude_path:
- /path/to/foo/ba*
format: bz2
- name: Use gzip to compress a single archive (i.e don't archive it first with tar)
archive:
path: /path/to/foo/single.file
dest: /path/file.gz
format: gz
- name: Create a tar.gz archive of a single file.
archive:
path: /path/to/foo/single.file
dest: /path/file.tar.gz
format: gz
force_archive: true
使用范例
# 打包站点目录
[root@k8s-master-01 ansible]# ansible code -i hosts -m archive -a 'path=/code dest=/tmp/code.tar.gz'
# 解压控制端的包到受控端
[root@k8s-master-01 ansible]# ansible code -i hosts -m unarchive -a 'src=/package/nginx.tar.gz dest=/tmp/'
# 解压受控端的包到受控端
[root@k8s-master-01 ansible]# ansible code -i hosts -m unarchive -a 'src=/package/zabbix.tar.gz dest=/tmp/ remote_src=yes'
4、lineinfile
ansible lineinfile模块用于在文件中查找指定的行,并对其进行修改、添加或删除操作。
- 设置禁用swap分区
[root@k8s-master-01 ansible]# ansible client -i hosts -m lineinfile -a "dest=/etc/fstab regexp=.*swap line=''"
dest:指定要操作的文件
regexp:为正则匹配
line:替换的内容
- 设置selinux为disabled
[root@k8s-master-01 ansible]# ansible client -i hosts -m lineinfile -a "dest=/etc/selinux/config regexp=^SELINUX line='SELINUX=disabled'"
- 文件中追加行
create=yes :表示目标文件不存在就先创建再追加line关键字设置的字符,文件存在则追加。
[root@k8s-master-01 ansible]# ansible client -i hosts -m lineinfile -a "path=/tmp/testfile line=hello world create=yes"
5、script模块
script 模块可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,不需要手动拷贝到远程主机后再执行。它可以用于在远程主机上执行任何可执行文件,包括bash脚本、Python脚本等。
使用script模块执行远程脚本,在远程主机/tmp目录下创建1、2、3三个文件
[root@k8s-master-01 ansible]# cat /opt/ansible/script-demo.sh
#!/bin/bash
touch /tmp/{1,2,3}
[root@k8s-master-01 ansible]# ansible client -i hosts -m script -a '/opt/ansible/script-demo.sh'
验证:
本章节的内容到这里就结束了,这些都是比较基础的,大家还是要勤加练习,从下一章节开始学习playbook剧本,通过playbook的逻辑判断结合多个模块可以完成更为复杂的部署任务。