实例学习ansible系列(2)从Helloworld深度解析Ansible执行原理

知识点:-v -vv -vvv选项
知识点: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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5

这是一个更为简单的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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

分析

[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常用的模块的使用方法。

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/firsttry/p/10294108.html