Score:0

using logical operator && to validate condition in Ansible

in flag

i would create a playbook that Check configured filesystems for TSM configuration I need to echo "${FS_TSM[@]}" from register: FS_TO_ADD and to conclude it, a conditional must be set to debug the FS_TSM [[ ! -z "${FS_TO_ADD}" ]] && { FS_TSM+=( "${FS_TO_ADD}" } . I think When: statement1 than statement2 is the good but i don't know how to set it to achieve it


  - hosts: all
    vars:
      whitelist:
        - '/bin'
        - '/boot'
      FS_TSM:
        - '/'

    tasks:
      - set_fact:
          mount_point: "{{ansible_facts.mounts | selectattr('fstype', 'match', '^xf+') | map(attribute='mount')}}"
      - debug:
          var: mount_point

        loop: "{{ whitelist }}"
        when: item in mount_point
        register: FS_TO_ADD

the final result of the playbook is to get this output:

/
/boot 
/home
/opt  
/var 
/var
/opt 
/var/tmp
/var/log
/var/log/audit
Score:0
in flag

@KrisVandenbergh your suggestion is really helpful especially with

intersect(whitelist) | union(FS_TSM) It worked well. Meanwhile i modified my playbook and it gave me what i am looking for:

- set_fact:

mount_point: "{{ansible_facts.mounts | selectattr('fstype', 'match', '^ext+') | map(attribute='mount') | list }}"

vars:

query: "[?mount==whitelist].mount "

- debug:

var: mount_point
Score:0
pe flag

Not sure what you're getting at, in any case, maybe some inspiration:

- name: Test
  hosts: all
  vars:
    whitelist:
      - /bin
      - /boot
    FS_TSM:
      - /

  tasks:

  - name: Debug
    debug:
      msg: "{{ ansible_facts.mounts | selectattr('fstype', 'match', '^xf+') | map(attribute='mount') | intersect(whitelist) | union(FS_TSM) | list }}"

on my system yields:

ASK [Debug] ***********************************************************************************************************************************************************************************************
ok: [server] => {
    "msg": [
        "/boot",
        "/"
    ]
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.