Template unable to replace file; the playbook uses "become" to get the elevated permission

472 views
Skip to first unread message

Indra Gunawan

unread,
Jan 19, 2016, 8:31:27 AM1/19/16
to Ansible Project
This error is a blocker my effort to use Template to automate our Jenkins installation to target Jenkins master on our corporate environment:

PUT /tmp/tmpUjjIrF TO /users/ingunawa/.ansible/tmp/ansible-tmp-1453075333.7-198301965511122/source
EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1453075334.26-42896703240882 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1453075334.26-42896703240882 )" )
PUT /tmp/tmpcz5OYl TO /users/ingunawa/.ansible/tmp/ansible-tmp-1453075334.26-42896703240882/copy
EXEC /bin/sh -c 'sudo -H -S -p "[sudo via ansible, key=vtgcupyzjtojsbtboeyttvmiwyunzkwc] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-vtgcupyzjtojsbtboeyttvmiwyunzkwc; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /users/ingunawa/.ansible/tmp/ansible-tmp-1453075334.26-42896703240882/copy; rm -rf "/users/ingunawa/.ansible/tmp/ansible-tmp-1453075334.26-42896703240882/" > /dev/null 2>&1'"'"''
fatal: [sjc-nmtgci-15]: FAILED! => {"changed": true, "failed": true, "invocation": {"module_args": {"dest": "/opt/nmtgre-tools/jenkins/config/jenkins", "group": "eng", "mode": "0600", "owner": "px-build", "src": "default-jenkins.j2"}, "module_name": "template"}, "module_stderr": "", "module_stdout": "\r\nBECOME-SUCCESS-vtgcupyzjtojsbtboeyttvmiwyunzkwc\r\n{\"msg\": \"Could not replace file: /users/ingunawa/.ansible/tmp/ansible-tmp-1453075333.7-198301965511122/source to /opt/nmtgre-tools/jenkins/config/jenkins: [Errno 13] Permission denied: '/users/ingunawa/.ansible/tmp/ansible-tmp-1453075333.7-198301965511122/source'\", \"failed\": true, \"invocation\": {\"module_args\": {\"src\": \"/users/ingunawa/.ansible/tmp/ansible-tmp-1453075333.7-198301965511122/source\", \"directory_mode\": null, \"force\": true, \"follow\": true, \"remote_src\": null, \"dest\": \"/opt/nmtgre-tools/jenkins/config/jenkins\", \"selevel\": null, \"seuser\": null, \"serole\": null, \"content\": null, \"setype\": null, \"original_basename\": \"default-jenkins.j2\", \"delimiter\": null, \"mode\": \"0600\", \"regexp\": null, \"owner\": \"px-build\", \"group\": \"eng\", \"validate\": null, \"backup\": false}}}\r\nException exceptions.OSError: (2, 'No such file or directory', '/opt/nmtgre-tools/jenkins/config/.ansible_tmpFDjmX5jenkins') in ', mode 'w+b' at 0x16019a80>> ignored\r\n", "msg": "MODULE FAILURE", "parsed": false}


Here is the playbook (roles/jenkins-master/tasks/main.yml)

---

- name: Create {{ jenkins_install_dir }}
  file: path={{ item }} state=directory owner={{ jenkins_user }} group={{ jenkins_group }} mode=0644
  with_items:
    - {{ jenkins_install_dir }}
    - {{ jenkins_install_dir }}/war-packages
    - {{ jenkins_install_dir }}/config

- name: Download the Jenkins LTS from mirrors.jenkins-ci.org
  get_url:
    url="http://mirrors.jenkins-ci.org/war-stable/{{ jenkins_version }}/jenkins.war"
    dest={{ jenkins_install_dir }}/war-packages
    owner={{ jenkins_user }}
    group={{ jenkins_group }}
    mode=0755

- name: Remove existing Jenkins init script
   file: path=/etc/init.d/jenkins state=absent

- name: Create Jenkins init script
  template:
    src=default-jenkins-initd.j2
    dest=/etc/init.d/jenkins
    owner=root
    group=root
    mode=0755

- name: Create Jenkins default configuration file
  template:
    src=default-jenkins.j2
    dest={{ jenkins_install_dir }}/config/jenkins
    owner={{ jenkins_user }}
    group={{ jenkins_group }}
    mode=0600

The playbook that calls it (jenkins-master.yml)

---
# file: jenkins-master.yml
- hosts: jenkinsci
  gather_facts: false
  become: true

  roles:
    - jenkins-master

To run the playbook : ansible-playbook -i hosts jenkins-master.xml --limit <hostname>

jenkinsci is group in hosts:
[jenkinsci:children]
jenkinsci-sjc
jenkinsci-bxb
jenkinsci-rtp
jenkinsci-bgl
jenkinsci-gpk

The hostname further is listed under jenkinsci-sjc.


I have looked up the github for issues related to template.  The issue I found remotely close to this is:

and it is already closed.


I am using ansible version 2.0.0.2.  The same playbook also does not work on my dev box running ansible version 1.9.4

Any insight is greatly appreciated!

Thank you
-Indra

Indra Gunawan

unread,
Jan 19, 2016, 4:19:08 PM1/19/16
to Ansible Project
I am running it as me (userid: ingunawa) like this "ansible-playbook -i hosts jenkins-master.xml --limit <hostname> -k -K"

I have sudo permission on the target machine/VM.  I can create/copy file to /etc/init.d as root and create/copy file to the other directory owned by the other user (generic user) owning/running Jenkins. 

May I know how Template is used ideally with "become".  The become_method by default is "sudo".
The default "become_user" is root I believe.

Thank you

Reply all
Reply to author
Forward
0 new messages