Link to home
Start Free TrialLog in
Avatar of Leo
LeoFlag for United States of America

asked on

Ansible Playbook mount CIFS, copy and run shell script

Hello, 


I am having some issues with the following playbook that runs in linux. I've added a part to mount a cifs share, copy the script and the run it locally. 


Getting the following error: 


JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
mapping values are not allowed in this context
The error appears to be in '/home/ansible/ubuntus.yml': line 20, column 15, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Mount network share
mount:
^ here  


--- 
- hosts: all
  become: true
  vars: 
    uri: "//auto01.nal.com/auto_hq_ds01/v/Tan/Ub/"
    username: "lxx@al.com"
    password: "XXX"
    mountpoint: "/share"
  tasks:
     - name: Join the machine if it is not already on the domain.
       become: yes 
       command: /opt/pbis/bin/domainjoin-cli join --ou OU=bolt,DC=ev,DC=nal,DC=com ev.nal.com jdom XXX
       no_log: True
     - name: Allow {{ reqUser }} to sudo, and validate the sudoers file before saving
      #- set_fact: reqUser="{{ lookup('env','requestedBy') }}"
       ansible.builtin.lineinfile:
           path: "/etc/sudoers.d/90-cloud-init-users"
           state: present
           mode: 0440
           create: yes
           regexp: '^corp\\{{ reqUser }}'
           line: 'orp\\{{ reqUser }} ALL=(ALL) NOPASSWD: ALL'
           validate: /usr/sbin/visudo -cf %s
     - name: utility present
       ansible.builtin.package:
         name: cifs-utils
         state: present
     - name: Mount network share
       ansible.posix.mount:
         src: "{{ uri }}"
         path: "{{ mountpoint }}"
         fstype: cifs
         opts: 'username={{ username }},password={{ password }}'
         state: mounted
     - name: Copy data
       copy:
           src: //auto01.nal.com/auto_hq_ds01/v/Tan/Ub/install_tanium_ubuntu.sh
           dest: /opt/ mode=0755
           remote_src: yes
     - name: "Execute the script"
       command: bash /opt/install_t_ubuntu.sh
       register: version

Open in new window

Can someone please help? 

Thank you 


ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Leo

ASKER

Thanks Simon! I am testing now…

Avatar of Leo

ASKER

Hi Simon, 


This is the latest revision. 


The error appears to be in '/home/ansible/ubuntuscript.yml': line 19, column 15, but may
be elsewhere in the file depending on the exact syntax problem. 


The line reference it's a little different in Visual Studio Code than here. The error complains about line 19 which is 


User generated imageUser generated image


Any thoughts on how to should I define this? 


Thank you so much! 


--- 

- hosts: all

  become: true

  tasks:

     - name: Join the machine if it is not already on the domain.

       become: yes 

       command: /opt/pbis/bin/domainjoin-cli join --ou OU=bolt,DC=ev,DC=nal,DC=com ev.nal.com dom xxxx

       no_log: True

     - name: Allow {{ reqUser }} to sudo, and validate the sudoers file before saving

       ansible.builtin.lineinfile:

           path: "/etc/sudoers.d/90-cloud-init-users"

           state: present

           mode: "0440"

           create: yes

           regexp: '^corp\\{{ reqUser }}'

           line: 'corp\\{{ reqUser }} ALL=(ALL) NOPASSWD: ALL'

           validate: /usr/sbin/visudo -cf %s

     - name: Mount network share

         mount:

         src: //auto_hq00cifs_ds01.orp.nal.com/auto_cifs_ds01/8/um/tu/

         path: /mnt

         fstype: cifs

         opts: 'username=admin@corp.nal.com,password=XXX'

         state: mounted

         become: true

     - name: Copy data

        copy:

           src: //auto_hq00cifs_ds01.orp.nal.com/auto_cifs_ds01/8/um/tu/install_tanium_ubuntu.sh

           dest: /opt/ 

           mode: "0755"

           remote_src: yes

     - name: "Execute the script"

       command: bash /opt/install_tanium_ubuntu.sh

       register: version




The "mount:" on line 19 needs to be indented the same as the "name:" on 18 (i.e. the "m" immediately below the "n"). The "src:", "path:" etc on the following lines are the parameters of the "mount:" so should be left where they are, indented 2 spaces more than the repositioned "mount:".

I'm doing this on my phone on a walk, so may be misreading it!
Avatar of Leo

ASKER

Thanks Simon! I made the change and testing now.

Avatar of Leo

ASKER

That seemed to have worked, however the playbook errors out with 


TASK [Mount network share] *****************************************************,fatal: [tan3]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (mount) module: become. Supported parameters include: src, path (name), dump, passno, fstab, boot, fstype, state, backup, opts."},,PLAY RECAP *********************************************************************,tan3 : ok=3 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0


Sorry, missed that. "become:" is a feature of the task itself, so must be at the same indentation as "name:" and "mount:".
Avatar of Leo

ASKER

Ok, "become" it's now indented this way... testing now. 


- name: Mount network share
  mount:
         src: //cifs_ds01.orp.nal.com/auto_cifs_ds01/8/ium/tu/
         path: /mnt
         fstype: cifs
         opts: 'username=dmin@op.nal.com,password=XXX'
         state: mounted
  become: true

Open in new window

Thanks!


Avatar of Leo

ASKER

Looks like I am still getting the error:


TASK [Mount network share] *****************************************************,fatal: [tan6]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (mount) module: become. Supported parameters include: src, path (name), dump, passno, fstab, boot, fstype, state, backup, opts."},,PLAY RECAP *********************************************************************,tan6 : ok=3 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

That's odd. Are you sure you saved the updated version? Do you have any tabs in the files (though Ansible would usually tell you if you'd mixed tabs and spaces)?
Avatar of Leo

ASKER

Let me double check the modify date for the yaml file. 

Avatar of Leo

ASKER

Ok, so the time stamp was old looks like the VPN timed out and it was not updated. 

With the latest changes I am getting the following:


TASK [Mount network share] *****************************************************,fatal: [tan7]: FAILED! => {"changed": false, "msg": "Error mounting /mnt: mount: /mnt: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.\n"},,PLAY RECAP *********************************************************************,tan7 : ok=3 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0 

Open in new window


Avatar of Leo

ASKER

Looks like I might need to install CIFS-utils first?

 

sudo apt install cifs-utils

Open in new window

Avatar of Leo

ASKER

Maybe adding something like this?


tasks:

     - name: Execute command using the shell module 

       become: true 

       become_user: root

       shell: sudo apt install cifs-utils 


Yes, that looks like it is needed. Often a good idea to try a step manually first, before you automate it!
You have already become root, so no need for the sudo, but you do need a "-y"
     apt install -y cifs-utils
Avatar of Leo

ASKER

Ok, thanks, testing now...

Better would be to use the "package" module:
- name: Install cifs-utils to get the mount.cifs program
  package:
    name: cifs-utils
    state: present

Open in new window

Avatar of Leo

ASKER

That sounds much better, I've updated the play with that, fingers crossed! 

Also, as a bit of tidy-up, you have "become: true" at the top level, so don't need to add it to the individual tasks, and the default "become_user" is root so you don't have to specify that.
Avatar of Leo

ASKER

Thanks so much for pointing and explaining that out! 

Avatar of Leo

ASKER

This is weird, getting the following:


Unable to validate syntax for Playbook(s). Failed to execute script on host hq00ansvrap01. Error: [WARNING]: While constructing a mapping from /home/ansible/ubuntuscript.yml,
line 5, column 8, found a duplicate dict key (name). Using last defined value
only.
ERROR! conflicting action statements: package, state The offending line appears to be:

tasks:
- name: Install cifs-utils to get the mount.cifs program
^ here 

Open in new window


Can you paste the lines around that bit, in a code block?
Avatar of Leo

ASKER

Sure, I just did. 

I meant the text of the playbook, It looks as though you have the same key at one level - one should probably be indented!
For example, the "name" line for the package to be managed must be indented 2 spaces more than the "package" key.
Avatar of Leo

ASKER

User generated image

Does this look ok? 



Avatar of Leo

ASKER

Ok, looks that the above might have worked! 


Getting closer, I hope :) 


Next looks like it can't find "install_tanium_ubuntu.sh" but I know it's there...


TASK [Copy data] ***************************************************************,fatal: [tan12]: FAILED! => {"changed": false, "msg": "Source //auto_hq00cifs_ds01.orp.nal.com/auto_cifs_ds01/8/um/tu/install_tanium_ubuntu.sh not found"},,PLAY RECAP *********************************************************************,tan12 : ok=5 changed=4 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0


Avatar of Leo

ASKER

All right...so I've manually tested the steps and they seem to be working fine when I use the following:


root@tan12:~# sudo mount.cifs //auto_hq00cifs_ds01.internal.com/auto_hq00cifs_ds01/vRA8/Tanium/Ubuntu /mnt/share/ -o user=admin@corp.adcinternal.com,pass=IloveBCN123!
root@tan12:~# cd share
-bash: cd: share: No such file or directory
root@tan12:~# cd /mnt
root@tan12:/mnt# ls

Open in new window

Something isn't working with the copy part. 


Play:

 

     - name: Allow {{ reqUser }} to sudo, and validate the sudoers file before saving
       ansible.builtin.lineinfile:
           path: "/etc/sudoers.d/90-cloud-init-users"
           state: present
           mode: "0440"
           create: yes
           regexp: '^corp\\{{ reqUser }}'
           line: 'corp\\{{ reqUser }} ALL=(ALL) NOPASSWD: ALL'
           validate: /usr/sbin/visudo -cf %s
     - name: Mount network share
       mount:
         src: //auto_hq00cifs_ds01.orp.nal.com/auto_cifs_ds01/8/um/tu/
         path: /mnt
         fstype: cifs
         opts: 'username=admin@corp.nal.com,password=XXX'
         state: mounted
       become: true
     - name: Copy data
        copy:
           src: //auto_hq00cifs_ds01.orp.nal.com/auto_cifs_ds01/8/um/tu/install_tanium_ubuntu.sh
           dest: /opt/ 
           mode: "0755"
           remote_src: yes
     - name: "Execute the script"
       command: bash /opt/install_tanium_ubuntu.sh
       register: version

Open in new window


The "become" and "mount" under "name: Mount network share" are in the wrong place again (move them left 2 spaces).

Also, in the "copy" section, you should replace
src: //auto_hq00cifs_ds01.orp.nal.com/auto_cifs_ds01/8/um/tu/install_tanium_ubuntu.sh

Open in new window

with
src: /mnt/install_tanium_ubuntu.sh

Open in new window

Or, miss out the "copy" tasks completely, and run the command as "bash /mnt/install_tanium_ubuntu.sh"
Avatar of Leo

ASKER

Sorry! For some reason the copy/paste sometimes doesn't seem to keep the spaces in place. I've updated post above. 

I have also change the src with your suggestion. 

Testing! 

Yes, copy-n-paste isn't always reliable!
Avatar of Leo

ASKER

Simon, thank you so much for the help, the teaching and the patience! :)