即使在使用本地连接运行剧本时delegate_to设置为remote,Ansible也会在控制机中创建目录



我正在运行一个可解析的剧本,该剧本被配置为提供ec2并配置机器。我将剧本的连接设置为本地,因为在脚本运行之前没有要管理的机器。一旦配置好,我就应该在远程服务器中创建目录。由于剧本在本地连接中运行,我将其设置为delete_to:{{remote_host}},因此此目录创建在远程主机中执行,但它仍然在控制机中创建目录。

- name: provision instance for Apache
hosts: localhost
connection: local
remote_user: ubuntu
gather_facts: false
vars_files:
- vars/env.yml
vars:
allow_world_readable_tmpfiles: true
key_name: ansible-test
region: us-east-2
image: ami-0e82959d4ed12de3f # Ubuntu 18.04
id: "practice-akash-ajay"
sec_group: "{{ id }}-sec"
remote_host: ansible-test
remaining_days: 20
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory
# acme_directory: https://acme-v02.api.letsencrypt.org/directory
cert_name: "{{ app_slug }}.{{ app_domain}}"
intermediate_path: /etc/pki/letsencrypt/intermediate.pem
cert:
common_name: "{{ app_slug }}.{{ app_domain}}"
organization_name: PearlThoughts
email_address: "{{ letsencrypt_email }}"
subject_alt_name:
- "DNS:{{ app_slug }}.{{ app_domain}}"
roles:
- aws
- name: Create certificate storage directory
file:
dest: "{{item.path}}"
mode: 0750
state: directory
delegate_to: {{ remote_host }}
with_items:
- path: ~/lets-seng-test

当您在播放中明确设置connection时,它将用于该播放中的所有任务。所以不要那样做。Ansible默认情况下会为localhost使用local连接,除非您在库存中明确更改了该连接(同样,不要这样做(。

如果你在游戏中删除connection设置,delegate_to可能会按照你的预期工作。。。但我认为你不想那样做。

如果你的剧本为你准备了一个新主机,那么用Ansible瞄准该主机的方法是与该主机(或其对应的组(进行新的播放,该主机列在播放的目标hosts:中。从概念上讲,你想要:

- host: localhost
tasks:
- name: provisiong an AWS instance
aws_ec2: [...]
register: hostinfo
- add_host:
name: myhost
ansible_host: "{{ hostinfo... }}"
- hosts: myhost
tasks:
- name: do something on the new host
command: uptime

在配置主机和对其执行任务之间,您可能需要一些逻辑,以确保它已启动并准备好为请求提供服务。

与其使用add_host模块,更好的解决方案通常是依赖适当的库存插件。

最新更新