实例学习ansible系列(2)从Helloworld深度解析Ansible执行原理
2016-07-30 07:09
1296 查看
知识点:-v -vv -vvv选项
知识点:ansible执行原理
ansible与puppet等相比,其号称是agentless的,而且这个也确实在很多台机器上进行运维时不用一台一台安装或者升级客户端确实带来了一定的便利。Ansible为什么能够实现不需要agent呢,原理在于其将要执行的命令或者脚本通过sftp的方式传到要执行的对象机器,然后通过ssh远程执行,执行之后清理现场将sftp传过去的文件删除,好像一切都没有发生过的一样,这个就是ansible不需要agent的原理。
口说无凭,上实例。
这是一个更为简单的helloworld,-v的选项是显示出详细信息。ansible支持三种显示信息的方式
-v
-vv
-vvv
我们接下来使用-vvv来再看ansible是如何动作的
分析
[root@host31 ~]# ansible host32 -m command -a “echo hello world” -vvv
Using /etc/ansible/ansible.cfg as config file
ESTABLISH SSH CONNECTION FOR USER: None
SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r host32 ‘/bin/sh -c ‘”’”’( umask 77 && mkdir -p “
PUT /tmp/tmpUjtNjh TO /root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/command
到此处我们可以清晰地看到ansible做了两件事情
- 通过ssh保证通路的畅通
- 将要执行的echo hello world放到了一个文件中
SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r ‘[host32]’
到此处可以看到通过sftp传送到host32
ESTABLISH SSH CONNECTION FOR USER: None
SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt host32 ‘/bin/sh -c ‘”’“‘LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/command; rm -rf “/root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/” > /dev/null 2>&1 && sleep 0’”’””
通过ssh远程执行,执行之后清理现场将sftp传过去的文件删除
host32 | SUCCESS | rc=0 >>
hello world
[root@host31 ~]#
总结:
Ansible是非常强大的工具,但是归根到底也就是基于python或者ssh或者其他实现了ssh模块的这样一个功能。这件事情从非常古老的计算机时代就开始是如此,为什么ansible如此之流行,linux的推广以及总多模块的支持以及活跃的参与者和社区是其不断进步的重要原因,接下来我们会学习ansible常用的模块的使用方法。
知识点:ansible执行原理
ansible与puppet等相比,其号称是agentless的,而且这个也确实在很多台机器上进行运维时不用一台一台安装或者升级客户端确实带来了一定的便利。Ansible为什么能够实现不需要agent呢,原理在于其将要执行的命令或者脚本通过sftp的方式传到要执行的对象机器,然后通过ssh远程执行,执行之后清理现场将sftp传过去的文件删除,好像一切都没有发生过的一样,这个就是ansible不需要agent的原理。
口说无凭,上实例。
[root@host31 ~]# ansible host32 -m command -a "echo hello world" -v Using /etc/ansible/ansible.cfg as config file host32 | SUCCESS | rc=0 >> hello world [root@host31 ~]#
这是一个更为简单的helloworld,-v的选项是显示出详细信息。ansible支持三种显示信息的方式
-v
-vv
-vvv
我们接下来使用-vvv来再看ansible是如何动作的
[root@host31 ~]# ansible host32 -m command -a "echo hello world" -vvv Using /etc/ansible/ansible.cfg as config file <host32> ESTABLISH SSH CONNECTION FOR USER: None <host32> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r host32 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064 `" && echo ansible-tmp-1469831679.43-224816968104064="` echo $HOME/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064 `" ) && sleep 0'"'"'' <host32> PUT /tmp/tmpUjtNjh TO /root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/command <host32> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r '[host32]' <host32> ESTABLISH SSH CONNECTION FOR USER: None <host32> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt host32 '/bin/sh -c '"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/command; rm -rf "/root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/" > /dev/null 2>&1 && sleep 0'"'"'' host32 | SUCCESS | rc=0 >> hello world [root@host31 ~]#
分析
[root@host31 ~]# ansible host32 -m command -a “echo hello world” -vvv
Using /etc/ansible/ansible.cfg as config file
ESTABLISH SSH CONNECTION FOR USER: None
SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r host32 ‘/bin/sh -c ‘”’”’( umask 77 && mkdir -p “
echo $HOME/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064” && echo ansible-tmp-1469831679.43-224816968104064=”
echo $HOME/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064” ) && sleep 0’”’””
PUT /tmp/tmpUjtNjh TO /root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/command
到此处我们可以清晰地看到ansible做了两件事情
- 通过ssh保证通路的畅通
- 将要执行的echo hello world放到了一个文件中
SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r ‘[host32]’
到此处可以看到通过sftp传送到host32
ESTABLISH SSH CONNECTION FOR USER: None
SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt host32 ‘/bin/sh -c ‘”’“‘LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/command; rm -rf “/root/.ansible/tmp/ansible-tmp-1469831679.43-224816968104064/” > /dev/null 2>&1 && sleep 0’”’””
通过ssh远程执行,执行之后清理现场将sftp传过去的文件删除
host32 | SUCCESS | rc=0 >>
hello world
[root@host31 ~]#
总结:
Ansible是非常强大的工具,但是归根到底也就是基于python或者ssh或者其他实现了ssh模块的这样一个功能。这件事情从非常古老的计算机时代就开始是如此,为什么ansible如此之流行,linux的推广以及总多模块的支持以及活跃的参与者和社区是其不断进步的重要原因,接下来我们会学习ansible常用的模块的使用方法。
相关文章推荐
- 基于 ANSIBLE 自动化运维实践
- 运维入门
- 运维提升
- 【DevOps】为什么我们永远疲于奔命?
- 二级域名原理以及程序
- 一根网线内的8根线哪4根是传输数据的,哪四根是防干扰的
- TCP三次握手及原理
- jQuery中的pushStack实现原理和应用实例
- sqlserver索引的原理及索引建立的注意事项小结
- php中的ini配置原理详解
- jquery原理以及学习技巧介绍
- PHP strtotime函数用法、实现原理和源码分析
- Base64编码解码原理及C#编程实例
- IsPostBack原理的介绍
- mvvm双向绑定机制的原理和实现代码(推荐)
- 浅析JSONP技术原理及实现
- jQuery中noconflict函数的实现原理分解
- 深入理解Java事务的原理与应用
- MySQL主从同步原理介绍
- 浅析Ajax的 原理及优缺点