Ansible 回调插件 : how to get play attribute values with variables expanded?

标签 ansible

我在下面有一个游戏,并试图在回调插件中获取 remote_user 属性的解析值。

- name:          test play
  hosts:         "{{ hosts_pattern }}"
  strategy:      free
  gather_facts:  no
  remote_user:   "{{ my_remote_user if my_remote_user is defined else 'default_user' }}"
  tasks:
     - name:     a test task
       shell:    whoami && hostname

我目前正在访问 play field 属性,如下所示:
 def v2_playbook_on_play_start(self, play):
     self._play_remote_user = play.remote_user

我还尝试将 remote_user 保存在 v2_playbook_on_task_start 中看看这是否能解决问题,如 this is where the templated task name is made available .
def v2_playbook_on_task_start(self, task, is_conditional):
    self._tasks[task._uuid].remote_user = task.remote_user
    self._tasks[task._uuid].remote_user_2 = task._get_parent_attribute('remote_user')

但是以上所有情况都给我{{ my_remote_user if my_remote_user is defined else 'default_user' }}而不是扩展/解析的值。

一般来说,有没有一种巧妙的方法来获取所有游戏属性的集合,这些属性具有剧本中定义的解析值?

最佳答案

很高兴 Action 插件更容易。
ActionBase类已经有 templar 和 loader 属性。
可以遍历 task_vars 并使用 Templar.template 渲染所有内容

for k in task_vars:
    new_module_args = merge_hash(
        new_module_args,
        {k: self._templar.template(task_vars.get(k, None))}
    )

并调用模块
        result = self._execute_module(
            module_name='my_module',
            task_vars=task_vars,
            module_args=new_module_args
        )  

关于Ansible 回调插件 : how to get play attribute values with variables expanded?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46320821/

相关文章:

Ansible 2.7 include_tasks 不再接受变量

ansible-galaxy错误: API server not found

linux - 如何处理引号,反引号特殊字符以在远程服务器中运行 linux bash shell 命令

url - 等待几分钟以检查 URL 是否响应

Ansible INI 查找

python - Ansible + Ubuntu 18.04 + MySQL = "The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required."

ansible - Jenkins 管道与 terraform - 如何将输出变量馈送到以下 Ansible 阶段

tomcat - ansible jar 检查是否已经安装

python - 如何为Python虚拟环境配置Ansible?

python - 将参数传递给 Ansible 动态 list