Serialization 如何在远程主机上执行命令?

Serialization 如何在远程主机上执行命令?,serialization,ssh,ansible,Serialization,Ssh,Ansible,如果目标主机上没有代理,Ansible可以执行诸如添加用户(-m个用户)之类的任务 为了理解这一点,我读了以下内容: Ansible的工作原理是连接到节点并向节点推出称为“Ansible模块”的小程序。这些程序被编写为系统所需状态的资源模型 为了理解这一点,我的解释是,user模块是位于控制服务器中的python模块,在运行带有-m user选项的ansible命令后,该模块在连接到目标主机的线路上序列化 ansible是否通过ssh序列化这些程序(user源代码)?要在远程主机上执行 此序列

如果目标主机上没有代理,Ansible可以执行诸如添加用户(-m个用户)之类的任务

为了理解这一点,我读了以下内容:

Ansible的工作原理是连接到节点并向节点推出称为“Ansible模块”的小程序。这些程序被编写为系统所需状态的资源模型

为了理解这一点,我的解释是,
user
模块是位于控制服务器中的python模块,在运行带有
-m user
选项的
ansible
命令后,该模块在连接到目标主机的线路上序列化


ansible
是否通过ssh序列化这些程序(
user
源代码)?要在远程主机上执行


此序列化是否涉及ssh代理转发技术?

当ansible执行playbook中的模块时,它会将需要使用遇到的参数运行的代码序列化到名为
/.ansible/tmp/ansible local-/tmp
的本地python文件中

使用该主机的声明连接(ssh、docker、local…),将该文件上载到
/.ansible/tmp/ansible tmp-/AnsiballZ_uuz.py
中的远程主机

python文件通过该连接在远程主机上执行,结果被回迁到本地计算机,文件被清理

使用
ansible playbook
-vvv
选项(或
ansible
发送特殊命令时),您可以看到所有这些是如何执行的。下面是一个在本地机器上针对docker主机运行模块的示例

任务:

- name: Check if SystemD service is installed
  stat:
    path: /etc/systemd/system/nexus.service
  register: nexus_systemd_service_file
使用
-vvv
运行。文件复制到远程从第7行开始

    TASK [nexus3-oss : Check if SystemD service is installed] **********************
    task path: /projects/ansible/nexus3-oss/tasks/main.yml:13
    <nexus3-oss-debian-stretch> ESTABLISH DOCKER CONNECTION FOR USER: root
    <nexus3-oss-debian-stretch> EXEC ['/usr/bin/docker', b'exec', b'-i', 'nexus3-oss-debian-stretch', '/bin/sh', '-c', "/bin/sh -c 'echo ~ && sleep 0'"]
    <nexus3-oss-debian-stretch> EXEC ['/usr/bin/docker', b'exec', b'-i', 'nexus3-oss-debian-stretch', '/bin/sh', '-c', '/bin/sh -c \'( umask 77 && mkdir -p "` echo /home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721 `" && echo ansible-tmp-1555848182.1761565-31974482443721="` echo /deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721 `" ) && sleep 0\'']
    Using module file /home/localuser/.local/lib/python3.6/site-packages/ansible/modules/files/stat.py
    <nexus3-oss-debian-stretch> PUT /home/localuser/.ansible/tmp/ansible-local-30458wt820190/tmpq2vjarrv TO /home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721/AnsiballZ_stat.py
    <nexus3-oss-debian-stretch> EXEC ['/usr/bin/docker', b'exec', b'-i', 'nexus3-oss-debian-stretch', '/bin/sh', '-c', "/bin/sh -c 'chmod u+x /home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721/ /home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721/AnsiballZ_stat.py && sleep 0'"]
    <nexus3-oss-debian-stretch> EXEC ['/usr/bin/docker', b'exec', b'-i', 'nexus3-oss-debian-stretch', '/bin/sh', '-c', '/bin/sh -c \'http_proxy=\'"\'"\'\'"\'"\' https_proxy=\'"\'"\'\'"\'"\' no_proxy=\'"\'"\'\'"\'"\' /usr/bin/python /home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721/AnsiballZ_stat.py && sleep 0\'']
    <nexus3-oss-debian-stretch> EXEC ['/usr/bin/docker', b'exec', b'-i', 'nexus3-oss-debian-stretch', '/bin/sh', '-c', "/bin/sh -c 'rm -f -r /home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721/ > /dev/null 2>&1 && sleep 0'"]
    ok: [nexus3-oss-debian-stretch] => {
        "changed": false,
        "invocation": {
            "module_args": {
                "checksum_algorithm": "sha1",
                "follow": false,
                "get_attributes": true,
                "get_checksum": true,
                "get_md5": null,
                "get_mime": true,
                "path": "/etc/systemd/system/nexus.service"
            }
        },
        "stat": {
            "atime": 1555848116.0796735,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 8,
            "charset": "us-ascii",
            "checksum": "f1de2c2bc91adc019e58f83a29c970d1d79d5cc9",
            "ctime": 1553622777.8884165,
            "dev": 77,
            "device_type": 0,
            "executable": false,
            "exists": true,
            "gid": 0,
            "gr_name": "root",
            "inode": 22997,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mimetype": "text/plain",
            "mode": "0644",
            "mtime": 1553622777.3485653,
            "nlink": 1,
            "path": "/etc/systemd/system/nexus.service",
            "pw_name": "root",
            "readable": true,
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 248,
            "uid": 0,
            "version": "687353",
            "wgrp": false,
            "woth": false,
            "writeable": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    }

TASK[nexus3 oss:检查是否安装了SystemD服务]**********************
任务路径:/projects/ansible/nexus3 oss/tasks/main.yml:13
为用户建立DOCKER连接:root
EXEC['/usr/bin/docker',b'EXEC',b'-i',nexus3-oss-debian-stretch','/bin/sh','-c',“/bin/sh-c'echo~&&sleep 0']
EXEC['/usr/bin/docker',b'EXEC',b'-i',nexus3 oss debian stretch','/bin/sh','-c','/bin/sh-c\'(umask 77&&mkdir-p“`echo/home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721`”和&echo-ansible-tmp-155584848182.1761565-319744824421=“`echo/deployuser/.ansible/tmp-1554444828221''&&sleep 0\'']
使用模块文件/home/localuser/.local/lib/python3.6/site-packages/ansible/modules/files/stat.py
将/home/localuser/.ansible/tmp/ansible-local-30458wt820190/tmpq2vjarrv放到/home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721/AnsiballZ_stat.py
EXEC['/usr/bin/docker',b'EXEC',b'-i',nexus3 oss debian stretch','/bin/sh','-c',“/bin/sh-c'chmod u+x/home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721//home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721/AnsiballZ\u stat&&sleep 0']
EXEC['/usr/bin/docker',b'EXEC',b'-i',nexus3 oss debian stretch','/bin/sh','-c','/bin/sh-c'http\u proxy=\'''''''''''''''''''''http\u proxy=\''''''''''''''''''''''''''''''''''''''''''''''''/usr/bin/python/home/deployuser/.ansible/tmp/ansible-tmp/ansible-tmp-tmp-5858585865-374482.176LLISU''''''''''''''''''''''''''
EXEC['/usr/bin/docker',b'EXEC',b'-i',nexus3 oss debian stretch','/bin/sh','-c',“/bin/sh-c'rm-f-r/home/deployuser/.ansible/tmp/ansible-tmp-1555848182.1761565-31974482443721/>/dev/null 2>&1&&sleep 0']
ok:[nexus3 oss debian stretch]=>{
“更改”:错误,
“调用”:{
“模块参数”:{
“校验和算法”:“sha1”,
“跟随”:错误,
“获取属性”:true,
“获取校验和”:真,
“get_md5”:空,
“get_mime”:对,
“路径”:“/etc/systemd/system/nexus.service”
}
},
“统计”:{
“atime”:1555848116.0796735,
“属性标志”:“,
“属性”:[],
“块大小”:4096,
“块”:8,
“字符集”:“us ascii”,
“校验和”:“f1de2c2bc91adc019e58f83a29c970d1d79d5cc9”,
“ctime”:1553622777.8884165,
“dev”:77,
“设备类型”:0,
“可执行文件”:false,
“存在”:是的,
“gid”:0,
“gr_名称”:“根”,
“inode”:22997,
“isblk”:错误,
“ischr”:错误,
“isdir”:错误,
“isfifo”:错误,
“isgid”:错误,
“islnk”:错误,
“isreg”:没错,
“issock”:假,
“isuid”:false,
“mimetype”:“文本/普通”,
“模式”:“0644”,
“mtime”:1553622777.3485653,
“nlink”:1,
“路径”:“/etc/systemd/system/nexus.service”,
“pw_名称”:“根”,
“可读”:正确,
“rgrp”:正确,
“罗斯”:没错,
“rusr”:没错,
“尺寸”:248,
“uid”:0,
“版本”:“687353”,
“wgrp”:错误,
“woth”:假,
“可写”:正确,
“wusr”:没错,
“xgrp”:错误,
“xoth”:假,
“xusr”:错误
}
}