ansible with_dict and register variable
default/main.yml
is:
mongodb_nodes:
node1:
ip: 192.168.190.161
port: 27017
primary: true
node2:
ip: 192.168.190.162
port: 27017
primary: false
id: 2
node3:
ip: 192.168.190.163
port: 27017
primary: false
my tasks/mongodb.yml
is:
- name: --- detect master node ---
shell: >
mongo --host 127.0.0.1 --port 27017
--quiet --eval 'db.isMaster().primary'
when: item.value.primary == true and item.value.ip == ansible_env.SSH_CONNECTION.split(' ')[2]
with_dict: "{{ mongodb_nodes }}"
register: master
- debug: var=master.stdout
output:
TASK [ansible-mongodb : --- detect master node ---] ****************
changed: [mongodb-server1] => (item={'value': {ip': u'192.168.190.161', u'primary': True, u'port': 27017},
'key': u'node1'}) =>
{"changed": true, "cmd": "mongo --host 127.0.0.1 --port 27017 --quiet --eval 'db.isMaster().primary'",
"delta": "0:00:00.055407", "end": "2019-02-11 05:49:35.002382",
"item": {"key": "node1", "value": {"hostname": "mongodb","port": 27017, "primary": true, "ip": "192.168.190.161"}},
"rc": 0, "start": "2019-02-11 05:49:34.946975", "stderr": "", "stderr_lines": ,
"stdout": "mongodb1:27017", "stdout_lines": ["mongodb1:27017"]}
TASK [ansible-mongodb : debug] ************************************
ok: [mongodb-server1] => {
"master.stdout": "VARIABLE IS NOT DEFINED!"
}
Why can not the register module save the variable value?
thanks.
ansible
add a comment |
default/main.yml
is:
mongodb_nodes:
node1:
ip: 192.168.190.161
port: 27017
primary: true
node2:
ip: 192.168.190.162
port: 27017
primary: false
id: 2
node3:
ip: 192.168.190.163
port: 27017
primary: false
my tasks/mongodb.yml
is:
- name: --- detect master node ---
shell: >
mongo --host 127.0.0.1 --port 27017
--quiet --eval 'db.isMaster().primary'
when: item.value.primary == true and item.value.ip == ansible_env.SSH_CONNECTION.split(' ')[2]
with_dict: "{{ mongodb_nodes }}"
register: master
- debug: var=master.stdout
output:
TASK [ansible-mongodb : --- detect master node ---] ****************
changed: [mongodb-server1] => (item={'value': {ip': u'192.168.190.161', u'primary': True, u'port': 27017},
'key': u'node1'}) =>
{"changed": true, "cmd": "mongo --host 127.0.0.1 --port 27017 --quiet --eval 'db.isMaster().primary'",
"delta": "0:00:00.055407", "end": "2019-02-11 05:49:35.002382",
"item": {"key": "node1", "value": {"hostname": "mongodb","port": 27017, "primary": true, "ip": "192.168.190.161"}},
"rc": 0, "start": "2019-02-11 05:49:34.946975", "stderr": "", "stderr_lines": ,
"stdout": "mongodb1:27017", "stdout_lines": ["mongodb1:27017"]}
TASK [ansible-mongodb : debug] ************************************
ok: [mongodb-server1] => {
"master.stdout": "VARIABLE IS NOT DEFINED!"
}
Why can not the register module save the variable value?
thanks.
ansible
Something is wrong here. There is no such value "internal_ip" in the dictionary. Ansible should crash with an error. Follow How to create a Minimal, Complete, and Verifiable example.
– Vladimir Botka
Feb 11 at 13:06
@Vladimir Botka I edited, but I have problem yet.
– pyramid13
Feb 11 at 20:26
add a comment |
default/main.yml
is:
mongodb_nodes:
node1:
ip: 192.168.190.161
port: 27017
primary: true
node2:
ip: 192.168.190.162
port: 27017
primary: false
id: 2
node3:
ip: 192.168.190.163
port: 27017
primary: false
my tasks/mongodb.yml
is:
- name: --- detect master node ---
shell: >
mongo --host 127.0.0.1 --port 27017
--quiet --eval 'db.isMaster().primary'
when: item.value.primary == true and item.value.ip == ansible_env.SSH_CONNECTION.split(' ')[2]
with_dict: "{{ mongodb_nodes }}"
register: master
- debug: var=master.stdout
output:
TASK [ansible-mongodb : --- detect master node ---] ****************
changed: [mongodb-server1] => (item={'value': {ip': u'192.168.190.161', u'primary': True, u'port': 27017},
'key': u'node1'}) =>
{"changed": true, "cmd": "mongo --host 127.0.0.1 --port 27017 --quiet --eval 'db.isMaster().primary'",
"delta": "0:00:00.055407", "end": "2019-02-11 05:49:35.002382",
"item": {"key": "node1", "value": {"hostname": "mongodb","port": 27017, "primary": true, "ip": "192.168.190.161"}},
"rc": 0, "start": "2019-02-11 05:49:34.946975", "stderr": "", "stderr_lines": ,
"stdout": "mongodb1:27017", "stdout_lines": ["mongodb1:27017"]}
TASK [ansible-mongodb : debug] ************************************
ok: [mongodb-server1] => {
"master.stdout": "VARIABLE IS NOT DEFINED!"
}
Why can not the register module save the variable value?
thanks.
ansible
default/main.yml
is:
mongodb_nodes:
node1:
ip: 192.168.190.161
port: 27017
primary: true
node2:
ip: 192.168.190.162
port: 27017
primary: false
id: 2
node3:
ip: 192.168.190.163
port: 27017
primary: false
my tasks/mongodb.yml
is:
- name: --- detect master node ---
shell: >
mongo --host 127.0.0.1 --port 27017
--quiet --eval 'db.isMaster().primary'
when: item.value.primary == true and item.value.ip == ansible_env.SSH_CONNECTION.split(' ')[2]
with_dict: "{{ mongodb_nodes }}"
register: master
- debug: var=master.stdout
output:
TASK [ansible-mongodb : --- detect master node ---] ****************
changed: [mongodb-server1] => (item={'value': {ip': u'192.168.190.161', u'primary': True, u'port': 27017},
'key': u'node1'}) =>
{"changed": true, "cmd": "mongo --host 127.0.0.1 --port 27017 --quiet --eval 'db.isMaster().primary'",
"delta": "0:00:00.055407", "end": "2019-02-11 05:49:35.002382",
"item": {"key": "node1", "value": {"hostname": "mongodb","port": 27017, "primary": true, "ip": "192.168.190.161"}},
"rc": 0, "start": "2019-02-11 05:49:34.946975", "stderr": "", "stderr_lines": ,
"stdout": "mongodb1:27017", "stdout_lines": ["mongodb1:27017"]}
TASK [ansible-mongodb : debug] ************************************
ok: [mongodb-server1] => {
"master.stdout": "VARIABLE IS NOT DEFINED!"
}
Why can not the register module save the variable value?
thanks.
ansible
ansible
edited Feb 11 at 20:23
pyramid13
asked Feb 11 at 11:45
pyramid13pyramid13
113515
113515
Something is wrong here. There is no such value "internal_ip" in the dictionary. Ansible should crash with an error. Follow How to create a Minimal, Complete, and Verifiable example.
– Vladimir Botka
Feb 11 at 13:06
@Vladimir Botka I edited, but I have problem yet.
– pyramid13
Feb 11 at 20:26
add a comment |
Something is wrong here. There is no such value "internal_ip" in the dictionary. Ansible should crash with an error. Follow How to create a Minimal, Complete, and Verifiable example.
– Vladimir Botka
Feb 11 at 13:06
@Vladimir Botka I edited, but I have problem yet.
– pyramid13
Feb 11 at 20:26
Something is wrong here. There is no such value "internal_ip" in the dictionary. Ansible should crash with an error. Follow How to create a Minimal, Complete, and Verifiable example.
– Vladimir Botka
Feb 11 at 13:06
Something is wrong here. There is no such value "internal_ip" in the dictionary. Ansible should crash with an error. Follow How to create a Minimal, Complete, and Verifiable example.
– Vladimir Botka
Feb 11 at 13:06
@Vladimir Botka I edited, but I have problem yet.
– pyramid13
Feb 11 at 20:26
@Vladimir Botka I edited, but I have problem yet.
– pyramid13
Feb 11 at 20:26
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499917%2fansible-with-dict-and-register-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499917%2fansible-with-dict-and-register-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Something is wrong here. There is no such value "internal_ip" in the dictionary. Ansible should crash with an error. Follow How to create a Minimal, Complete, and Verifiable example.
– Vladimir Botka
Feb 11 at 13:06
@Vladimir Botka I edited, but I have problem yet.
– pyramid13
Feb 11 at 20:26