Ansible 自动化运维企业实战 (二)

一、 invenroty 清单文件

[root@ansible ~]# vim /etc/ansible/hosts

[webserver]
host1
host2
host3
host4

提前设置域名解析:

[root@ansible ~]# vim /etc/hosts

10.36.189.89 ansible
10.36.189.85 host1
10.36.189.87 host2
10.36.189.86 host3
10.36.189.83 host4

二、 常用模块

1. Ansible ping 模块

        ping模块执行成功后,会给你返回绿色的消息,并且有一个pong响应。all代表所有被管理的主机。

[root@ansible ~]# ansible webserver -m ping
host4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
host2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
host1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
host3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

2. Ansible command 模块

        因为ansible的默认模块是command,所以这里可以使用 -m 指定模块名 command,也可以直接省略。

  • Chdir:执行命令前,切换到目录。
  • Creates:当该文件存在时,则不执行该步骤。
  • Executable:换用shell环境执行命令。
  • Free_form:需要执行的脚本。
  • Removes:当该文件不存在时,则不执行该步骤。
  • Warn:若在ansible.cfg中存在告警,如果设定了false,不会警告此行。
[root@ansible ~]# ansible webserver -m command -a "free -m"
host4 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           1819         193        1503           9         122        1486
Swap:          2047           0        2047
host1 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           1819         236        1451           9         130        1439
Swap:          2047           0        2047
host3 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           1819         235        1452           9         131        1440
Swap:          2047           0        2047
host2 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           1819         231        1456           9         131        1444
Swap:          2047           0        2047

3. Ansible shell 模块

        shell模块和command模块比较类似,但是shell被大家称为万能模块,很多操作command不支持,但是shell却支持。注意最后一种情况shell模块也是不支持的。但是可以将命令写在一个脚本,将脚本拷贝到远端执行,然后执行shell模块获取结果。

[root@ansible ~]# ansible webserver -m shell -a "touch /tmp/a.txt"
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
host4 | CHANGED | rc=0 >>

host2 | CHANGED | rc=0 >>

host3 | CHANGED | rc=0 >>

host1 | CHANGED | rc=0 >>

注意: 上面出现了WARNING警告。这不是报错,它只是告诉你,应该选择file模块进行创建文件的操作会更好,而不是使用shell模块操作。当然它还告诉你可以在ansible.cfg配置文件中设置command_warnings=False以关闭警告。force:覆盖远程主机不一致的内容。group:设定远程主机文件夹的组名。

4. Ansible copy 模块

从ansible管理节点拷贝文件到远程主机。

  • src: Ansible端源文件或者目录,空文件夹不复制。
  • content:用来替代src,用于将指定文件的内容复制到远程文件内。
  • dest:客户端目标目录或者文件,需要绝对路径。
  • backup:复制之前,先备份远程节点上的原始文件。
  • directory_mode:用于复制文件夹,新建的文件会被复制,而老旧的不会被复制。
  • follow:支持link 文件复制。
  • force:覆盖远程主机不一致的内容。
  • group:设定远程主机文件夹的组名。
  • mode:指定远程主机文件及文件夹的权限。
  • owner:设定远程主机文件夹的用户名。
[root@ansible ~]# ansible webserver -m copy -a "src=system.sh dest=/tmp/ mode=07                                                                              55 owner=root group=root"
host4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "e144cd424d5d12fea3d3487de8f382b7df53eb38",
    "dest": "/tmp/system.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "19a2e25348e58637f95072f4edcfb18b",
    "mode": "0755",
    "owner": "root",
    "size": 7276,
    "src": "/root/.ansible/tmp/ansible-tmp-1693574676.49-10625-146551214815236/s                                                                              ource",
    "state": "file",
    "uid": 0
}
host2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "e144cd424d5d12fea3d3487de8f382b7df53eb38",
    "dest": "/tmp/system.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "19a2e25348e58637f95072f4edcfb18b",
    "mode": "0755",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 7276,
    "src": "/root/.ansible/tmp/ansible-tmp-1693574676.49-10621-157641249349702/s                                                                              ource",
    "state": "file",
    "uid": 0
}
host3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "e144cd424d5d12fea3d3487de8f382b7df53eb38",
    "dest": "/tmp/system.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "19a2e25348e58637f95072f4edcfb18b",
    "mode": "0755",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 7276,
    "src": "/root/.ansible/tmp/ansible-tmp-1693574676.49-10623-267465691470688/s                                                                              ource",
    "state": "file",
    "uid": 0
}
host1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "e144cd424d5d12fea3d3487de8f382b7df53eb38",
    "dest": "/tmp/system.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "19a2e25348e58637f95072f4edcfb18b",
    "mode": "0755",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 7276,
    "src": "/root/.ansible/tmp/ansible-tmp-1693574676.48-10619-57255450517253/so                                                                              urce",
    "state": "file",
    "uid": 0
}

5. Ansible YUM 模块

  • conf_file:设定远程YUM执行时所依赖的YUM配置文件。
  • disable_gpg_check:安装软件包之前是否检查gpg key。
  • name:需要安装的软件名称,支持软件组安装。
  • update_cache:安装软件前更新缓存。
  • enablerepo:指定repo源名称。
  • skip_broken:跳过异常软件节点。
  • state:软件包状态,包括installed , present ,latest,absent、 removed。
[root@ansible ~]# ansible host4 -m yum -a 'name=nginx state=absent'
host4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "removed": [
            "nginx"
        ]
    },
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror\nResolving Dependencies\n--> Running tran                                                                              saction check\n---> Package nginx.x86_64 1:1.20.1-10.el7 will be erased\n--> Fin                                                                              ished Dependency Resolution\n\nDependencies Resolved\n\n========================                                                                              ========================================================\n Package        Arch                                                                                          Version                    Repository      Size\n=====================                                                                              ===========================================================\nRemoving:\n nginx                                                                                        x86_64          1:1.20.1-10.el7            @epel          1.7 M\n\nTrans                                                                              action Summary\n================================================================                                                                              ================\nRemove  1 Package\n\nInstalled size: 1.7 M\nDownloading packag                                                                              es:\nRunning transaction check\nRunning transaction test\nTransaction test succe                                                                              eded\nRunning transaction\n  Erasing    : 1:nginx-1.20.1-10.el7.x86_64                                                                                                               1/1 \nwarning: /etc/nginx/nginx.conf saved as /etc/nginx/                                                                              nginx.conf.rpmsave\n  Verifying  : 1:nginx-1.20.1-10.el7.x86_64                                                                                                               1/1 \n\nRemoved:\n  nginx.x86_64 1:1.20.1-10.el7                                                                                                                                \n\nComplete!\n"
    ]
}

6. Ansible file 模块

         Ansible file模块主要用于对文件的创建、删除、修改、权限、属性的维护和管理, file模块使用详解如下:

  • src: Ansible端源文件或者目录。
  • follow:支持link 文件复制。
  • force:覆盖远程主机不一致的内容。
  • group:设定远程主机文件夹的组名。
  • mode:指定远程主机文件及文件夹的权限。
  • owner:设定远程主机文件夹的用户名。
  • path:目标路径,也可以用dest,name代替。
  • state:状态包括file,link , directory,hard ,touch ,absent。
  • attributes:文件或者目录特殊属性。

[root@ansible ~]# ansible host1 -m file -a "path=/tmp/abc.txt state=touch mode=755"
host1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/tmp/abc.txt",
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}

 7. Ansible user 模块

        Ansible user模块主要用于操作系统用户、组、权限﹑密码等操作,user模块使用详解如下:

  •  system:默认创建为普通用户,为yes则创建系统用户。
  • append:添加一个新的组。
  • comment:新增描述信息。
  • createhome:给用户创建家目录。
  • force:强制删除用户。
  • group:创建用户主组。
  • groups :将用户加入组或者附属组添加。
  • home:指定用户的家目录。
  • name:表示状态,是否create、remove, modify。
  • password:指定用户的密码,此处为加密密码。
  • remove:删除用户。
  • shell:设置用户的shell 登录环境。
  • uid:设置用户ID。
  • update_password:修改用户密码。
  • state:用户状态,默认为present,表示新建用户。
[root@ansible ~]# ansible host1 -m user -a "name=mas home=/tmp/"
host1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/tmp/",
    "name": "mas",
    "shell": "/bin/bash",
    "state": "present",
    "stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\n",
    "stderr_lines": [
        "useradd: warning: the home directory already exists.",
        "Not copying any file from skel directory into it."
    ],
    "system": false,
    "uid": 1000
}

 8. Ansible cron 模块

        Ansible cron模块主要用于添加﹑删除、更新操作系统crontab任务计划,cron模块使用详解如下:

  • name:任务计划名称。
  • cron_file:替换客户端该用户的任务计划的文件。
  • minute:分(0-59,* ,*/2)。
  • hour:时(0-23,* ,*/2)。
  • day:日(1-31,* ,*/2)。
  • month:月(1-12,* ,*/2)。
  • weekday:周(0-6或1-7,* )。
  • job:任何计划执行的命令,state要等于present。
  • backup:是否备份之前的任务计划。
  • user:新建任务计划的用户。
  • state:指定任务计划present,absent。
[root@ansible ~]# ansible host1 -m cron -a "minute=0 hour=0 day=*  month=* weekday=* name='Ntpdateserver for sync time' job='/usr/sbin/ntpdate ntp.aliyun.com'"
host1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "Ntpdateserver for sync time"
    ]
}

9.  Ansible service 模块

        Ansible service模块主要用于远程客户端各种服务管理,包括启动、停止、重启、重新加载等,service模块使用详解如下:

  • enabled:是否开机启动服务。
  • name:服务名称。
  • runlevel:服务启动级别。
  • arguments:服务命令行参数传递。
  • state:服务操作状态,状态包括started, stopped,restarted、reloaded。
[root@ansible ~]# ansible host3 -m service -a "name=firewalld state=stopped"
host3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "name": "firewalld",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "Fri 2023-09-01 20:57:54 EDT",
        "ActiveEnterTimestampMonotonic": "5622901",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "active",
        "After": "system.slice polkit.service dbus.service basic.target",
        "AllowIsolate": "no",
        "AmbientCapabilities": "0",
        "AssertResult": "yes",
        "AssertTimestamp": "Fri 2023-09-01 20:57:54 EDT",
        "AssertTimestampMonotonic": "5052989",
        "Before": "multi-user.target shutdown.target network-pre.target",
        "BlockIOAccounting": "no",
        "BlockIOWeight": "18446744073709551615",
        "BusName": "org.fedoraproject.FirewallD1",
        "CPUAccounting": "no",
        "CPUQuotaPerSecUSec": "infinity",
        "CPUSchedulingPolicy": "0",
        "CPUSchedulingPriority": "0",
        "CPUSchedulingResetOnFork": "no",
        "CPUShares": "18446744073709551615",
        "CanIsolate": "no",
        "CanReload": "yes",
        "CanStart": "yes",
        "CanStop": "yes",
        "CapabilityBoundingSet": "18446744073709551615",
        "CollectMode": "inactive",
        "ConditionResult": "yes",
        "ConditionTimestamp": "Fri 2023-09-01 20:57:54 EDT",
        "ConditionTimestampMonotonic": "5052989",
        "Conflicts": "iptables.service ebtables.service ip6tables.service ipset.service shutdown.target",
        "ControlGroup": "/system.slice/firewalld.service",
        "ControlPID": "0",
        "DefaultDependencies": "yes",
        "Delegate": "no",
        "Description": "firewalld - dynamic firewall daemon",
        "DevicePolicy": "auto",
        "Documentation": "man:firewalld(1)",
        "EnvironmentFile": "/etc/sysconfig/firewalld (ignore_errors=yes)",
        "ExecMainCode": "0",
        "ExecMainExitTimestampMonotonic": "0",
        "ExecMainPID": "722",
        "ExecMainStartTimestamp": "Fri 2023-09-01 20:57:54 EDT",
        "ExecMainStartTimestampMonotonic": "5054660",
        "ExecMainStatus": "0",
        "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
        "ExecStart": "{ path=/usr/sbin/firewalld ; argv[]=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS ; ignore_errors=no ; start_time=[Fri 2023-09-01 20:57:54 EDT] ; stop_time=[n/a] ; pid=722 ; code=(null) ; status=0/0 }",
        "FailureAction": "none",
        "FileDescriptorStoreMax": "0",
        "FragmentPath": "/usr/lib/systemd/system/firewalld.service",
        "GuessMainPID": "yes",
        "IOScheduling": "0",
        "Id": "firewalld.service",
        "IgnoreOnIsolate": "no",
        "IgnoreOnSnapshot": "no",
        "IgnoreSIGPIPE": "yes",
        "InactiveEnterTimestampMonotonic": "0",
        "InactiveExitTimestamp": "Fri 2023-09-01 20:57:54 EDT",
        "InactiveExitTimestampMonotonic": "5055144",
        "JobTimeoutAction": "none",
        "JobTimeoutUSec": "0",
        "KillMode": "mixed",
        "KillSignal": "15",
        "LimitAS": "18446744073709551615",
        "LimitCORE": "18446744073709551615",
        "LimitCPU": "18446744073709551615",
        "LimitDATA": "18446744073709551615",
        "LimitFSIZE": "18446744073709551615",
        "LimitLOCKS": "18446744073709551615",
        "LimitMEMLOCK": "65536",
        "LimitMSGQUEUE": "819200",
        "LimitNICE": "0",
        "LimitNOFILE": "4096",
        "LimitNPROC": "7183",
        "LimitRSS": "18446744073709551615",
        "LimitRTPRIO": "0",
        "LimitRTTIME": "18446744073709551615",
        "LimitSIGPENDING": "7183",
        "LimitSTACK": "18446744073709551615",
        "LoadState": "loaded",
        "MainPID": "722",
        "MemoryAccounting": "no",
        "MemoryCurrent": "18446744073709551615",
        "MemoryLimit": "18446744073709551615",
        "MountFlags": "0",
        "Names": "firewalld.service",
        "NeedDaemonReload": "no",
        "Nice": "0",
        "NoNewPrivileges": "no",
        "NonBlocking": "no",
        "NotifyAccess": "none",
        "OOMScoreAdjust": "0",
        "OnFailureJobMode": "replace",
        "PermissionsStartOnly": "no",
        "PrivateDevices": "no",
        "PrivateNetwork": "no",
        "PrivateTmp": "no",
        "ProtectHome": "no",
        "ProtectSystem": "no",
        "RefuseManualStart": "no",
        "RefuseManualStop": "no",
        "RemainAfterExit": "no",
        "Requires": "system.slice basic.target",
        "Restart": "no",
        "RestartUSec": "100ms",
        "Result": "success",
        "RootDirectoryStartOnly": "no",
        "RuntimeDirectoryMode": "0755",
        "SameProcessGroup": "no",
        "SecureBits": "0",
        "SendSIGHUP": "no",
        "SendSIGKILL": "yes",
        "Slice": "system.slice",
        "StandardError": "null",
        "StandardInput": "null",
        "StandardOutput": "null",
        "StartLimitAction": "none",
        "StartLimitBurst": "5",
        "StartLimitInterval": "10000000",
        "StartupBlockIOWeight": "18446744073709551615",
        "StartupCPUShares": "18446744073709551615",
        "StatusErrno": "0",
        "StopWhenUnneeded": "no",
        "SubState": "running",
        "SyslogLevelPrefix": "yes",
        "SyslogPriority": "30",
        "SystemCallErrorNumber": "0",
        "TTYReset": "no",
        "TTYVHangup": "no",
        "TTYVTDisallocate": "no",
        "TasksAccounting": "no",
        "TasksCurrent": "18446744073709551615",
        "TasksMax": "18446744073709551615",
        "TimeoutStartUSec": "1min 30s",
        "TimeoutStopUSec": "1min 30s",
        "TimerSlackNSec": "50000",
        "Transient": "no",
        "Type": "dbus",
        "UMask": "0022",
        "UnitFilePreset": "enabled",
        "UnitFileState": "enabled",
        "WantedBy": "multi-user.target",
        "Wants": "network-pre.target",
        "WatchdogTimestamp": "Fri 2023-09-01 20:57:54 EDT",
        "WatchdogTimestampMonotonic": "5622374",
        "WatchdogUSec": "0"
    }
}

 10. Ansible script 模块

        Ansible的script模块允许在远程主机上执行本地脚本或命令。

  • script: 指定要执行的本地脚本文件或命令。可以是绝对路径或相对路径。
  • args: 指定要传递给脚本的参数。可以是一个字符串或一个列表。例如:args: "--arg1 value1 --arg2 value2"
  • chdir: 指定在目标主机上执行脚本时的工作目录。可以是绝对路径或相对路径。
  • creates: 指定一个文件路径,如果该文件存在,则不会执行脚本。这可以用于检查脚本是否已经执行过。
  • removes: 指定一个文件路径,如果该文件存在,则会执行脚本。这可以用于检查脚本是否需要重新执行。
  • executable: 指定要在目标主机上执行的脚本的可执行程序。例如:executable: /bin/bash
  • stdin: 指定一个字符串,作为脚本的标准输入。例如:stdin: "input data"
  • stdin_add_newline: 如果设置为no,则不会在stdin的末尾添加换行符。默认为yes
  • strip_empty_ends: 如果设置为no,则不会从脚本的输出中删除空行。默认为yes
  • warn: 如果设置为no,则不会在脚本执行失败时生成警告。默认为yes
  • become: 如果设置为yes,则以提升的权限(通常是sudo)执行脚本。默认为no
  • become_user: 指定以哪个用户的身份执行脚本,仅在become设置为yes时有效。
  • become_method: 指定使用哪种方法进行身份提升,例如sudosu。默认为sudo
  • environment: 指定一个字典,包含要设置的环境变量。例如:environment: {PATH: "/usr/local/bin:/usr/bin:/bin"}
[root@ansible ~]# ansible host1 -m script -a "hello.sh"
host1 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to host1 closed.\r\n",
    "stderr_lines": [
        "Shared connection to host1 closed."
    ],
    "stdout": "hello\r\n",
    "stdout_lines": [
        "hello"
    ]
}

11.  Ansible fetch 模块

 fetch模块用于从远程主机上复制文件到控制主机上。

  • src(必填):指定要从远程主机复制的文件路径。可以是绝对路径或相对路径。
  • dest(必填):指定要将文件复制到的本地目录路径。可以是绝对路径或相对路径。
  • flat(可选):如果设置为yes,则将所有文件复制到dest目录中,而不会保留原始的目录结构。默认为no,即保留原始的目录结构。
  • validate_checksum(可选):如果设置为yes,则在复制文件之前验证远程文件的校验和。默认为no
  • checksum_algorithm(可选):指定用于校验和计算的算法。默认为md5
  • remote_src(可选):如果设置为yes,则将src参数解释为远程主机上的路径,而不是控制主机上的路径。默认为no
  • flat_suffix(可选):如果设置了flat参数,并且复制的文件名在目标目录中已存在,则使用此后缀进行重命名。默认为空字符串。
  • fail_on_missing(可选):如果设置为yes,则在远程主机上的文件不存在时,任务将失败。默认为yes
  • follow(可选):如果设置为yes,则跟随符号链接。默认为no
  • get_checksum(可选):如果设置为yes,则在复制文件之后返回文件的校验和。默认为no
  • get_md5(可选):如果设置为yes,则在复制文件之后返回文件的MD5校验和。默认为no
  • get_sha1(可选):如果设置为yes,则在复制文件之后返回文件的SHA1校验和。默认为no
  • get_sha256(可选):如果设置为yes,则在复制文件之后返回文件的SHA256校验和。默认为no

[root@ansible ~]# ansible host1 -m fetch -a "src=/tmp/a.txt dest=/root/"
host1 | CHANGED => {
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/host1/tmp/a.txt",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "remote_md5sum": null
}

12. Ansible unarchive 模块

        Ansible的unarchive模块用于解压缩文件。它支持解压缩多种压缩格式,包括tar、gzip、bzip2、zip和rar等。

  • src(必填):指定要解压缩的文件路径。可以是远程主机上的路径或控制主机上的路径。
  • dest(必填):指定解压缩后的文件存放路径。可以是远程主机上的路径或控制主机上的路径。
  • remote_src(可选):如果设置为yes,则将src参数解释为远程主机上的路径,而不是控制主机上的路径。默认为no
  • format(可选):指定要解压缩的文件格式。可选值包括targzipbzip2ziprar。如果不指定该参数,Ansible会自动根据文件扩展名来确定格式。
  • creates(可选):指定一个文件路径,如果该文件已经存在,则不执行解压缩操作。
  • extra_opts(可选):指定额外的解压缩选项。可根据不同的格式提供不同的选项。
  • remote_src(可选):如果设置为yes,则将src参数解释为远程主机上的路径,而不是控制主机上的路径。默认为no
[root@ansible ~]# ansible host1 -m unarchive -a 'src=/root/1.tar dest=/tmp/'
host1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/tmp/",
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar",
            "--extract",
            "-C",
            "/tmp/",
            "-f",
            "/root/.ansible/tmp/ansible-tmp-1693621741.48-31359-276269799795958/source"
        ],
        "err": "",
        "out": "",
        "rc": 0
    },
    "gid": 0,
    "group": "root",
    "handler": "TarArchive",
    "mode": "01777",
    "owner": "root",
    "secontext": "system_u:object_r:tmp_t:s0",
    "size": 4096,
    "src": "/root/.ansible/tmp/ansible-tmp-1693621741.48-31359-276269799795958/source",
    "state": "directory",
    "uid": 0
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值