谢谢你给我一些时间看这个问题:)
我的完整剧本是。
---
- hosts: all
remote_user: secret_user
become: yes
tasks:
- include_vars: vars.yml
- name: Copy set of rpms/binaries/and Java scripts to server.
copy:
src: "{{ item }}"
dest: "{{ tempdir }}"
with_items:
- files/mongo-10gen-2.4.4-5.el6.x86_64.rpm
- files/mongo-10gen-server-2.4.4-5.el6.x86_64.rpm
- files/mongo-10gen-2.6-1.el6.x86_64.rpm
- files/mongo-10gen-server-2.6-1.el6.x86_64.rpm
- files/mongo26
- bash/javascript/populateDB.js
- name: Create mongo user.
user:
name: mongod
comment: For mongo installation.
home: /var/lib/mongo
shell: /bin/false
- name: Install mongo-2.4
yum:
name: "{{ item }}"
state: present
with_items:
- "{{ tempdir }}/mongo-10gen-2.4.4-5.el6.x86_64.rpm"
- "{{ tempdir }}/mongo-10gen-server-2.4.4-5.el6.x86_64.rpm"
- name: Send configuration files.
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- { src: 'templates/mongod.j2', dest: '/etc/mongod.conf' }
- { src: 'templates/mongodb-configsvr.j2', dest: '/etc/mongodb-configsvr.conf' }
- name: Create directories to storing logs.
file:
path: "{{ item }}"
state: directory
owner: mongod
group: mongod
mode: u=rwx,g=rw,o=rw
recurse: yes
with_items:
- /var/log/mongodb/
- /var/lib/mongodb/
- /var/lib/mongodb-config/
- name: Create files to storing logs.
file:
path: "{{ item }}"
state: touch
owner: mongod
group: mongod
with_items:
- /var/log/mongodb/mongod.log
- name: Start mongo2.4 service
shell: "{{ item }}"
with_items:
- /usr/bin/mongod -f /etc/mongod.conf
- /usr/bin/mongod -f /etc/mongodb-configsvr.conf
### Remove or comment once test is finished.
ignore_errors: yes
#name: mongod
# state: started
# args: -f /etc/mongod.conf
#### Finished Pre tasks
- name: Check who is the master.
script:
cmd: bash/checkmaster.sh
chdir: "{{ tempdir }}"
register: master
when: inventory_hostname == play_hosts[0]
- name: Check who are the secondary nodes.
script:
cmd: bash/checksecond.sh
chdir: "{{ tempdir }}"
register: secondary
when: inventory_hostname == play_hosts[0]
- name: debug var name.
debug:
var: master
- name: debug var name - second.
debug:
var: secondary
- name: Populate DB. (Execute only if is a NEW INSTALLATION).
script:
cmd: bash/populateDB.sh
chdir: "{{ tempdir }}"
register: populateDB
with_items:
- "{{ master.stdout_lines }}"
- name: (Before upgrade.) Execute V2.6 script to validate if is good to proceed.
script:
cmd: bash/mongo2.6.sh
chdir: "{{ tempdir }}"
register: Mongo26
failed_when:
- '"Everything is ready for the upgrade!" not in Mongo26.stdout'
with_items:
- "{{ master.stdout_lines }}"
# Starting with upgrade.
- name: Upgrading on SECONDARY NODES - Stop and installing service node:1.
shell: pgrep mongo | xargs kill -2; rpm -Uvh "{{ tempdir }}/mongo-10gen-2.6-1.el6.x86_64.rpm"; rpm -Uvh "{{ tempdir }}/mongo-10gen-server-2.6-1.el6.x86_64.rpm"
throttle: 1
with_items:
- "{{ secondary.stdout_lines[0] }}"
- "{{ secondary.stdout_lines[1] }}"
我的问题是最后一个任务叫 Upgrading on secondary nodes
. 我想达到的目的是得到列表中的值secondary.stdoutlines
来将最后一个任务只传递给这些服务器。这个变量的内容有。
TASK [debug var name - second.] ****************************************************************************************************************************************************************************
ok: [192.168.33.2] => {
"secondary": {
"changed": true,
"failed": false,
"rc": 0,
"stderr": "Shared connection to 192.168.33.2 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.33.2 closed."
],
"stdout": "\r\nmongodb02.ppc.us\r\nmongodb03.ppc.us\r\n",
"stdout_lines": [
"",
"mongodb02.ppc.us",
"mongodb03.ppc.us"
]
}
}
ok: [192.168.33.3] => {
"secondary": {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
}
ok: [192.168.33.4] => {
"secondary": {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
}
所以,我想把最后一个任务运行在以下节点上: {{secondary.stdout_lines}}
. 每当我运行这个剧本时,与。
with_items:
- "{{ secondary.stdout_lines[0] }}"
- "{{ secondary.stdout_lines[1] }}"
这个任务在第一个节点(mongodb01)上执行,而不是在secondary.stdout_lines(mongodb02,mongodb03)上执行。
有谁知道如何实现这个问题?
先谢谢大家了
编辑:这里是新问题。
的内容 inventory:
---
all:
hosts:
mongodb01.ppc.us:
ansible_host: 192.168.33.2
mongodb02.ppc.us:
ansible_host: 192.168.33.3
mongodb03.ppc.us:
ansible_host: 192.168.33.4
vars:
ansible_python_interpreter: /usr/bin/python
任务的调试。
TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [mongodb01.ppc.us] => {
"master": {
"changed": true,
"failed": false,
"rc": 0,
"stderr": "Shared connection to mongodb01.ppc.us closed.\r\n",
"stderr_lines": [
"Shared connection to mongodb01.ppc.us closed."
],
"stdout": "\r\nmongodb01.ppc.us\r\n",
"stdout_lines": [
"",
"mongodb01.ppc.us"
]
}
}
ok: [mongodb02.ppc.us] => {
"master": {
"changed": true,
"failed": false,
"rc": 0,
"stderr": "Shared connection to mongodb02.ppc.us closed.\r\n",
"stderr_lines": [
"Shared connection to mongodb02.ppc.us closed."
],
"stdout": "\r\nmongodb01.ppc.us\r\n",
"stdout_lines": [
"",
"mongodb01.ppc.us"
]
}
}
ok: [mongodb03.ppc.us] => {
"master": {
"changed": true,
"failed": false,
"rc": 0,
"stderr": "Shared connection to mongodb03.ppc.us closed.\r\n",
"stderr_lines": [
"Shared connection to mongodb03.ppc.us closed."
],
"stdout": "\r\nmongodb01.ppc.us\r\n",
"stdout_lines": [
"",
"mongodb01.ppc.us"
]
}
}
TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [mongodb01.ppc.us] => {
"inventory_hostname": "mongodb01.ppc.us"
}
ok: [mongodb02.ppc.us] => {
"inventory_hostname": "mongodb02.ppc.us"
}
ok: [mongodb03.ppc.us] => {
"inventory_hostname": "mongodb03.ppc.us"
}
[WARNING]: Could not match supplied host pattern, ignoring: role_master
PLAY [role_master] *****************************************************************************************************************************************************************************************
skipping: no hosts matched
PLAY [role_secondary] **************************************************************************************************************************************************************************************
TASK [check master node] ***********************************************************************************************************************************************************************************
changed: [mongodb01.ppc.us] => {"changed": true, "rc": 0, "stderr": "Shared connection to 192.168.33.2 closed.\r\n", "stderr_lines": ["Shared connection to 192.168.33.2 closed."], "stdout": "\r\nmongodb01.ppc.us\r\n", "stdout_lines": ["", "mongodb01.ppc.us"]}
changed: [mongodb02.ppc.us] => {"changed": true, "rc": 0, "stderr": "Shared connection to 192.168.33.3 closed.\r\n", "stderr_lines": ["Shared connection to 192.168.33.3 closed."], "stdout": "\r\nmongodb01.ppc.us\r\n", "stdout_lines": ["", "mongodb01.ppc.us"]}
changed: [mongodb03.ppc.us] => {"changed": true, "rc": 0, "stderr": "Shared connection to 192.168.33.4 closed.\r\n", "stderr_lines": ["Shared connection to 192.168.33.4 closed."], "stdout": "\r\nmongodb01.ppc.us\r\n", "stdout_lines": ["", "mongodb01.ppc.us"]}
首先,让我们看看你的问题。
在你的游戏中的第一个主机上(play_hosts[0]
),你在设置 secondary
变量为运行结果的 bash/checksecond.sh
. 你在剧中没有明确地在其他主机上做这件事。这意味着当你运行这个任务时... ...
- name: Upgrading on SECONDARY NODES - Stop and installing service node:1.
shell: pgrep mongo | xargs kill -2; rpm -Uvh "{{ tempdir }}/mongo-10gen-2.6-1.el6.x86_64.rpm"; rpm -Uvh "{{ tempdir }}/mongo-10gen-server-2.6-1.el6.x86_64.rpm"
throttle: 1
with_items:
- "{{ secondary.stdout_lines[0] }}"
- "{{ secondary.stdout_lines[1] }}"
...the secondary
变量只有在 play_hosts[0]
. 它将在任何其他主机上未定义的播放。你没有显示你的剧本的输出,但我希望这个剧本在你的次要节点上会失败。
你真的想通过只针对相关节点的剧本来处理这种 "只在某些节点上运行 "的行为。你可以使用 "动态节点组 "来处理这种 "只在某些节点上运行 "的行为。group_by
模块。 例如,如果我们假设您的 bash/checkmaster.sh
脚本输出主节点的名称,我们可以这样做。
---
- hosts: all
gather_facts: false
tasks:
- name: check master node
script:
cmd: bash/checkmaster.sh
register: master
- set_fact:
is_master: "{{ master.stdout_lines[0] == inventory_hostname }}"
- group_by:
key: "role_{{ 'master' if is_master else 'secondary' }}"
在这个剧本完成后,你将会有一个组名为 role_master
包含主节点,以及一个名为 role_secondary
,其中包含二级节点。然后,你可以在后续的游戏中像这样针对这些节点。
- hosts: role_master
gather_facts: false
tasks:
- debug:
msg: "tasks on master node"
- hosts: role_secondary
gather_facts: false
tasks:
- debug:
msg: "tasks on secondary nodes"
我已经把一个可运行的例子放在了 https:/github.comlarsksso-example-barra51。.