Conflicting action statements: set_fact, add_host

Hello

I retrieve all hosts from the monitoring system to var host_list.
Than I loop through host_list and want to build an inventory with add_host: by passing through some filters. Finally, the build inventory will be taken to install software. But, I receive the following error:

Vault password: 

PLAY [Update all CMK agents] ***************************************************

TASK [get host list from CMK] **************************************************
ERROR! conflicting action statements: set_fact, add_host

The error appears to be in '/runner/project/import_cmk_get_list.yaml': line 67, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  # ############################################# WindowsServerDomain ###################################
  - name: Add host to group 'WindowsServerDomain'
    ^ here

I want to use set_fact: to the the var Kunde for each item of the loop for a later use to the the appropriate credentials. The playbook is running in AWX.
I have no idea why this happens. I googled a long time but find no solution.
Hope someone can give me hint or the solution.
Following the piece of the playbook around the error.

---
  # ################################################################################
  # create list of selected hosts and build inventory
  # ################################################################################
  - name: get host list from CMK
    uri:
      url: "{{ cmk_rest_api_url }}/domain-types/host_config/collections/all?effective_attributes=true"
      method: GET
      headers:
         Authorization: "Bearer {{ cmk_user }} {{ cmk_secret }}"
         Accept: 'application/json'
      force_basic_auth: yes
      status_code: 200,404
    register: host_list

  # ############################################# Linux ###################################
  - name: Add host to group 'Linux'
    with_items: "{{ host_list.json.value }}" 
    loop_control:
      label: "Name: {{ item.id }},
              OS: {{ item.extensions.effective_attributes.tag_Betriebssystem | default('-') }},
              State: {{ item.extensions.effective_attributes.tag_criticality }}"
    when: 
      - do_Linux == 'Yes'
      - item.extensions.effective_attributes.tag_Betriebssystem | default('-') == 'Linux' 
      - item.extensions.effective_attributes.tag_AgentType == 'Agent' 
      - item.extensions.effective_attributes.tag_Systemtyp != 'IoT' 
      - ( (item.extensions.effective_attributes.tag_criticality == 'test' and crit_Test == 'Yes') 
         or (item.extensions.effective_attributes.tag_criticality == 'prod' and crit_Prod == 'Yes') 
         or (item.extensions.effective_attributes.tag_criticality == 'critical' and crit_BusinessCrit == 'Yes')
        )
      - ( (item.extensions.effective_attributes.tag_Kunde == 'inseo' and cust_INSEO == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'cdhw' and cust_DHW == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'ceizo' and cust_EIZO == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'ckru' and cust_KRUCH == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'cDK' and cust_DONAUK == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'cted' and cust_TEDALOS == 'Yes') 
        )
    add_host:
      hostname: '{{ item.id }}'
      groups: Linux
      ansible_host: '{{ item.extensions.attributes.ipaddress }}'
      ansible_port: 22
      become: yes
      become_method: sudo
        #ansible_ssh_user: "{{ inseo_ansible_user }}"
        #ansible_ssh_password: "{{ inseo_ansible_pw }}"
      tag_criticality: '{{ item.extensions.effective_attributes.tag_criticality }}'

  # ############################################# WindowsServerDomain ###################################
  - name: Add host to group 'WindowsServerDomain' 
    with_items: "{{ host_list.json.value }}" 
    loop_control:
      label: "Name: {{ item.id }},
              OS: {{ item.extensions.effective_attributes.tag_Betriebssystem | default('-') }},
              State: {{ item.extensions.effective_attributes.tag_criticality }}"
    when: 
      - do_WindowsServer == 'Yes'
      - item.extensions.effective_attributes.tag_Betriebssystem | default('-') == 'WindowsServer'
      - item.extensions.effective_attributes.tag_AgentType == 'Agent' 
      - item.extensions.effective_attributes.tag_Systemtyp != 'IoT' 
      - ( (item.extensions.effective_attributes.tag_criticality == 'test' and crit_Test == 'Yes') 
         or (item.extensions.effective_attributes.tag_criticality == 'prod' and crit_Prod == 'Yes') 
         or (item.extensions.effective_attributes.tag_criticality == 'critical' and crit_BusinessCrit == 'Yes')
        )
      - ( (item.extensions.effective_attributes.tag_Kunde == 'inseo' and cust_INSEO == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'cdhw' and cust_DHW == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'ceizo' and cust_EIZO == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'ckru' and cust_KRUCH == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'cDK' and cust_DONAUK == 'Yes') 
         or (item.extensions.effective_attributes.tag_Kunde == 'cted' and cust_TEDALOS == 'Yes') 
        )
        #- item.extensions.effective_attributes.tag_ansible_login == 'Domain'
    set_fact:
      Kunde: "{{ item.extensions.effective_attributes.tag_Kunde }}"
    add_host:
      hostname: '{{ item.id }}'
      groups: WindowsServerDomain
      ansible_host: '{{ item.extensions.attributes.ipaddress }}'
      ansible_connection: winrm
      ansible_port: 5985
      ansible_winrm_transport: kerberos
      ansible_user: "{{ cred[Kunde]['user'] }}"
      ansible_password: "{{ cred[Kunde]['pw'] }}"
      tag_criticality: '{{ item.extensions.effective_attributes.tag_criticality }}'

regards Robi

This is improper. You can’t call set_facts and add_host in the same task.

P.S. You probably want to use vars: instead of set_facts here.

1 Like