【问题标题】:Using Ansible variable across roles in different files在不同文件中跨角色使用 Ansible 变量
【发布时间】:2020-05-10 08:48:59
【问题描述】:

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

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

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

下面是从 ALB 注销 Instance 的代码:

---
- 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-role


    【解决方案1】:

    你用的是什么版本的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"
    }
    

    【讨论】:

    • Ansible 版本 ansible 2.9.6 我能够以另一个角色访问该事实。但是,变量也可以是值列表。帮助我将列表值传递给另一个角色。我已经编辑了问题。
    【解决方案2】:

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

    注销实例

    ---
    - 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
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 2021-04-05
      • 1970-01-01
      • 2015-07-31
      • 2017-01-29
      • 2020-11-02
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      相关资源
      最近更新 更多