在不同文件中跨角色使用 Ansible 变量

Using Ansible variable across roles in different files

我在这里尝试执行下面列出的活动

  1. 从 Application Load Balancer 注销实例
  2. 部署应用程序
  3. 在 Application Load Balancer 上重新注册相同的实例

用例是将已取消注册的实例添加回其删除位置的相同 ALB 目标组。在取消注册实例时,我将目标组 ARN 保存在 {{ my_target_arn }} 中,稍后以不同的角色使用它以将其注册回目标组中的同一实例。

下面是从 ALB 注销实例的代码:

---
- name: Get EC2 instance ID
  ec2_instance_info:
    filters:
      private-ip-address: "{{ ansible_host }}"
  register: ec2_details

- name: Get list of target groups with the EC2
  delegate_to: localhost
  elb_target_info:
    instance_id: "{{ ec2_details.instances[0].instance_id }}"
  register: target_info

- name: Deregister the EC2
  delegate_to: localhost
  elb_target:
    target_group_arn: "{{ item.target_group_arn }}"
    target_id: "{{ ec2_details.instances[0].instance_id }}"
    deregister_unused: yes
    target_status_timeout: 10
    state: absent
  with_items: "{{ target_info.instance_target_groups }}"
  register: detached_details
  when: target_info != ""

- set_fact:
    my_target_arn: "{{ item.target_group_arn }}"
  with_items: "{{ target_info.instance_target_groups }}"
  when: target_info != ""

下面列出了添加回实例的代码:

---
- name: Get EC2 instance ID
  ec2_instance_info:
    filters:
      private-ip-address: "{{ ansible_host }}"
  register: ec2_details

- name: Register the EC2
  delegate_to: localhost
  elb_target:
    target_group_arn: "{{ item }}"
    target_id: "{{ ec2_details.instances[0].instance_id }}"
    deregister_unused: yes
    target_status_timeout: 10
    state: present
  with_items: "{{ my_target_arn }}"
  register: attached_details
  when: my_target_arn != ""

剧本代码:

---
- name: Check EC2 details
  hosts: appserver

  roles:
    - roles/ec2_deregister
    - roles/deploy_application
    - roles/ec2_register

这里的问题是,我的 {{ ansible_host }} 与多个目标组相关联。因此,在注销实例时,它会从这些多个目标组中注销实例。但是,在重新注册实例时,它只会将其添加回单个目标组(它获得的第一个目标组)。

所以基本上我想 set_fact 有一个可能有也可能没有多个值的列表变量(它可以是单个值或值列表)。请建议如何 set_fact 使用列表变量。

您使用的是什么版本的ansible?

我尝试在一个角色中设置一个事实并将其传递给另一个角色,似乎效果很好。

我正在使用 ansible 2.9.6

角色 1:

- set_fact:
    my_fact: test

角色 2:

- debug:
    var: my_fact

剧本:

---
- hosts: localhost
  gather_facts: no
  become: no
  roles:
    - ./roles/role1
    - ./roles/role2

和输出:

TASK [./roles/role1 : set_fact] 
ok: [localhost]

TASK [./roles/role2 : debug] 
ok: [localhost] => {
    "my_fact": "test1234"
}

能够使用下面提到的代码获取列表

注销实例

---
- name: Get EC2 instance ID
  ec2_instance_info:
    filters:
      private-ip-address: "{{ ansible_host }}"
  register: ec2_details

- name: Get list of target groups with the EC2
  delegate_to: localhost
  elb_target_info:
    instance_id: "{{ ec2_details.instances[0].instance_id }}"
  register: target_info

- set_fact:
    my_target_arn: "{{ my_target_arn|default([]) + [{'target_group_arn': item}] }}"
  loop: "{{ target_info.instance_target_groups|map(attribute='target_group_arn')|list }}"
  when: target_info != ""

- name: Deregister the EC2
  delegate_to: localhost
  elb_target:
    target_group_arn: "{{ item.target_group_arn }}"
    target_id: "{{ ec2_details.instances[0].instance_id }}"
    deregister_unused: yes
    target_status_timeout: 10
    state: absent
  with_items: "{{ target_info.instance_target_groups }}"
  register: detached_details
  when: target_info != ""

注册回同一个实例

---
- name: Get EC2 instance ID
  ec2_instance_info:
    filters:
      private-ip-address: "{{ ansible_host }}"
  register: ec2_details
  when: my_target_arn is defined

- name: Register the EC2
  delegate_to: localhost
  elb_target:
    target_group_arn: "{{ item.target_group_arn }}"
    target_id: "{{ ec2_details.instances[0].instance_id }}"
    deregister_unused: yes
    target_status_timeout: 10
    state: present
  with_items: "{{ my_target_arn }}"
  register: attached_details
  when: my_target_arn is defined