0

I have a simple Ansible role which creates a Foreman activation key, however it only "subscriptions" with the last value and not both?

# cat roles/hammer/tasks/subscription.yml
---

- name: Add Subscription key ID to Activation Key
  katello_activation_key:
    username: "{{ hammer.username|default('NotSet') }}"
    password: "{{ hammer.password|default('NotSet') }}"
    server_url: "https://{{ system_host_name }}"
    name: "{{ item.0.name }}"
    organization: "{{ hammer.organisation.name }}"
    lifecycle_environment: "{{ item.0.lifecycle }}"
    content_view: '{{ item.0.cview }}'
    subscriptions:
      - name: "{{ item.1 }}"
  #   - name: "{{ item.1.name2 }}"
    auto_attach: False
    release_version: Initial
  tags: hammer
...

My vars file contains:

act_key:
  - name: CentOS 7 content Development Key
    desc: CentOS 7 content Development Key
    release: Initial
    cview: CentOS 7 content
    lifecycle: Development
  #  subscription: ['CentOS-7','CentOS-7-EPEL']
    subscription:
      - CentOS-7
      - CentOS-7-EPEL

  - name: CentOS 7 content Production Key
    desc: CentOS 7 content Production Key
    cview: CentOS 7 content
    release: Initial
    lifecycle: Production
  #  subscription: ['CentOS-7','CentOS-7-EPEL']
    subscription:
      - CentOS-7
      - CentOS-7-EPEL

  - name: CentOS 8 content Development Key
    desc: CentOS 8 content Development Key
    cview: CentOS 8 content
    release: Initial
    lifecycle: Development
  #  subscription: ['CentOS-8','CentOS-8-EPEL']
    subscription:
      - CentOS-8
      - CentOS-8-EPEL

  - name: CentOS 8 content Production Key
    desc: CentOS 8 content Production Key
    cview: CentOS 8 content  
    release: Initial
    lifecycle: Production
  #  subscription: ['CentOS-8','CentOS-8-EPEL']
    subscription:
      - CentOS-8
      - CentOS-8-EPEL

I am trying to to add " subscription: ['CentOS-7','CentOS-7-EPEL']" in a single pass as if I use the "with_subelements" loop only the last item "CentOS-7-EPEL" or "CentOS-8-EPEL" is added (and not both).

Can anyone suggest a way to change my variables file or play to make the katello_activation_key add both variables in a single pass?

1
  • You loop over something in your task, could you please add that loop to see, what you mean with item.0 and item.1, etc.
    – TRW
    Feb 15 at 8:19
0

First the list of dictionaries in act_key contains a key subscription and that value is a list. So you don't need to iterate over multiple lists

- name: Add Subscription key ID to Activation Key
  katello_activation_key:
    ..
    name: "{{ item.name }}"
    organization: "{{ hammer.organisation.name }}"
    lifecycle_environment: "{{ item.lifecycle }}"
    content_view: '{{ item.cview }}'
    subscriptions: "{{ item.subscription }}"
    ...
  loop: "{{ act_key }}"

Second - the katello_activation_key.subscriptions doesn't allow a string list (but also a list of dict with a single key name) - so you can use dict or items2dict to create a map/dict of the list of the names. But I would prefer to change the act_key for better reading to contain the same format in subscription, then ´katello_activation_key` needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.