• 欢迎访问蜷缩的蜗牛博客 蜷缩的蜗牛
  • 微信搜索: 蜷缩的蜗牛 | 联系站长 kbsonlong@qq.com
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

实例学习ansible系列(5)常用模块之copy

Ansible 蜷缩的蜗牛 8个月前 (01-23) 14次浏览 已收录

知识点:使用 copy 模块,可以实现向目标机器进行远程 copy 的能力。

使用参数说明

参数 说明
src 被复制到远程主机的本地对象文件或者文件夹,可以是绝对路径,也可以是相对路径。
dest 被复制到远程主机的本地对象文件或者文件夹
mode 复制对象的设定权限
backup 在文件存在的时候可以选择覆盖之前,将源文件备份.设定值:yes/no 缺省为 yes
force 是否强制覆盖.设定值:yes/no 缺省为 no
其余请自行 ansible-doc -s copy

使用实例

使用 ansible 的 copy 的 module 将 ttt.sh 文件 copy 到远程的目标机上并命名为 hello.sh

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m command -a /tmp/hello.sh</span>
host32 | FAILED | rc=<span class="hljs-number">2</span> >>
[Errno <span class="hljs-number">2</span>] No such file <span class="hljs-keyword">or</span> directory

[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m copy -a "src=/tmp/ttt.sh dest=/tmp/hello.sh mode=0750"</span>
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"checksum"</span>: <span class="hljs-string">"098994f5d86562667b71ec90d13904eedf1be5f1"</span>,
    <span class="hljs-string">"dest"</span>: <span class="hljs-string">"/tmp/hello.sh"</span>,
    <span class="hljs-string">"gid"</span>: <span class="hljs-number">0</span>,
    <span class="hljs-string">"group"</span>: <span class="hljs-string">"root"</span>,
    <span class="hljs-string">"md5sum"</span>: <span class="hljs-string">"fcc7e6c36e7a19db4b69fab163e03a36"</span>,
    <span class="hljs-string">"mode"</span>: <span class="hljs-string">"0750"</span>,
    <span class="hljs-string">"owner"</span>: <span class="hljs-string">"root"</span>,
    <span class="hljs-string">"secontext"</span>: <span class="hljs-string">"unconfined_u:object_r:admin_home_t:s0"</span>,
    <span class="hljs-string">"size"</span>: <span class="hljs-number">31</span>,
    <span class="hljs-string">"src"</span>: <span class="hljs-string">"/root/.ansible/tmp/ansible-tmp-1469870735.2-12356407661121/source"</span>,
    <span class="hljs-string">"state"</span>: <span class="hljs-string">"file"</span>,
    <span class="hljs-string">"uid"</span>: <span class="hljs-number">0</span>
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m command -a /tmp/hello.sh</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
hello world

[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

force 使用实例

default 的情况下,force 是 yes 的,所以什么都不写,文件存在的情况是会被覆盖的,如下所示。

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m raw -a "ls -l /tmp/hello.sh"</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
-rwxr-x---. <span class="hljs-number">1</span> root root <span class="hljs-number">31</span> Jul <span class="hljs-number">30</span> <span class="hljs-number">05</span>:<span class="hljs-number">25</span> /tmp/hello.sh


[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># touch /tmp/ttt</span>
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ll /tmp/ttt</span>
-rw-r--r--. <span class="hljs-number">1</span> root root <span class="hljs-number">0</span> Jul <span class="hljs-number">30</span> <span class="hljs-number">05</span>:<span class="hljs-number">39</span> /tmp/ttt
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m copy -a "src=/tmp/ttt dest=/tmp/hello.sh"</span>
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"checksum"</span>: <span class="hljs-string">"da39a3ee5e6b4b0d3255bfef95601890afd80709"</span>,
    <span class="hljs-string">"dest"</span>: <span class="hljs-string">"/tmp/hello.sh"</span>,
    <span class="hljs-string">"gid"</span>: <span class="hljs-number">0</span>,
    <span class="hljs-string">"group"</span>: <span class="hljs-string">"root"</span>,
    <span class="hljs-string">"md5sum"</span>: <span class="hljs-string">"d41d8cd98f00b204e9800998ecf8427e"</span>,
    <span class="hljs-string">"mode"</span>: <span class="hljs-string">"0750"</span>,
    <span class="hljs-string">"owner"</span>: <span class="hljs-string">"root"</span>,
    <span class="hljs-string">"secontext"</span>: <span class="hljs-string">"unconfined_u:object_r:admin_home_t:s0"</span>,
    <span class="hljs-string">"size"</span>: <span class="hljs-number">0</span>,
    <span class="hljs-string">"src"</span>: <span class="hljs-string">"/root/.ansible/tmp/ansible-tmp-1469871615.42-248549508847058/source"</span>,
    <span class="hljs-string">"state"</span>: <span class="hljs-string">"file"</span>,
    <span class="hljs-string">"uid"</span>: <span class="hljs-number">0</span>
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m raw -a "ls -l /tmp/hello.sh"</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
-rwxr-x---. <span class="hljs-number">1</span> root root <span class="hljs-number">0</span> Jul <span class="hljs-number">30</span> <span class="hljs-number">05</span>:<span class="hljs-number">40</span> /tmp/hello.sh


[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

明确写成 force=no,此时将不会被覆盖。

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "ls -l /tmp/hello.sh"</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
-rwxr-x---. <span class="hljs-number">1</span> root root <span class="hljs-number">0</span> Jul <span class="hljs-number">30</span> <span class="hljs-number">05</span>:<span class="hljs-number">40</span> /tmp/hello.sh

[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ll /tmp/ttt.sh</span>
-rwxr-x---. <span class="hljs-number">1</span> root root <span class="hljs-number">31</span> Jul <span class="hljs-number">30</span> <span class="hljs-number">03</span>:<span class="hljs-number">32</span> /tmp/ttt.sh
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m copy -a "src=/tmp/ttt.sh dest=/tmp/hello.sh force=no"</span>
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-string">"dest"</span>: <span class="hljs-string">"/tmp/hello.sh"</span>,
    <span class="hljs-string">"src"</span>: <span class="hljs-string">"/tmp/ttt.sh"</span>
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "ls -l /tmp/hello.sh"</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
-rwxr-x---. <span class="hljs-number">1</span> root root <span class="hljs-number">0</span> Jul <span class="hljs-number">30</span> <span class="hljs-number">05</span>:<span class="hljs-number">40</span> /tmp/hello.sh

[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

backup 使用实例

覆盖的动作作出之前,其会真正覆盖之前,会作出一个带时间戳的文件作为 backup 文件

<code class=" hljs ruby">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m copy -a "src=/tmp/ttt.sh dest=/tmp/hello.sh backup=yes"</span>
host32 | <span class="hljs-constant">SUCCESS</span> => {
    <span class="hljs-string">"backup_file"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"/tmp/hello.sh.2016-07-30@05:59:50~"</span>,
    <span class="hljs-string">"changed"</span><span class="hljs-symbol">:</span> <span class="hljs-keyword">true</span>,
    <span class="hljs-string">"checksum"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"098994f5d86562667b71ec90d13904eedf1be5f1"</span>,
    <span class="hljs-string">"dest"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"/tmp/hello.sh"</span>,
    <span class="hljs-string">"gid"</span><span class="hljs-symbol">:</span> <span class="hljs-number">0</span>,
    <span class="hljs-string">"group"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"root"</span>,
    <span class="hljs-string">"md5sum"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"fcc7e6c36e7a19db4b69fab163e03a36"</span>,
    <span class="hljs-string">"mode"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"0750"</span>,
    <span class="hljs-string">"owner"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"root"</span>,
    <span class="hljs-string">"secontext"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"unconfined_u:object_r:admin_home_t:s0"</span>,
    <span class="hljs-string">"size"</span><span class="hljs-symbol">:</span> <span class="hljs-number">31</span>,
    <span class="hljs-string">"src"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"/root/.ansible/tmp/ansible-tmp-1469872789.7-172371209052357/source"</span>,
    <span class="hljs-string">"state"</span><span class="hljs-symbol">:</span> <span class="hljs-string">"file"</span>,
    <span class="hljs-string">"uid"</span><span class="hljs-symbol">:</span> <span class="hljs-number">0</span>
}
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "ls -l /tmp/hello*"</span>
host32 | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
-rwxr-x---. <span class="hljs-number">1</span> root root <span class="hljs-number">31</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">05</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span> /tmp/hello.sh
-rwxr-x---. <span class="hljs-number">1</span> root root  <span class="hljs-number">0</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">05</span><span class="hljs-symbol">:</span><span class="hljs-number">40</span> /tmp/hello.sh.<span class="hljs-number">2016</span>-<span class="hljs-number">07</span>-<span class="hljs-number">30</span><span class="hljs-variable">@05</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span><span class="hljs-symbol">:</span><span class="hljs-number">50</span>~ -〉此文件为 backup 文件
-rw-r--r--. <span class="hljs-number">1</span> root root <span class="hljs-number">12</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">29</span> <span class="hljs-number">10</span><span class="hljs-symbol">:</span><span class="hljs-number">18</span> /tmp/helloworld

[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

本文转载自 实例学习 ansible 系列(5)常用模块之 copy


蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明实例学习 ansible 系列(5)常用模块之 copy
喜欢 (0)
[]
分享 (0)