[英]Ansible: How to get the value of a list (stdout_lines)?
感谢您给我一些时间阅读这个问题。 :)
我完整的剧本是:
---
- 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] }}"
... secondary
变量仅在play_hosts[0]
上具有有用的值。 它将在剧中的任何其他主机上未定义。 您没有显示您的剧本的 output,但我希望这个剧本在您的辅助节点上失败。
您真的想通过仅针对相关节点的播放来处理这种“仅在某些节点上运行”行为。 您可以使用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.com/larsks/so-example-barra51中整理了一个可运行的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.