Copy模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[root@m01 ~]# ansible-doc copy EXAMPLES: - name: Copy file with owner and permissions copy: src: /srv/myfiles/foo.conf dest: /etc/foo.conf owner: foo group: foo mode: '0644' backup: yes follow: yes content: '# This file was moved to /etc/other.conf' src: #源路径(要进行copy的文件,文件在控制端) dest: #目标路径(在受控端) owner: #文件推过去之后的属主 group: #文件推过去之后的属组 mode: #文件推过去之后的权限 backup: #文件件是否备份 yes #备份 no #不备份 follow: #是否识别软链接 yes no content: #直接写入内容到文件 |
2)模块实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#推送nginx官方源 [root@m01 ~]# ansible 'web_group' -m copy -a 'src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/' #推送文件并授权 [root@m01 ~]# ansible 'web_group' -m copy -a 'src=/root/test.conf dest=/etc/nginx/conf.d/ owner=root group=root mode=777' #推送文件并备份 [root@m01 ~]# ansible 'web_group' -m copy -a 'src=/code/index.html dest=/code owner=nginx group=nginx mode=0644 backup=yes' #识别软链接 [root@m01 ~]# ansible 'web_group' -m copy -a 'src=/root/test dest=/tmp owner=nginx group=nginx mode=0644 follow=yes' [root@m01 ~]# ansible 'web_group' -m copy -a 'src=/root/test dest=/tmp owner=nginx group=nginx mode=0644 follow=no' #直接写入内容到文件 [root@m01 ~]# ansible 'web_group' -m copy -a 'content="123456" dest=/etc/rsync_password mode=0600' |
File模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[root@m01 ~]# ansible-doc file EXAMPLES: - name: Change file ownership, group and permissions file: path: /etc/foo.conf owner: foo group: foo mode: '0644' state: link,hard,touch,directory,absent recurse: yes src: #源文件(如果做软链接就是远程机器上的文件) dest: #目标文件(如果做软链接就是远程机器上的链接文件) path: #路径/文件 owner: #文件或目录的属主 group: #文件或目录的属组 mode: #文件或目录的权限 state: link #软链接 touch #创建文件 directory #创建目录 absent #删除 recurse: #递归操作 yes |
2)file模块实践
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
1.创建目录 [root@m01 ~]# ansible 'web_group' -m file -a 'path=/code state=directory' #相当于到远程机器 mkdir /code 2.创建目录并授权 [root@m01 ~]# ansible 'web01' -m file -a 'path=/code state=directory owner=nginx group=nginx mode=755' #相当于执行 mkdir /code && chown -R nginx.nginx /code && chmod 755 /code 3.递归创建目录 [root@m01 ~]# ansible 'web01' -m file -a 'path=/code/wordpress/wp-content/uploads state=directory owner=nginx group=nginx mode=755' 1)如果目录不存在则创建并递归授权 2)如果目录上级存在,则只授权新创建的目录 4.递归授权目录 [root@m01 ~]# ansible 'web01' -m file -a 'path=/code state=directory owner=nginx group=nginx mode=755 recurse=yes' 5.创建文件 [root@m01 ~]# ansible 'web01' -m file -a 'path=/code/1.txt state=touch owner=nginx group=nginx mode=666' 6.删除文件 [root@m01 ~]# ansible 'web01' -m file -a 'path=/code/index.html state=absent' 7.做软连接 [root@m01 ~]# ansible 'web01' -m file -a 'src=/code/wordpress dest=/code/link state=link' #注意: 1.创建文件时,上层目录必须存在 |
Get_url 模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@m01 ~]# ansible-doc get_url EXAMPLES: - name: Download foo.conf get_url: url: http://example.com/path/file.conf dest: /etc/foo.conf mode: '0440' checksum: sha256:http://example.com/path/sha256sum.txt url: #下载文件的地址 dest: #下载保存的路径 mode: #下载之后的权限 checksum: #下载时进行验证 sha256:http://example.com/path/sha256sum.txt md5 |
2)模块实例
1 2 3 4 5 6 7 8 9 10 11 |
#下载包 [root@m01 ~]# ansible 'web_group' -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm dest=/tmp/' #下载包并授权 [root@m01 ~]# ansible 'web_group' -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm dest=/tmp/ mode=777' #下载包时验证 [root@linux /code]# md5sum index.html ba1f2511fc30423bdbb183fe33f3dd0f index.html [root@m01 ~]# ansible 'web03' -m get_url -a 'url=http://10.0.0.7/index.html dest=/tmp mode=777 checksum=md5:ba1f2511fc30423bdbb183fe33f3dd0f' |