简体   繁体   English

创建主机列表以便传递给Ansible剧本中的Python脚本

[英]Creating hosts list in order to pass to Python script in Ansible playbook

I'm trying to run an Ansible playbook that will gather all the hosts (or per group) to a list that next I'll pass as an argument to a Python script. 我正在尝试运行Ansible剧本,它将所有主机(或每个组)收集到一个列表,接下来我将其作为Python脚本的参数传递。 I tried tons of things but at the end only the current host is passed to the script. 我尝试了很多事情,但最后仅将当前主机传递给了脚本。

ansible 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

Some examples: 一些例子:


My simple hosts file: 我的简单主机文件:

[cluster_1]
100.126.247.167 corename=paas-om-core1 vip=1.1.1.1
100.126.246.237 vip=1.1.1.1
100.126.246.201

---
 - hosts: all
   become: yes
   become_user: root
   vars:
     ips: []
   tasks:
     - name: create list
       set_fact:
          ips: []

     - name: append to list
       set_fact:
         ips: "{{ ips  }} + [ '{{ inventory_hostname  }}' ]"

     - debug:
         var: "{{ ips }}"

Result: 结果:

...
TASK [append to list] *****************************************************************************************************************************************************************************************
ok: [100.126.247.167] => {"ansible_facts": {"ips": ["100.126.247.167"]}, "changed": false}
ok: [100.126.246.237] => {"ansible_facts": {"ips": ["100.126.246.237"]}, "changed": false}
ok: [100.126.246.201] => {"ansible_facts": {"ips": ["100.126.246.201"]}, "changed": false}

TASK [debug] **************************************************************************************************************************************************************************************************
ok: [100.126.247.167] => {
    "<type 'list'>": "VARIABLE IS NOT DEFINED!"
}
ok: [100.126.246.237] => {
    "<type 'list'>": "VARIABLE IS NOT DEFINED!"
}
ok: [100.126.246.201] => {
    "<type 'list'>": "VARIABLE IS NOT DEFINED!"
}

Example 2: 范例2:

tasks:
  - debug:
      msg: "{{ item }}"
    with_items:
         - "{{ groups['all'] }}"
    when: corename is defined  /*I want it run only on one defined host */
    register: host_list
  - debug:
      var: host_list

  - debug:
      var: item.item
    with_items: "{{ host_list.results }}"
    register: items

 - debug:
     var: items
 - script: "/home/oded/Sandbox/ansible/pyScript/test.py {{ host_list  }}"
   when: corename is defined
 - script: "/home/oded/Sandbox/ansible/pyScript/test.py {{host_list.results}}"
   when: corename is defined
 - script: "/home/oded/Sandbox/ansible/pyScript/test.py {{ items  }}"
   when: corename is defined

test.py: test.py:

#! /usr/bin/python3
import sys
print ( sys.argv)

Results: 结果:

TASK [debug] **************************************************************************************************************************************************************************************************
skip ...
ok: [100.126.247.167] => (item=100.126.247.167) => {
    "changed": false,
    "item": "100.126.247.167",
    "msg": "100.126.247.167"
}
ok: [100.126.247.167] => (item=100.126.246.237) => {
    "changed": false,
    "item": "100.126.246.237",
    "msg": "100.126.246.237"
}
ok: [100.126.247.167] => (item=100.126.246.201) => {
    "changed": false,
    "item": "100.126.246.201",
    "msg": "100.126.246.201"
}

TASK [debug] **************************************************************************************************************************************************************************************************
ok: [100.126.247.167] => {
    "host_list": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": "100.126.247.167",
                "msg": "100.126.247.167"
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": "100.126.246.237",
                "msg": "100.126.246.237"
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": "100.126.246.201",
                "msg": "100.126.246.201"
            }
        ]
    }
}
ok: [100.126.246.237] => {
    "host_list": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "changed": false,
                "item": "100.126.247.167",
                "skip_reason": "Conditional result was False",
                "skipped": true
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "changed": false,
                "item": "100.126.246.237",
                "skip_reason": "Conditional result was False",
                "skipped": true
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "changed": false,
                "item": "100.126.246.201",
                "skip_reason": "Conditional result was False",
                "skipped": true
            }
        ]
    }
}
skip ...

TASK [debug] **************************************************************************************************************************************************************************************************
ok: [100.126.247.167] => (item={'changed': False, '_ansible_no_log': False, 'item': u'100.126.247.167', '_ansible_item_result': True, 'failed': False, 'msg': u'100.126.247.167', '_ansible_verbose_always': True, '_ansible_ignore_errors': None}) => {
    "changed": false,
    "item": {
        "changed": false,
        "failed": false,
        "item": "100.126.247.167",
        "msg": "100.126.247.167"
    },
    "item.item": "100.126.247.167"
}
ok: [100.126.247.167] => (item={'changed': False, '_ansible_no_log': False, 'item': u'100.126.246.237', '_ansible_item_result': True, 'failed': False, 'msg': u'100.126.246.237', '_ansible_verbose_always': True, '_ansible_ignore_errors': None}) => {
    "changed": false,
    "item": {
        "changed": false,
        "failed": false,
        "item": "100.126.246.237",
        "msg": "100.126.246.237"
    },
    "item.item": "100.126.246.237"
}
ok: [100.126.247.167] => (item={'changed': False, '_ansible_no_log': False, 'item': u'100.126.246.201', '_ansible_item_result': True, 'failed': False, 'msg': u'100.126.246.201', '_ansible_verbose_always': True, '_ansible_ignore_errors': None}) => {
    "changed": false,
    "item": {
        "changed": false,
        "failed": false,
        "item": "100.126.246.201",
        "msg": "100.126.246.201"
    },
    "item.item": "100.126.246.201"
}
skip ...

TASK [debug] **************************************************************************************************************************************************************************************************
ok: [100.126.247.167] => {
    "items": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": {
                    "_ansible_ignore_errors": null,
                    "_ansible_item_result": true,
                    "_ansible_no_log": false,
                    "_ansible_verbose_always": true,
                    "changed": false,
                    "failed": false,
                    "item": "100.126.247.167",
                    "msg": "100.126.247.167"
                },
                "item.item": "100.126.247.167"
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": {
                    "_ansible_ignore_errors": null,
                    "_ansible_item_result": true,
                    "_ansible_no_log": false,
                    "_ansible_verbose_always": true,
                    "changed": false,
                    "failed": false,
                    "item": "100.126.246.237",
                    "msg": "100.126.246.237"
                },
                "item.item": "100.126.246.237"
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": {
                    "_ansible_ignore_errors": null,
                    "_ansible_item_result": true,
                    "_ansible_no_log": false,
                    "_ansible_verbose_always": true,
                    "changed": false,
                    "failed": false,
                    "item": "100.126.246.201",
                    "msg": "100.126.246.201"
                },
                "item.item": "100.126.246.201"
            }
        ]
    }
}
skip ...

TASK [script] *************************************************************************************************************************************************************************************************
skipping: [100.126.246.237] => {"changed": false, "skip_reason": "Conditional result was False"}
skipping: [100.126.246.201] => {"changed": false, "skip_reason": "Conditional result was False"}
changed: [100.126.247.167] => {"changed": true, "rc": 0, "stderr": "Shared connection to 100.126.247.167 closed.\r\n", "stdout": "{msg:\r\n", "stdout_lines": ["{msg:"]}

TASK [script] *************************************************************************************************************************************************************************************************
skipping: [100.126.246.237] => {"changed": false, "skip_reason": "Conditional result was False"}
skipping: [100.126.246.201] => {"changed": false, "skip_reason": "Conditional result was False"}
changed: [100.126.247.167] => {"changed": true, "rc": 0, "stderr": "Shared connection to 100.126.247.167 closed.\r\n", "stdout": "[{changed:\r\n", "stdout_lines": ["[{changed:"]}

TASK [script] *************************************************************************************************************************************************************************************************
skipping: [100.126.246.237] => {"changed": false, "skip_reason": "Conditional result was False"}
skipping: [100.126.246.201] => {"changed": false, "skip_reason": "Conditional result was False"}
changed: [100.126.247.167] => {"changed": true, "rc": 0, "stderr": "Shared connection to 100.126.247.167 closed.\r\n", "stdout": "{msg:\r\n", "stdout_lines": ["{msg:"]}

Most likely, all you want is this single task: 最有可能的,您只需要完成以下一项任务:

- script: "/home/oded/Sandbox/ansible/pyScript/test.py {{ groups['all'] | join(' ')  }}"
  when: corename is defined

It will pass the inventory names, members of the all group, as space-separated arguments to the script. 它将清单名称( all组的成员)作为空格分隔的参数传递给脚本。

Add quotes around, if you need. 如果需要,请在周围加上引号。


It's hard to comment on all the mistakes in the code you posted... 很难评论您发布的代码中的所有错误...

  • Semantic error in the first method: var: "{{ ips }}" should be var: ips . 第一种方法的语义错误: var: "{{ ips }}"应该是var: ips
  • Expectation of facts having a common value across all hosts (while a fact holds a different value on each distinct host). 对事实的期望在所有主机上具有相同的值(而事实在每个不同的主机上具有不同的值)。
  • Misuse of register with debug in the second method. 第二种方法在debug中误用了寄存器。 That's what set_fact is for. 这就是set_fact目的。
  • Trying to pass a Python object as a command line argument. 尝试将Python对象作为命令行参数传递。
  • Using item outside of loops. 在循环外使用item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法从 Ansible 剧本调用 Python 脚本并将 output 作为变量再次传递给 Ansible 剧本以进行下一个任务? - Is there any way to call a Python script from an Ansible playbook and pass the output as variable to an Ansible playbook back again for the next task? Unbale 从 Ansible playbook 运行 python 脚本 - Unbale to run python script from Ansible playbook 如何将字典列表从Python脚本导入Ansible剧本? - How do I import List of dictionaries from Python script into Ansible playbook? Ansible剧本脚本失败 - Ansible playbook script failure Ansible python API 2.0:在Windows客户端的python脚本中运行一个playbook - Ansible python API 2.0: run a playbook in a python script for windows clients 尝试运行python脚本时出现错误的剧本错误 - ansible playbook errors out while trying to to run python script 如何使用虚拟环境在 ansible 剧本中运行 python 脚本? - how to run python script in ansible playbook using virtual enviroment? Ansible AWX:当 .yml 和 .py 文件位于 Github 中时,使用 Ansible playbook 触发 python 脚本 - Ansible AWX: trigger python script using Ansible playbook when .yml and .py files are located in Github Ansible 剧本中的 Python 正则表达式问题 - Python regexp problem in Ansible playbook 如何使用 ansible playbook 在 python 中运行 Ansible 模块 - How to run Ansible Module in python with an ansible playbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM