List Ansible:如何获取列表的值(stdout\u行)?
谢谢你给我一些时间阅读这个问题 我的完整剧本是:List Ansible:如何获取列表的值(stdout\u行)?,list,variables,ansible,List,Variables,Ansible,谢谢你给我一些时间阅读这个问题 我的完整剧本是: --- - 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: "{{ tem
---
- 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] }}"
我的问题是上一个名为“在辅助节点上升级”的任务。我试图实现的是获取listsecondary.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_行(mongodb02、mongodb03)中执行
有人知道如何做到这一点吗
提前谢谢
编辑:以下是新发行的:
存货的内容:
---
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
任务的调试:
- 变量:主
- 清单主机名的调试
- 检查主节点任务:
首先,让我们看看你的问题 在播放的第一台主机上(
play\u 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] }}"
…次要变量仅在播放主机[0]上有一个有用的值。这将是未定义的任何其他主机在发挥。您不显示playbook的输出,但我希望该playbook在辅助节点上失败
你真的想通过让只针对相关节点的播放来处理这种“只在某些节点上运行”的行为。您可以使用group\u 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' }}"
- 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"
本剧完成后,您将拥有一个名为role\u master
的组,其中包含主节点,以及一个名为role\u secondary
的组,其中包含次节点。然后,您可以在后续播放中以这些为目标,如下所示:
---
- 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' }}"
- 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"
我在中列出了一个可运行的示例。您能具体向我们展示一下您的尝试吗?向我们展示您试图使用此值的剧本。完成!我添加了完整的剧本和更好的描述:)谢谢你的帮助@larsks你是个救命恩人!谢谢这就是我所需要的!不幸的是,我在执行的时候遇到了问题:```ok:[mongodb01.ppc.us]=>{“是大师”:false}ok:[mongodb02.ppc.us]=>{“是大师”:false}ok:[mongodb03.ppc.us]=>{“是大师”:false}我的清单文件:----hosts:mongodb01.ppc.us:ansible_主机:192.168.33.2 mongodb02.ppc.us:ansible_主机:192.168.33.2 mongodb03.ppc.us:ansible_主机:192.168.33.2您应该检查
checkmaster.sh
脚本的输出是否与我在这里所做的假设(即输出的第一行包含必要的信息)。这个事实非常奇怪。checkmaster.sh的内容是echo$'rs.status()'|$MONGO_BIN | grep-b3“PRIMARY”| grep-v-E“health | state”| awk'{print$3}'| awk-F':“{print$1}”sed's/“//g'
它在mongodb01.ppc.us
上产生。发生这种情况时,所有条件都会导致剧本中出现错误的debug
任务,该任务显示master
变量的内容和inventory\u主机名的内容。也许可以把这个加到你的问题上?你无法在评论中有效地格式化它。我刚刚更新了新版本。我真的很感激你的帮助!!!我肯定我错过了一些非常简单的东西,但我不知道是什么
- 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"