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

实例学习ansible系列(11)常用模块之get_url/cron/synchronize

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

知识点:cron 模块用于管理对象节点 cron 任务
知识点:get_url 模块类似于 wget 和 curl 的功能,可以进行下载以及 webapi 交互等操作
知识点:synchronize 模块使用 rsync 用于控制节点和管理对象节点之间的内容同步操作。

cron 模块使用实例

事前对象节点 cron 信息确认

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "crontab -l"</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
<span class="hljs-number">10</span> * * * * echo <span class="hljs-string">`date`</span> >> <span class="hljs-regexp">/tmp/cronlog</span>.<span class="hljs-keyword">log</span>

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

利用 cron 模块向对象节点添加一个叫做 sayhellojob 的一个无聊 job,此 job 每 2 分钟说一次 hello

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m cron -a 'name=sayhellojob minute=*/2 hour=* day=* month=* weekday=* job="echo hello `date` >> /tmp/cronlog.log"'</span>
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"envs"</span>: [],
    <span class="hljs-string">"jobs"</span>: [
        <span class="hljs-string">"sayhellojob"</span>
    ]
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

事后对象节点的 crontab 内容

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "crontab -l"</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
<span class="hljs-number">10</span> * * * * echo <span class="hljs-string">`date`</span> >> <span class="hljs-regexp">/tmp/cronlog</span>.<span class="hljs-keyword">log</span>
<span class="hljs-comment">#Ansible: sayhellojob  ->jobname 是作为注释管理的</span>
<span class="hljs-variable">*/</span><span class="hljs-number">2</span> * * * * echo hello <span class="hljs-string">`date`</span> >> <span class="hljs-regexp">/tmp/cronlog</span>.<span class="hljs-keyword">log</span>

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

事后输出 log 的确认

<code class=" hljs ruby">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "cat /tmp/cronlog.log"</span>
host32 | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
hello <span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">06</span><span class="hljs-symbol">:</span><span class="hljs-number">01</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>
hello <span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span>08<span class="hljs-symbol">:</span><span class="hljs-number">01</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>
<span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">10</span><span class="hljs-symbol">:</span><span class="hljs-number">01</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>
hello <span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">10</span><span class="hljs-symbol">:</span><span class="hljs-number">01</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>
hello <span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">12</span><span class="hljs-symbol">:</span><span class="hljs-number">02</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>
hello <span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">14</span><span class="hljs-symbol">:</span><span class="hljs-number">01</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>
hello <span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">16</span><span class="hljs-symbol">:</span><span class="hljs-number">01</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>
hello <span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">18</span><span class="hljs-symbol">:</span><span class="hljs-number">01</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>
hello <span class="hljs-constant">Sat</span> <span class="hljs-constant">Jul</span> <span class="hljs-number">30</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">20</span><span class="hljs-symbol">:</span><span class="hljs-number">01</span> <span class="hljs-constant">EDT</span> <span class="hljs-number">2016</span>

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

get_url 使用实例

使用 get_url 下载 baidu 的 index

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host31 -m get_url -a "url=http://www.baidu.com dest=/tmp"</span>
host31 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"checksum_dest"</span>: <span class="hljs-literal">null</span>,
    <span class="hljs-string">"checksum_src"</span>: <span class="hljs-string">"2fccd8d95deeee8058575bd727057f56bde82875"</span>,
    <span class="hljs-string">"dest"</span>: <span class="hljs-string">"/tmp/index.html"</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">"903a4edc8fef9db948f2fb5533a66f6c"</span>,
    <span class="hljs-string">"mode"</span>: <span class="hljs-string">"0644"</span>,
    <span class="hljs-string">"msg"</span>: <span class="hljs-string">"OK (unknown bytes)"</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:user_tmp_t:s0"</span>,
    <span class="hljs-string">"size"</span>: <span class="hljs-number">99451</span>,
    <span class="hljs-string">"src"</span>: <span class="hljs-string">"/tmp/tmp84dCzW"</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>,
    <span class="hljs-string">"url"</span>: <span class="hljs-string">"http://www.baidu.com"</span>
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

下载确认

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ll /tmp/index.html</span>
-rw-r--r--. <span class="hljs-number">1</span> root root <span class="hljs-number">99451</span> Jul <span class="hljs-number">30</span> <span class="hljs-number">19</span>:<span class="hljs-number">35</span> /tmp/<span class="hljs-keyword">index</span>.html
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

synchronize 使用实例

同步内容准备和确认

<code class=" hljs ruby">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># mkdir -p /tmp/tst-syn /tmp/tst-syn/src /tmp/tst-syn/target /tmp/tst-syn/target/bin</span>
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># echo "hello" > /tmp/tst-syn/target/bin/hello</span>
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span>
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ssh host32 ls -l /opt</span>
total <span class="hljs-number">0</span>
drwxr-xr-x. <span class="hljs-number">2</span> root root <span class="hljs-number">6</span> <span class="hljs-constant">Mar</span> <span class="hljs-number">26</span>  <span class="hljs-number">2015</span> rh
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

将此目录结构完整同步到对象机器的/opt 下

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m synchronize -a "src=/tmp/tst-syn dest=/opt/dst-syn"</span>
host32 | <span class="hljs-string">SUCCESS =></span> {
    <span class="hljs-string">"changed"</span>: true,
    <span class="hljs-string">"cmd"</span>: <span class="hljs-string">"/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>><span class="hljs-variable">%i</span> <span class="hljs-variable">%n</span><span class="hljs-variable">%L</span>' \"/tmp/tst-syn\" \"host32:/opt/dst-syn\""</span>,
    <span class="hljs-string">"msg"</span>: <span class="hljs-string">"cd+++++++++ tst-syn/\ncd+++++++++ tst-syn/src/\ncd+++++++++ tst-syn/target/\ncd+++++++++ tst-syn/target/bin/\n<f+++++++++ tst-syn/target/bin/hello\n"</span>,
    <span class="hljs-string">"rc"</span>: <span class="hljs-number">0</span>,
    <span class="hljs-string">"stdout_lines"</span>: [
        <span class="hljs-string">"cd+++++++++ tst-syn/"</span>,
        <span class="hljs-string">"cd+++++++++ tst-syn/src/"</span>,
        <span class="hljs-string">"cd+++++++++ tst-syn/target/"</span>,
        <span class="hljs-string">"cd+++++++++ tst-syn/target/bin/"</span>,
        <span class="hljs-string">"<f+++++++++ tst-syn/target/bin/hello"</span>
    ]
}
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

同步后确认

<code class=" hljs avrasm">[root@host31 ~]<span class="hljs-preprocessor"># ssh host32 ls -l /opt</span>
total <span class="hljs-number">0</span>
drwxr-xr-<span class="hljs-built_in">x</span>. <span class="hljs-number">3</span> root root <span class="hljs-number">20</span> Jul <span class="hljs-number">30</span> <span class="hljs-number">19</span>:<span class="hljs-number">44</span> dst-syn
drwxr-xr-<span class="hljs-built_in">x</span>. <span class="hljs-number">2</span> root root  <span class="hljs-number">6</span> Mar <span class="hljs-number">26</span>  <span class="hljs-number">2015</span> rh
[root@host31 ~]<span class="hljs-preprocessor"># ssh host32 find /opt/dst-syn -type f</span>
/opt/dst-syn/<span class="hljs-keyword">tst</span>-syn/target/bin/hello
[root@host31 ~]<span class="hljs-preprocessor"># ssh host32 find /opt/dst-syn -type d</span>
/opt/dst-syn
/opt/dst-syn/<span class="hljs-keyword">tst</span>-syn
/opt/dst-syn/<span class="hljs-keyword">tst</span>-syn/src
/opt/dst-syn/<span class="hljs-keyword">tst</span>-syn/target
/opt/dst-syn/<span class="hljs-keyword">tst</span>-syn/target/bin
[root@host31 ~]<span class="hljs-preprocessor">#</span></code>

本文转载自 实例学习 ansible 系列(11)常用模块之 get_url/cron/synchronize


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