読者です 読者をやめる 読者になる 読者になる

ansistranoのafter_symlinkでsudo(become)を実行する

Python

ansible の YAML

ansible ansistrano の after_symlink hook で
/etc/init.d/
シンボリックリンクを作る。

- name: initd symlink
  become: true
  file:
    state: link
    src: /var/www/app1/current/deploy/initd/app1_uwsgi
    #dest: /home/user1/app1_uwsgi
    dest: /etc/init.d/app1_uwsgi
    mode: "u=rwx,g=rx,o=rx"

sudoをするにはbecomeを使うと書いてあったけど、
指定の方法が分からなかった。
“配列の中にハッシュをネスト” という方法で書かないといけないっぽい。

http://docs.ansible.com/ansible/become.html

http://www.task-notes.com/entry/20150922/1442890800

ansible-playbook -i inventory/hosts/localvm playbook/deploy.yml -K

ansible-playbook コマンドの最後に -K を付けると最初にsudo passwordを聞かれる。
Ansible, sudoパスワード要求を忘れただけでめんどくなる - Goldstine研究所

エラーメッセージ

その1

fatal: [192.168.3.43]: FAILED! => {"failed": true, "reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.\n\nThe error appears to have been in '/Users/aaa/PycharmProjects/app1/deploy/playbook/ansistrano/after_symlink.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- tasks:\n  ^ here\n\n\nThe error appears to have been in '/Users/aaa/PycharmProjects/app1/deploy/playbook/ansistrano/after_symlink.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- tasks:\n  ^ here\n"}

その2

fatal: [192.168.3.43]: FAILED! => {"failed": true, "reason": "Syntax Error while loading YAML.\n\n\nThe error appears to have been in '/Users/aaa/PycharmProjects/app1/deploy/playbook/ansistrano/after_symlink.yml': line 4, column 18, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: create django static files\n    django_manage:\n                 ^ here\n"}

完成品

PycharmProjects/app1/deploy/playbook/ansistrano/after_symlink.yml

- name: create django static files
  django_manage:
    command: collectstatic
    app_path: "{{ ansistrano_deploy_to }}/current/"
    virtualenv: "/home/user1/.pyenv/versions/app1/"


- name: init.d app1_uwsgi symlink
  become: true
  file:
    state: link
    src: /var/www/app1/current/deploy/initd/app1_uwsgi
    #dest: /home/user1/app1_uwsgi
    dest: /etc/init.d/app1_uwsgi
    mode: "u=rwx,g=rx,o=rx"

- name: init.d app1_celeryd symlink
  become: true
  file:
    state: link
    src: /var/www/app1/current/deploy/initd/app1_celeryd
    dest: /etc/init.d/app1_celeryd
    mode: "u=rwx,g=rx,o=rx"

- name: restart app1_uwsgi
  become: true
  service:
    name: app1_uwsgi
    state: restarted

- name: restart app1_celeryd
  become: true
  service:
    name: app1_celeryd
    state: restarted