ansible将playbook在本机执行做测试

- 服务器

当我们想要使用 ansible 做一些验证操作时,为了避免外部环境的影响。最方便快捷的方式就是直接在 ansible 本机执行,马上就能验证下效果。

[root@imzcy ansible]# cat local.yaml
---
- name: play1
  hosts: 127.0.0.1
  connection: local
  gather_facts: no
  tasks:
    - block:
      - name: hostname
        shell: hostname
        register: result

    - debug: msg="{{ result }}"
[root@imzcy ansible]#


执行效果如下所示

[root@imzcy ansible]# ansible-playbook ./local.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [play1] ********************************************************************************************************************************************************************************************************************************

TASK [hostname] *****************************************************************************************************************************************************************************************************************************
changed: [127.0.0.1]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": {
        "changed": true,
        "cmd": "hostname",
        "delta": "0:00:00.023943",
        "end": "2023-11-22 22:33:21.175224",
        "failed": false,
        "rc": 0,
        "start": "2023-11-22 22:33:21.151281",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "imzcy",
        "stdout_lines": [
            "imzcy"
        ]
    }
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@imzcy ansible]#