{"action":"closed","issue":{"url":"https://api.github.com/repos/geerlingguy/ansible-role-drupal/issues/19","labels_url":"https://api.github.com/repos/geerlingguy/ansible-role-drupal/issues/19/labels{/name}","comments_url":"https://api.github.com/repos/geerlingguy/ansible-role-drupal/issues/19/comments","events_url":"https://api.github.com/repos/geerlingguy/ansible-role-drupal/issues/19/events","html_url":"https://github.com/geerlingguy/ansible-role-drupal/issues/19","id":127252474,"number":19,"title":"library to use drupal variables inside a playbook","user":{"login":"drzraf","id":1169926,"avatar_url":"https://avatars.githubusercontent.com/u/1169926?v=3","gravatar_id":"","url":"https://api.github.com/users/drzraf","html_url":"https://github.com/drzraf","followers_url":"https://api.github.com/users/drzraf/followers","following_url":"https://api.github.com/users/drzraf/following{/other_user}","gists_url":"https://api.github.com/users/drzraf/gists{/gist_id}","starred_url":"https://api.github.com/users/drzraf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drzraf/subscriptions","organizations_url":"https://api.github.com/users/drzraf/orgs","repos_url":"https://api.github.com/users/drzraf/repos","events_url":"https://api.github.com/users/drzraf/events{/privacy}","received_events_url":"https://api.github.com/users/drzraf/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-18T15:42:57Z","updated_at":"2016-01-18T16:35:04Z","closed_at":"2016-01-18T16:35:04Z","body":"Goal :\r\n```yaml\r\n- name: install Drupal module X\r\n  command: drush dl securelogin\r\n  when: ansible_local[\"{{ 'drupal-modules.json' }}\"]['securelogin'] is not defined\r\n```\r\n\r\nFor this to work variables and modules needs to be stored as Ansible facts.\r\nSince this is a costly process, this should not happen at every run, but only when they change (Drupal & somehow MySQL keep a trace of that)\r\n\r\nThe following fact file does exactly that, given a drush alias :\r\n```yml\r\n- name: ansible custom facts directory\r\n  file: state=directory recurse=yes path=/etc/ansible/facts.d\r\n\r\n# should be done only if variable UPDATE_TIME > stat of the fact file\r\n# but... https://bugs.mysql.com/bug.php?id=18229\r\n# UPDATE_TIME isn't supported for InnoDB\r\n\r\n# Thus the alternative:\r\n\r\n\r\n#### facts about variables\r\n- name: last time Drupal variables where modified\r\n  command: drush {{ drupal_alias }} sqlq --extra=-N \"SELECT created FROM cache_bootstrap WHERE cid = 'variables' LIMIT 1\"\r\n  register: last_variables_update\r\n  changed_when: False\r\n  sudo: no\r\n\r\n- name: last time Drupal variables where registered\r\n  stat: path=\"{{ '/etc/ansible/facts.d/drupal-variables' ~ drupal_alias ~ '.json.fact' }}\"\r\n  register: last_variables_registration\r\n\r\n- name: store drupal variables for \"{{ drupal_alias }}\"\r\n  shell: drush {{ drupal_alias|quote }} --format=json vget > \"{{ '/tmp/drupal-variables' ~ drupal_alias ~ '.json.fact' }}\"\r\n  sudo: no\r\n  # if cache_bootstrap does not contains variables, it may have just been updated recently\r\n  when: last_variables_update|failed or last_variables_registration.stat.mtime is not defined or (last_variables_update.stdout|int() > last_variables_registration.stat.mtime|int())\r\n\r\n- name: move drupal variables for \"{{ drupal_alias }}\" to fact-directory\r\n  command: removes=\"{{ '/tmp/drupal-variables' ~ drupal_alias ~ '.json.fact' }}\" mv \"{{ '/tmp/drupal-variables' ~ drupal_alias ~ '.json.fact' }}\" \"{{ '/etc/ansible/facts.d/drupal-variables' ~ drupal_alias ~ '.json.fact' }}\"\r\n\r\n\r\n#### facts about modules\r\n- name: last time Drupal modules where modified\r\n  command: drush {{ drupal_alias }} sqlq --extra=-N \"SELECT timestamp FROM watchdog WHERE type = 'system' AND message LIKE '\\%module%' ORDER BY wid desc limit 1;\"\r\n  register: last_modules_update\r\n  changed_when: False\r\n  sudo: no\r\n\r\n- name: last time Drupal modules where registered\r\n  stat: path=\"{{ '/etc/ansible/facts.d/drupal-modules' ~ drupal_alias ~ '.json.fact' }}\"\r\n  register: last_modules_registration\r\n\r\n- name: store drupal modules for \"{{ drupal_alias }}\"\r\n  shell: drush {{ drupal_alias|quote }} --format=json pmi > \"{{ '/tmp/drupal-modules' ~ drupal_alias ~ '.json.fact' }}\" 2>/dev/null\r\n  sudo: no\r\n  when: last_modules_update|failed or last_modules_registration.stat.mtime is not defined or (last_modules_update.stdout|int() > last_modules_registration.stat.mtime|int())\r\n\r\n- name: move store drupal modules for \"{{ drupal_alias }}\" to fact-directory\r\n  command: removes=\"{{ '/tmp/drupal-modules' ~ drupal_alias ~ '.json.fact' }}\" mv \"{{ '/tmp/drupal-modules' ~ drupal_alias ~ '.json.fact' }}\" \"{{ '/etc/ansible/facts.d/drupal-modules' ~ drupal_alias ~ '.json.fact' }}\"\r\n\r\n\r\n#### finally\r\n- name: chown/chmod drupal facts in /etc/ansible/facts.d/{{ \"drupal-*\" ~ drupal_alias }}.json.fact\r\n  file: path=\"/etc/ansible/facts.d/{{ item }}.json.fact\" owner=root group=root mode=0640\r\n  with_items:\r\n    - \"{{ 'drupal-variables' ~ drupal_alias }}\"\r\n    - \"{{ 'drupal-modules' ~ drupal_alias }}\"\r\n\r\n- name: re-read facts after adding custom fact(s)\r\n  setup: filter=ansible_local\r\n  when: last_variables_registration.stat.mtime is not defined or last_modules_registration.stat.mtime is not defined or last_variables_update.stdout|int > last_variables_registration.stat.mtime|int or last_modules_update.stdout|int > last_modules_registration.stat.mtime|int\r\n```\r\n\r\nThen using variables by :\r\n```yaml\r\n- name: do blah\r\n  command: drush \"{{ drupal_alias }}\" blah\r\n  sudo: no\r\n  when: ansible_local[\"{{ 'drupal-variables' ~ drupal_alias ~ '.json' }}\"]['blah'] is not defined\r\n\r\n\r\nAn alternative could be to write a Drush module for that which would determinate if variables/modules changed significantly to be worth the rewrite then handle export and write to /etc.\r\nThe task file would just have to run this drush command every time and re-read facts in order to benefit from Drupal variables/modules.\r\n\r\nShould this be a library?"}}