ansiblednsredhat

ansible.posix.mount fails to resolve DNS when using variables


I have a test playbook to mount different types of mounts. It works when I don't use variables for the values, however fails DNS resolution when I do. I'm unsure why this is:

Playbook:

---
- hosts: all
  become: true
  gather_facts: yes
  vars:
    shares:
      - environments:
        - environment: test 
          mounts:
             - dest: '/mnt/cifs/share2'
               src: //windows.example.com/share2
               opts: 'x-systemd.automount,rw,vers=2.1,sec=krb5i,user=test@EXAMPLE.COM,uid=test,gid=test,file_mode=0770,dir_mode=0770,rsize=32768,wsize=32768'
               type: 'cifs'
             - dest: /mnt/efs/share2
               src: fs-0e7af4f4f58ff4dcd.efs.us-east-2.amazonaws.com
               type: efs
               opts: 'x-systemd.automount,_netdev,tls,accesspoint=fsap-0395b37eaf8cbbf9e'
  tasks:
    - name: Mount CIFS Share
      ansible.posix.mount:
        src: fs-0e7af4f4f58ff4dcd.efs.us-east-2.amazonaws.com 
        path: /mnt/efs/share1
        fstype: efs 
        opts: 'x-systemd.automount,_netdev,tls,accesspoint=fsap-e9fbbc8ae74b5930'
        state: mounted

    - name: Mount EFS Share 
      ansible.posix.mount:
        src: //windows.example.com/share1
        path: /mnt/cifs/share1
        fstype: cifs
        opts: 'x-systemd.automount,rw,vers=2.1,sec=krb5i,user=test@EXAMPLE.COM,uid=test,gid=test,file_mode=0770,dir_mode=0770,rsize=32768,wsize=32768'
        state: mounted

    - name: Mount Shares
      ansible.posix.mount:
        src: |-
          {%- if item.1.type == "cifs" -%}
          \\ "{{item.1.src}}"
          {%- else -%}
          "{{item.1.src}}"
          {%- endif -%}
        path: "{{item.1.dest}}"
        fstype: "{{item.1.type}}"  
        opts: "{{item.1.opts}}"  
        state: mounted
      with_subelements:
        - "{{ shares | map(attribute='environments') | list | sum(start=[]) }}"
        - mounts
      when: item.0.environment == 'test'
...

Result

[mose@lab ~/ansible]$ ansible-playbook -Ki hosts test-mounts.yml 
BECOME password: 

PLAY [all] ***********************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [server.example.com]

TASK [Mount CIFS Share] **********************************************************************************************************************************************************************************************************************
ok: [server.example.com]

TASK [Mount EFS Share] ***********************************************************************************************************************************************************************************************************************
ok: [server.example.com]

TASK [Mount Shares] **************************************************************************************************************************************************************************************************************************
failed: [server.example.com] (item=[{'environment': 'test'}, {'dest': '/mnt/cifs/share2', 'src': 'server.example.com/mnt/cifs/share2', 'opts': 'x-systemd.automount,rw,vers=2.1,sec=krb5i,user=test@EXAMPLE.COM,uid=test,gid=test,file_mode=0770,dir_mode=0770,rsize=32768,wsize=32768', 'type': 'cifs'}]) => {"ansible_loop_var": "item", "changed": false, "item": [{"environment": "test"}, {"dest": "/mnt/cifs/share2", "opts": "x-systemd.automount,rw,vers=2.1,sec=krb5i,user=test@EXAMPLE.COM,uid=test,gid=test,file_mode=0770,dir_mode=0770,rsize=32768,wsize=32768", "src": "server.example.com/mnt/cifs/share2", "type": "cifs"}], "msg": "Error mounting /mnt/cifs/share2: mount error: could not resolve address for  \"server.example.com: Unknown error\n"}
failed: [server.example.com] (item=[{'environment': 'test'}, {'dest': '/mnt/efs/share2', 'src': 'fs-0e7af4f4f58ff4dcd.efs.us-east-2.amazonaws.com', 'type': 'efs', 'opts': 'x-systemd.automount,_netdev,tls,accesspoint=fsap-e9fbbc8fae73b5930'}]) => {"ansible_loop_var": "item", "changed": false, "item": [{"environment": "test"}, {"dest": "/mnt/efs/share2", "opts": "x-systemd.automount,_netdev,tls,accesspoint=fsap-e9fbbc8fae73b5930", "src": "fs-0e7af4f4f58ff4dcd.efs.us-east-2.amazonaws.com", "type": "efs"}], "msg": "Error mounting /mnt/efs/share2: Failed to resolve \"\"fs-0e7af4f4f58ff4dcd.efs.us-east-2.amazonaws.com\"\" - check that the specified DNS name is a CNAME record resolving to a valid EFS DNS name\n"}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
server.example.com   : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Note, content is sanitized; in my actual example the values for each of the NFS/CIFS shares are precisely the same. Both client and server are running the same OS/ansible version:

[mose@lab ~/ansible]$ ansible --version
ansible [core 2.14.14]
  config file = /home/mose/ansible/ansible.cfg
  configured module search path = ['/home/mose/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /home/mose/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.18 (main, Jan 24 2024, 00:00:00) [GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
[mose@lab ~/ansible]$ cat /etc/redhat-release 
Red Hat Enterprise Linux release 9.4 (Plow)

Solution

  • I found the issue, the variable being encapsulated by quotes "'s was the culprit. I changed:

    "{{item.1.src}}"
    

    to:

    {{item.1.src}}
    

    And mounts started working. Ironically, I recall getting a syntax error stating that former should be used.