我试图运行一个Ansible playbook,它将所有主机(或每个组)收集到一个列表中,然后我将作为参数传递给Python脚本。 我尝试了很多东西,但最后只将当前主机传递给脚本。
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]
[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 }}"
...
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!"
}
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
#! /usr/bin/python3
import sys
print ( sys.argv)
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:"]}
答案 0 :(得分:0)
最有可能的是,您只需要完成这项任务:
- script: "/home/oded/Sandbox/ansible/pyScript/test.py {{ groups['all'] | join(' ') }}"
when: corename is defined
它会将库存名称all
组的成员作为空格分隔的参数传递给脚本。
如果需要,请添加引号。
很难评论您发布的代码中的所有错误......
var: "{{ ips }}"
应为var: ips
。debug
注册。这就是set_fact
的用途。item
。