ansible 和文件复制相关的几个模块

ansible 复制远程主机的文件到本地主机——fetch

例子1:
[root@centos_7 ansible]# cat fetched.yml

hosts: centos7
remote_user: root tasks:

  • name : fetched file from centos7 into local
    fetch:
    src: /tmp/fstab_from_centos6
    dest: /tmp/centos

说明:src - 是远程主机的文件,这个参数只能使用文件,不能使用目录
dest - 是本地主机,用来保存文件的目录。它会将这里的参数看成是目录。
注意,在执行ansible 成功后,dest 的路径会自动将远程文件保存到 /tmp/centos/remote_host/tmp/fstab_from_centos6

上面例子的结果:
[root@centos_7 ansible]# ls /tmp/centos/example.fetch.host/tmp
fstab_from_centos6
example.fetch.host 是在 /etc/ansible/hosts 主机列表里 centos7 对应的名称

例子2:保存到本地时,直接使用远程的文件名,不需要remote_host 创建新的目录

[root@centos_7 ansible]# cat fetched.yml


hosts: centos7
remote_user: root
tasks:

  • name : fetched file from centos7 into local
    fetch:
    src: /tmp/fstab_from_centos6
    dest: /tmp/
    flat: yes

增加 flat 参数,当flat 为 yes 时,有两种情况:
(1)dest 是以"/" 结尾,文件直接保存在 dest 的目录下,所以现在的结果是:fstab_from_centos6 保存在本地主机的 /tmp/fstab_from_centos6
(2) dest 不是以 “/” 结尾,则dest 被看做文件,相当于 fstab_from_centos6 会重命名再保存。

例如:[root@centos_7 ansible]# cat fetched.yml


hosts: centos7
remote_user: root
tasks:

  • name : fetched file from centos7 into local
    fetch:
    src: /tmp/fstab_from_centos6
    dest: /tmp/centos
    flat: yes

fstab_from_centos6 被保存为 /tmp/cnetos

复制本地(或远程)文件 到远程主机
#Example from Ansible Playbooks

  • copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: 0644
    #The same example as above, but using a symbolic mode equivalent to 0644

  • copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u=rw,g=r,o=rfile

模块file 模块用来修改文件、目录和符号文件的属性权限等。利用它的 state 参数,有更多丰富的用法。

修改权限属性:
[root@centos_7 ansible]# ansible centos6 -m command -a “chdir=/tmp/new/ ls 1.txt -l”

192.168.188.109 | SUCCESS | rc=0 >>-rw-r–r-- 1 root root 0 Jan 16 12:22 1.txt

[root@centos_7 ansible]# ansible centos6 -m file -a “path=/tmp/new/1.txt mode=0777”
192.168.188.109 | SUCCESS =>
{ “changed”: true, “gid”: 0, “group”: “root”, “mode”: “0777”, “owner”: “root”,
“path”: “/tmp/new/1.txt”, “size”: 0, “state”: “file”, “uid”: 0}

[root@centos_7 ansible]# ansible centos6 -m command -a “chdir=/tmp/new/ ls 1.txt -l”
192.168.188.109 | SUCCESS | rc=0 >>-rwxrwxrwx 1 root root 0 Jan 16 12:22 1.txt

说明: 将远程主机 /tmp/new/1.txt 的文件修改权限属性为0777;
ansible 命令中,path 相当于 dest,指明了目标文件。如果想修改所属主和所属组,
可以:ansible centos6 -m file -a “path=/tmp/new/1.txt mode=0755 ower=apple group=apple”
命令执行结果:
[root@centos_7 ansible]# ansible centos6 -m file -a “path=/tmp/new/1.txt mode=0755 owner=apple group=apple”

192.168.188.109 | SUCCESS => { “changed”: true, “gid”: 508, “group”: “apple”, “mode”: “0755”, “owner”: “apple”, “path”: “/tmp/new/1.txt”, “size”: 0, “state”: “file”, “uid”: 508}

[root@centos_7 ansible]# ansible centos6 -m command -a “chdir=/tmp/new/ ls 1.txt -l”

192.168.188.109 | SUCCESS | rc=0 >>-rwxr-xr-x 1 apple apple 0 Jan 16 12:22 1.txt

说明: 只要增加 owner 和 group 参数就可以了。

file 的 state 参数
熟悉 state 的几个参数,基本就满足我们平时对文件、目录和符号链接文件的操作了。先来看看 state 有几个值?

state 的值和我们要进行的操作,其实是一一对应的。

  1. state=directory – 如果想创建一个目录

[root@centos_7 ansible]# ansible test -m file -a “path=/tmp/testdir state=directory”

192.168.188.109 | SUCCESS => { “changed”: true, “gid”: 0, “group”: “root”, “mode”: “0755”, “owner”: “root”, “path”: “/tmp/testdir”, “size”: 4096, “state”: “directory”, “uid”: 0}

可以看到,在远程主机已经创建了 /tmp/testdir 目录:

[root@centos_7 ansible]# ansible test -m command -a “chdir=/tmp ls -lthr”

192.168.188.109 | SUCCESS | rc=0 >>
total 16K
-rw-r–r-- 1 root root 484 Jan 12 15:08 fstab
drwxr-xr-x 2 root root 4.0K Jan 16 12:22 new
drwxr-xr-x 2 root root 4.0K Jan 16 14:50 testdir
drwx------ 2 root root 4.0K Jan 16 14:51 ansible_IOZLxg

  1. state=touch – 创建文件

[root@centos_7 ansible]# ansible test -m file -a “path=/tmp/new/1.txt state=touch”

[root@centos_7 ansible]# ansible test -m command -a “chdir=/tmp/new ls -lthr”

192.168.188.109 | SUCCESS | rc=0 >>
total 0-rw-r–r-- 1 root root 0 Jan 16 15:00 1.txt

注意: 使用 state=file ,当 path 指定的文件不存在的时候,并不能新创建文件。
重复对相同的文件使用 touch 只能改变文件的元数据属性,例如访问时间等等 stat 查看到的数据。

  1. state=link – 创建符号链接

当stat=link 的时候,就需要使用另一个 src 的参数一起搭配使用了。

[root@centos_7 ansible]# ansible test -m file -a “path=/tmp/new/1_link.txt src=/tmp/new/1.txt state=link”
结果:

[root@centos_7 ansible]# ansible test -m command -a “ls /tmp/new -l”

192.168.188.109 | SUCCESS | rc=0 >>
total 0
lrwxrwxrwx 1 root root 14 Jan 16 15:25 1_link.txt -> /tmp/new/1.txt
-rw-r–r-- 1 root root 0 Jan 16 15:02 1.txt

说明: path 指定了生成的符号链接文件的路径, src 指定了 被链接的文件是什么。

  1. state=hard – 创建硬链接

[root@centos_7 ansible]# ansible test -m file -a “path=/tmp/new/1_hard.txt src=/tmp/new/1.txt state=hard”

[root@centos_7 ansible]# ansible test -m command -a "ls -l /tmp/new "

192.168.188.109 | SUCCESS | rc=0 >>
total 0
-rw-r–r-- 2 root root 0 Jan 16 15:02 1_hard.txt
lrwxrwxrwx 1 root root 14 Jan 16 15:25 1_link.txt -> /tmp/new/1.txt
-rw-r–r-- 2 root root 0 Jan 16 15:02 1.txt

可以看到,1.txt 和 1_hard.txt 文件的属性都是一样的。

  1. state=absent – 删除目录、文件和符号链接

[root@centos_7 ansible]# ansible test -m file -a “path=/tmp/new/1_link.txt state=absent”

这样就删除了一个符号链接文件。
template 模块

template 模块官网说明:template使用了Jinja2格式作为文件模版,进行文档内变量的替换的模块。

它的每次使用都会被ansible标记为”changed”状态。通俗来讲,就是在本地 file.j2 文件里的变量状态。

例如在file.j2 文件里有变量: username="{{ admin_username }}" password="{{ admin_password }}"。

那么,file.j2 文件会一直保持变量状态,直到 file.j2 文件被 ansible 的 template 模块执行后,文件里的变量就会被具体的值代替,
并且传送到远程主机。例如,在roles 里面调用了 template 模块,那么ansible 会在 roles 同级目录 global_vars 里找到存储变量的文件,
或者有时会在 roles 目录里的 vars 目录定义变量的文件,然后,找到对应变量的值,再将变量传递给 file.j2 文件。

### 3.1 Ansible 常用模块列表 Ansible 提供了丰富的模块来实现对远程主机的配置管理与自动化操作。以下是一些常用的模块及其功能描述: #### 1. `user` 模块 用于管理用户账户,包括创建、删除用户,设置用户密码等操作。例如: ```bash ansible 172.25.32.11 -m user -a 'name=admin password=123456' ``` 该命令将在目标主机上创建一个名为 `admin` 的用户,并设置密码为 `123456` [^2]。 #### 2. `shell` 模块 用于在受控主机上执行 Shell 命令,支持管道重定向。例如: ```bash ansible 10.10.10.135 -m shell -a 'echo "Hello Ansible" > /tmp/hello.txt' ``` 该命令将在目标主机上执行 Shell 命令,将字符串写入 `/tmp/hello.txt` 文件中 [^4]。 #### 3. `copy` 模块 用于将本地文件复制到远程主机的指定位置。例如: ```bash ansible 10.10.10.135 -m copy -a 'src=/local/file.txt dest=/remote/file.txt' ``` 该命令将本地的 `file.txt` 文件复制到远程主机的 `/remote` 目录下 [^2]。 #### 4. `file` 模块 用于管理文件属性,如权限、所有权、符号链接等。例如: ```bash ansible 10.10.10.135 -m file -a 'dest=/tmp/test.txt mode=0644' ``` 该命令将设置 `/tmp/test.txt` 文件的权限为 `0644` [^2]。 #### 5. `service` 模块 用于管理服务的启动、停止、重启状态检查。例如: ```bash ansible 10.10.10.135 -m service -a 'name=httpd state=restarted' ``` 该命令将在目标主机上重启 `httpd` 服务 。 #### 6. `yum` 模块 用于基于 Red Hat 的系统上使用 YUM 包管理器安装、卸载软件包。例如: ```bash ansible 10.10.10.135 -m yum -a 'name=httpd state=latest' ``` 该命令将安装或升级 `httpd` 软件包到最新版本 [^2]。 #### 7. `cron` 模块 用于管理定时任务。例如: ```bash ansible web -m cron -a 'name="ntp update" minute=* job="/sbin/ntpdate 210.72.145.44 &> /dev/null"' ``` 该命令将在 `web` 组主机上添加一个每分钟执行一次的定时任务 [^5]。 #### 8. `hostname` 模块 用于设置或更改远程主机的主机名。例如: ```bash ansible 172.25.32.11 -m hostname -a 'name=www.westos.org' ``` 该命令将目标主机的主机名设置为 `www.westos.org` [^3]。 #### 9. `fetch` 模块 用于从远程主机上拉取文件本地。例如: ```bash ansible 10.10.10.135 -m fetch -a 'src=/remote/file.txt dest=/local/' ``` 该命令将从远程主机的 `/remote/file.txt` 拉取文件本地 `/local/` 目录下 。 #### 10. `firewalld` 模块 用于管理 firewalld 防火墙规则。例如: ```bash ansible 10.10.10.135 -m firewalld -a 'port=80/tcp permanent=yes state=enabled' ``` 该命令将允许 `80/tcp` 端口通过防火墙,并设置为永久生效 [^2]。 #### 11. `lineinfile` 模块 用于在文件中插入、修改或删除特定行。例如: ```bash ansible 10.10.10.135 -m lineinfile -a 'dest=/etc/ssh/sshd_config regexp="^PermitRootLogin" line="PermitRootLogin no"' ``` 该命令将修改 `/etc/ssh/sshd_config` 文件中的 `PermitRootLogin` 配置项 。 #### 12. `script` 模块 用于在远程主机上执行本地脚本。例如: ```bash ansible 10.10.10.135 -m script -a '/scripts/test.sh' ``` 该命令将在目标主机上执行本地的 `test.sh` 脚本 [^4]。 #### 13. `replace` 模块 用于替换文件中的特定内容。例如: ```bash ansible 10.10.10.135 -m replace -a 'dest=/tmp/file.txt regexp="old" replace="new"' ``` 该命令将替换 `/tmp/file.txt` 中的 `old` 为 `new` 。 #### 14. `template` 模块 用于使用 Jinja2 模板引擎生成配置文件并部署到远程主机。例如: ```bash ansible 10.10.10.135 -m template -a 'src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf' ``` 该命令将根据模板生成 `httpd.conf` 文件并部署到远程主机 。 #### 15. `mount` 模块 用于管理文件系统的挂载点。例如: ```bash ansible 10.10.10.135 -m mount -a 'name=/mnt/data src=/dev/sdb1 fstype=ext4 state=mounted' ``` 该命令将挂载 `/dev/sdb1` 到 `/mnt/data` [^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值