{"action":"created","issue":{"url":"https://api.github.com/repos/saltstack/salt/issues/21214","labels_url":"https://api.github.com/repos/saltstack/salt/issues/21214/labels{/name}","comments_url":"https://api.github.com/repos/saltstack/salt/issues/21214/comments","events_url":"https://api.github.com/repos/saltstack/salt/issues/21214/events","html_url":"https://github.com/saltstack/salt/issues/21214","id":59445622,"number":21214,"title":"How to call other state in custom state code?","user":{"login":"Colstuwjx","id":3972545,"avatar_url":"https://avatars.githubusercontent.com/u/3972545?v=3","gravatar_id":"","url":"https://api.github.com/users/Colstuwjx","html_url":"https://github.com/Colstuwjx","followers_url":"https://api.github.com/users/Colstuwjx/followers","following_url":"https://api.github.com/users/Colstuwjx/following{/other_user}","gists_url":"https://api.github.com/users/Colstuwjx/gists{/gist_id}","starred_url":"https://api.github.com/users/Colstuwjx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Colstuwjx/subscriptions","organizations_url":"https://api.github.com/users/Colstuwjx/orgs","repos_url":"https://api.github.com/users/Colstuwjx/repos","events_url":"https://api.github.com/users/Colstuwjx/events{/privacy}","received_events_url":"https://api.github.com/users/Colstuwjx/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/saltstack/salt/labels/Feature","name":"Feature","color":"009800"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/saltstack/salt/milestones/13","html_url":"https://github.com/saltstack/salt/milestones/Approved","labels_url":"https://api.github.com/repos/saltstack/salt/milestones/13/labels","id":9265,"number":13,"title":"Approved","description":"All issues that are ready to be worked on, both bugs and features.","creator":{"login":"thatch45","id":507599,"avatar_url":"https://avatars.githubusercontent.com/u/507599?v=3","gravatar_id":"","url":"https://api.github.com/users/thatch45","html_url":"https://github.com/thatch45","followers_url":"https://api.github.com/users/thatch45/followers","following_url":"https://api.github.com/users/thatch45/following{/other_user}","gists_url":"https://api.github.com/users/thatch45/gists{/gist_id}","starred_url":"https://api.github.com/users/thatch45/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thatch45/subscriptions","organizations_url":"https://api.github.com/users/thatch45/orgs","repos_url":"https://api.github.com/users/thatch45/repos","events_url":"https://api.github.com/users/thatch45/events{/privacy}","received_events_url":"https://api.github.com/users/thatch45/received_events","type":"User","site_admin":false},"open_issues":2387,"closed_issues":2203,"state":"open","created_at":"2011-05-14T04:00:56Z","updated_at":"2015-06-24T15:40:10Z","due_on":null,"closed_at":null},"comments":25,"created_at":"2015-03-02T06:59:53Z","updated_at":"2015-06-24T16:41:02Z","closed_at":null,"body":"Hi, I'm writing a custom state in /srv/salt/_states/git_refresh.py, and I just want to call file.managed state in this py.\r\n\r\nWhat should I do??\r\nThanks."},"comment":{"url":"https://api.github.com/repos/saltstack/salt/issues/comments/114937464","html_url":"https://github.com/saltstack/salt/issues/21214#issuecomment-114937464","issue_url":"https://api.github.com/repos/saltstack/salt/issues/21214","id":114937464,"user":{"login":"thusoy","id":1466123,"avatar_url":"https://avatars.githubusercontent.com/u/1466123?v=3","gravatar_id":"","url":"https://api.github.com/users/thusoy","html_url":"https://github.com/thusoy","followers_url":"https://api.github.com/users/thusoy/followers","following_url":"https://api.github.com/users/thusoy/following{/other_user}","gists_url":"https://api.github.com/users/thusoy/gists{/gist_id}","starred_url":"https://api.github.com/users/thusoy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thusoy/subscriptions","organizations_url":"https://api.github.com/users/thusoy/orgs","repos_url":"https://api.github.com/users/thusoy/repos","events_url":"https://api.github.com/users/thusoy/events{/privacy}","received_events_url":"https://api.github.com/users/thusoy/received_events","type":"User","site_admin":false},"created_at":"2015-06-24T16:41:02Z","updated_at":"2015-06-24T16:41:02Z","body":"@jacksontj I don't agree that's a better solution, both files would be created even if only one of them is needed. Thus you'd need to check which init system is running, and you'd need to duplicate those checks and both files (three files if you also need to support systemd) for every state that sets up a running service. Putting this in one location greatly reduces duplication, which sounds much more reusable to me, but I think we're talking about different kinds of re-usability. How do you think this is less pluggable? Publishing the custom state in a repo would allow it to be re-used by lots of other states if they just include the repo as a gitfs backend, wouldn't that be pluggable?\r\n\r\nIn any case I don't think the best design of this precise problem is very interesting for the issue at hand, custom states will often need to either wrap many existing states or combine several of them, I think being able to re-use them is essential to avoid duplication of effort. This could probably also make many states shipped with salt easier, see f. ex `cron.file` (which basically re-implements `file.managed`).\r\n\r\nAnyway, I stumbled across something that I think solves the problem: `state.single`. At least for me this works:\r\n\r\n```python\r\ndef managed(name):\r\n  state_ret = __salt__['state.single']('file.managed', *args)\r\n  file_ret = state_ret.values()[0]\r\n  <...>\r\n```\r\n\r\nThe only difference from calling `file.managed` directly is that `state.single` [wraps the return value](https://github.com/saltstack/salt/blob/develop/salt/modules/state.py#L1088), so you have to dig it out with `.values()[0]`, but that's not a big problem. Is there any reason why this shouldn't generally work? If it does it's just a matter of adding a note to the custom state docs and this could be closed."}}