我需要运行任务,该任务将检查并创建Ansible控制节点(运行ansible-playbook
命令的地方(上是否缺少文件夹-接下来的任务将分别将一些指定的文件复制到这些本地子文件夹:
我有一项任务:
tasks:
- name: Create local directory
file:
path: "remotes/{{ inventory_hostname }}"
state: directory
recurse: yes
delegate_to: localhost
tags:
localfolders
然而当我用CCD_ 2运行时;改变";(创建文件夹(在每个远程:
TASK [Create local directory] ****************************************************************************************************************
changed: [ansible -> localhost]
changed: [remote1 -> localhost]
changed: [remote2 -> localhost]
为什么它不只在本地运行任务?
预期的结果是,在易受攻击的主机上(仅(,创建了以下文件夹:
remotes/ansible
remotes/remote1
remotes/remote2
为了更好地理解控制任务运行的位置:委派和本地操作以及run_once
的工作原理,我准备了一个带有的inventory
文件的小测试
[test]
remote01.example.com
remote02.example.com
和剧本local.yml
---
- hosts: test
become: false
gather_facts: false
tasks:
- name: Check where I am running on
delegate_to: localhost
shell:
cmd: "hostname && hostname -i"
register: result
run_once: true
- name: Show result
debug:
msg: "{{ result.stdout_lines }}"
run_once: false
通过在control.example.com
节点上执行
sshpass -p ${PASSWORD} ansible-playbook --user ${ACCOUNT} --ask-pass local.yml
导致的输出
PLAY [test] ********************************
TASK [Check where I am running on] *********
changed: [remote01.example.com -> localhost]
TASK [Show result] *************************
ok: [remote01.example.com] =>
msg:
- control.example.com
- 192.0.2.1
ok: [remote02.example.com] =>
msg:
- control.example.com
- 192.0.2.1
PLAY RECAP *************************************************************************************************
remote01.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
remote02.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
有趣的文档
- 保留用于文档的IPv4地址块