{"action":"opened","issue":{"url":"https://api.github.com/repos/ansible/ansible/issues/17714","repository_url":"https://api.github.com/repos/ansible/ansible","labels_url":"https://api.github.com/repos/ansible/ansible/issues/17714/labels{/name}","comments_url":"https://api.github.com/repos/ansible/ansible/issues/17714/comments","events_url":"https://api.github.com/repos/ansible/ansible/issues/17714/events","html_url":"https://github.com/ansible/ansible/issues/17714","id":178740006,"number":17714,"title":"Appending or \"stacking\" variable lists","user":{"login":"rahniwalden","id":22380896,"avatar_url":"https://avatars.githubusercontent.com/u/22380896?v=3","gravatar_id":"","url":"https://api.github.com/users/rahniwalden","html_url":"https://github.com/rahniwalden","followers_url":"https://api.github.com/users/rahniwalden/followers","following_url":"https://api.github.com/users/rahniwalden/following{/other_user}","gists_url":"https://api.github.com/users/rahniwalden/gists{/gist_id}","starred_url":"https://api.github.com/users/rahniwalden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rahniwalden/subscriptions","organizations_url":"https://api.github.com/users/rahniwalden/orgs","repos_url":"https://api.github.com/users/rahniwalden/repos","events_url":"https://api.github.com/users/rahniwalden/events{/privacy}","received_events_url":"https://api.github.com/users/rahniwalden/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-22T22:46:46Z","updated_at":"2016-09-22T22:46:46Z","closed_at":null,"body":"##### ISSUE TYPE\r\n<!--- Pick one below and delete the rest: -->\r\n\r\n - Feature Idea\r\n\r\n##### COMPONENT NAME\r\n<!--- Name of the plugin/task/feature -->\r\nAnsible support for appending or \"stacking\" Variable Lists\r\n\r\nIn this case, with \"with_items\"\r\n--Should be able to append variable lists on the fly, conditionally\r\n##### ANSIBLE VERSION\r\n<!--- Paste verbatim output from “ansible --version” between quotes below -->\r\n```\r\nansible --version\r\nansible 2.1.0.0\r\n  config file = /etc/ansible/ansible.cfg\r\n  configured module search path = Default w/o overrides\r\n\r\n```\r\n\r\n##### OS / ENVIRONMENT\r\n<!---\r\nN/A\r\n-->\r\n\r\n##### SUMMARY\r\n<!--- Explain the problem briefly -->\r\nWe have the goal of dynamically being able to append lists to another list in a variable during a task. This setup is for the configuration of sec on rhel 6. We have different variables we want appended to a base list, based on conditions being met by the host.\r\n\r\nFor example, we have three variables defined at the moment. One is listed in /defaults/main.yml. A basic list, then two others that correspond to additional configurations needed by different types of application hosts.\r\n\r\nLet's call the vars:\r\nbase\r\napp1 \r\napp2\r\n\r\nPure python can handle recursion. If we set:\r\n```\r\n  base = base + app1\r\n  then set: base = base + app2\r\n  we get: base = base + app1 + app2\r\n```\r\n\r\nBut Ansible does not appear to support this. It fails with a, \"recursion detected in template...\" error. If you fix the self-reference, Ansible  of course will only accepts the last value set, for example:\r\n\r\n```\r\n  base = config + app1\r\n  then set: base = config + app2\r\n  we get: base = config + app2\r\n```\r\n\r\nI've seen the same error pretty much spelled out here: https://groups.google.com/forum/#!topic/ansible-project/Jo50nu3g2b4\r\n\r\nAs of right now, we can get around this by listing all the variables (and only including the variables based on conditions being met by the host). For example:\r\n\r\n```\r\n- name: write /etc/sysconfig/sec \r\n  lineinfile: \r\n    state: present \r\n    dest: /path/to/file\r\n    backup: yes \r\n    insertafter: EOF \r\n    line: \"{{ item }}\" \r\n  with_items: \r\n    - \"{{ base }}\"\r\n    - \"{{ app1 | default(omit) }}\"\r\n    - \"{{ app2 | default(omit) }}\"\r\n```\r\nWe are required to continue appending variables under with_items, since currently, Ansible only uses the last definition of the variable.\r\n\r\nBut it would be nice to have ansible be able to handle appending variable values, so we could simply include something akin to the following in vars/app1.yml:\r\n\r\n```\r\n---\r\napp1:  <value>\r\nbase: \"{{ base + app1 }}\"\r\n\r\n```\r\n\r\nWhich would make it easier to manage large numbers of hosts which may need various mixes of settings applied. Including recursion in the variable file would cover all cases (appending the needed value to the base variable on the fly), instead of having to create the var.yml file and then still go back in and add the \"{{ app# | default(omit) }}\" to the list....."}}
{"actor":{"display_login":"rahniwalden"}}