Ansible Python API: how to pass extra vars to playbook

◇◆丶佛笑我妖孽 提交于 2020-05-15 05:34:07

问题


I'm trying to use Python API to run some playbooks because I want to integrate it with Flash to enable the web-base control. Here is my playbook(crondis.yaml):

- hosts: "{{app_name}}-server"
  tasks:
    - name: disable cron
      cron:
        name: "{{app_name|upper}}_MONITOR"
        job: "/{{app_name}}/monitor.sh"
        disabled: yes

From cml that can be this way:

ansible-playbook --extra-vars="{'app_name': 'newapp'}" crondis.yaml

But in the Python API, I'm not seeing any place to add the vars to the play. I checked Variable_Manager, DataLoader and PlaybookExecutor but didn't find any function can add vars to the play. Please kindly shed a little bit light for me if you have any idea.


回答1:


You can define extra variables by setting .extra_vars property, see ansible-playbook cli code:

    # create the variable manager, which will be shared throughout
    # the code, ensuring a consistent view of global variables
    variable_manager = VariableManager()
    variable_manager.extra_vars = load_extra_vars(loader=loader, options=self.options)



回答2:


You can specify extra variables from within context.CLIARGS. You can specify a file by using @.

context.CLIARGS = ImmutableDict(connection='local', module_path=['/to/mymodules'], forks=10, become=None,
                            become_method=None, become_user=None, check=False, diff=False,
                            extra_vars={'@/path/to/vars/file', 'key=value'})


来源:https://stackoverflow.com/questions/45220834/ansible-python-api-how-to-pass-extra-vars-to-playbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!