Ansible: Iterate over variables and run command if variable is definedList ALL Ansible variables for a host or group with an ad hoc command?Ansible variables scope clashansible pass shell command result to variableAnsible array variableseparator value from fact variable in ansibleansible variable as environment variableBuild Ansible inventory based on server variablesHow to specify range to iterate through an array in ansibleAnsible Playbook: Get value from variableAnsible task for searching words
Why doesn't using multiple commands with a || or && conditional work?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Is it possible to create a QR code using text?
Is "remove commented out code" correct English?
Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?
iPad being using in wall mount battery swollen
How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)
Little known, relatively unlikely, but scientifically plausible, apocalyptic (or near apocalyptic) events
What is a romance in Latin?
What about the virus in 12 Monkeys?
Would Slavery Reparations be considered Bills of Attainder and hence Illegal?
Detention in 1997
How do conventional missiles fly?
Why is consensus so controversial in Britain?
How to tell a function to use the default argument values?
Solving a recurrence relation (poker chips)
CAST throwing error when run in stored procedure but not when run as raw query
Intersection Puzzle
I would say: "You are another teacher", but she is a woman and I am a man
Watching something be piped to a file live with tail
Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?
Is it acceptable for a professor to tell male students to not think that they are smarter than female students?
How can saying a song's name be a copyright violation?
Is there a hemisphere-neutral way of specifying a season?
Ansible: Iterate over variables and run command if variable is defined
List ALL Ansible variables for a host or group with an ad hoc command?Ansible variables scope clashansible pass shell command result to variableAnsible array variableseparator value from fact variable in ansibleansible variable as environment variableBuild Ansible inventory based on server variablesHow to specify range to iterate through an array in ansibleAnsible Playbook: Get value from variableAnsible task for searching words
I want to set nameservers in /etc/resolv.conf using ansible. I basically want to set variables (DNS1, DNS2, DNS3). I only want to apply DNS# if it is defined. I have the following so far.
# Run this playbook on all hosts that should query the DNS server.
- hosts: all
vars:
# dns_server: 192.168.1.190
nameserver_ip: 192.168.1.214
DNS2: 192.168.1.1
tasks:
- name: Add DNS server's IPv4 address to /etc/resolv.conf
command: "nmcli con mod ansible_default_ipv4['interface'] ipv4.dns nameserver_ip "
- name: Add non-authoritative DNS servers to /etc/resolv.conf
shell: "nmcli con mod ansible_default_ipv4['interface'] +ipv4.dns item "
when: item is defined
with_items:
- DNS2
- DNS3
- name: Restart default network interface to update /etc/resolv.conf
shell: "nmcli con reload && nmcli con up ansible_default_ipv4['interface'] "
However, when I run this I get the following error
[root@ns1 dns]# ansible-playbook --user root -i ftp.home, dns_client.yaml -k
...
...
TASK [Add non-authoritative DNS servers to /etc/resolv.conf] *****************************************************************************************************************************************************
failed: [ftp.home] (item=DNS2) => "changed": true, "cmd": "nmcli con mod eth0 +ipv4.dns DNS2", "delta": "0:00:00.055982", "end": "2019-04-01 12:25:53.029983", "item": "DNS2", "msg": "non-zero return code", "rc": 2, "start": "2019-04-01 12:25:52.974001", "stderr": "Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS2'.", "stderr_lines": ["Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS2'."], "stdout": "", "stdout_lines": []
failed: [ftp.home] (item=DNS3) => "changed": true, "cmd": "nmcli con mod eth0 +ipv4.dns DNS3", "delta": "0:00:00.056684", "end": "2019-04-01 12:25:53.782999", "item": "DNS3", "msg": "non-zero return code", "rc": 2, "start": "2019-04-01 12:25:53.726315", "stderr": "Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS3'.", "stderr_lines": ["Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS3'."], "stdout": "", "stdout_lines": []
to retry, use: --limit @/root/ansible/dns/dns_client.retry
It seems that, instead of using the value of DNS2,3 it is just using the variable names (literally) DNS2 and DNS3. What am I doing wrong here?
ansible
add a comment |
I want to set nameservers in /etc/resolv.conf using ansible. I basically want to set variables (DNS1, DNS2, DNS3). I only want to apply DNS# if it is defined. I have the following so far.
# Run this playbook on all hosts that should query the DNS server.
- hosts: all
vars:
# dns_server: 192.168.1.190
nameserver_ip: 192.168.1.214
DNS2: 192.168.1.1
tasks:
- name: Add DNS server's IPv4 address to /etc/resolv.conf
command: "nmcli con mod ansible_default_ipv4['interface'] ipv4.dns nameserver_ip "
- name: Add non-authoritative DNS servers to /etc/resolv.conf
shell: "nmcli con mod ansible_default_ipv4['interface'] +ipv4.dns item "
when: item is defined
with_items:
- DNS2
- DNS3
- name: Restart default network interface to update /etc/resolv.conf
shell: "nmcli con reload && nmcli con up ansible_default_ipv4['interface'] "
However, when I run this I get the following error
[root@ns1 dns]# ansible-playbook --user root -i ftp.home, dns_client.yaml -k
...
...
TASK [Add non-authoritative DNS servers to /etc/resolv.conf] *****************************************************************************************************************************************************
failed: [ftp.home] (item=DNS2) => "changed": true, "cmd": "nmcli con mod eth0 +ipv4.dns DNS2", "delta": "0:00:00.055982", "end": "2019-04-01 12:25:53.029983", "item": "DNS2", "msg": "non-zero return code", "rc": 2, "start": "2019-04-01 12:25:52.974001", "stderr": "Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS2'.", "stderr_lines": ["Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS2'."], "stdout": "", "stdout_lines": []
failed: [ftp.home] (item=DNS3) => "changed": true, "cmd": "nmcli con mod eth0 +ipv4.dns DNS3", "delta": "0:00:00.056684", "end": "2019-04-01 12:25:53.782999", "item": "DNS3", "msg": "non-zero return code", "rc": 2, "start": "2019-04-01 12:25:53.726315", "stderr": "Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS3'.", "stderr_lines": ["Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS3'."], "stdout": "", "stdout_lines": []
to retry, use: --limit @/root/ansible/dns/dns_client.retry
It seems that, instead of using the value of DNS2,3 it is just using the variable names (literally) DNS2 and DNS3. What am I doing wrong here?
ansible
add a comment |
I want to set nameservers in /etc/resolv.conf using ansible. I basically want to set variables (DNS1, DNS2, DNS3). I only want to apply DNS# if it is defined. I have the following so far.
# Run this playbook on all hosts that should query the DNS server.
- hosts: all
vars:
# dns_server: 192.168.1.190
nameserver_ip: 192.168.1.214
DNS2: 192.168.1.1
tasks:
- name: Add DNS server's IPv4 address to /etc/resolv.conf
command: "nmcli con mod ansible_default_ipv4['interface'] ipv4.dns nameserver_ip "
- name: Add non-authoritative DNS servers to /etc/resolv.conf
shell: "nmcli con mod ansible_default_ipv4['interface'] +ipv4.dns item "
when: item is defined
with_items:
- DNS2
- DNS3
- name: Restart default network interface to update /etc/resolv.conf
shell: "nmcli con reload && nmcli con up ansible_default_ipv4['interface'] "
However, when I run this I get the following error
[root@ns1 dns]# ansible-playbook --user root -i ftp.home, dns_client.yaml -k
...
...
TASK [Add non-authoritative DNS servers to /etc/resolv.conf] *****************************************************************************************************************************************************
failed: [ftp.home] (item=DNS2) => "changed": true, "cmd": "nmcli con mod eth0 +ipv4.dns DNS2", "delta": "0:00:00.055982", "end": "2019-04-01 12:25:53.029983", "item": "DNS2", "msg": "non-zero return code", "rc": 2, "start": "2019-04-01 12:25:52.974001", "stderr": "Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS2'.", "stderr_lines": ["Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS2'."], "stdout": "", "stdout_lines": []
failed: [ftp.home] (item=DNS3) => "changed": true, "cmd": "nmcli con mod eth0 +ipv4.dns DNS3", "delta": "0:00:00.056684", "end": "2019-04-01 12:25:53.782999", "item": "DNS3", "msg": "non-zero return code", "rc": 2, "start": "2019-04-01 12:25:53.726315", "stderr": "Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS3'.", "stderr_lines": ["Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS3'."], "stdout": "", "stdout_lines": []
to retry, use: --limit @/root/ansible/dns/dns_client.retry
It seems that, instead of using the value of DNS2,3 it is just using the variable names (literally) DNS2 and DNS3. What am I doing wrong here?
ansible
I want to set nameservers in /etc/resolv.conf using ansible. I basically want to set variables (DNS1, DNS2, DNS3). I only want to apply DNS# if it is defined. I have the following so far.
# Run this playbook on all hosts that should query the DNS server.
- hosts: all
vars:
# dns_server: 192.168.1.190
nameserver_ip: 192.168.1.214
DNS2: 192.168.1.1
tasks:
- name: Add DNS server's IPv4 address to /etc/resolv.conf
command: "nmcli con mod ansible_default_ipv4['interface'] ipv4.dns nameserver_ip "
- name: Add non-authoritative DNS servers to /etc/resolv.conf
shell: "nmcli con mod ansible_default_ipv4['interface'] +ipv4.dns item "
when: item is defined
with_items:
- DNS2
- DNS3
- name: Restart default network interface to update /etc/resolv.conf
shell: "nmcli con reload && nmcli con up ansible_default_ipv4['interface'] "
However, when I run this I get the following error
[root@ns1 dns]# ansible-playbook --user root -i ftp.home, dns_client.yaml -k
...
...
TASK [Add non-authoritative DNS servers to /etc/resolv.conf] *****************************************************************************************************************************************************
failed: [ftp.home] (item=DNS2) => "changed": true, "cmd": "nmcli con mod eth0 +ipv4.dns DNS2", "delta": "0:00:00.055982", "end": "2019-04-01 12:25:53.029983", "item": "DNS2", "msg": "non-zero return code", "rc": 2, "start": "2019-04-01 12:25:52.974001", "stderr": "Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS2'.", "stderr_lines": ["Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS2'."], "stdout": "", "stdout_lines": []
failed: [ftp.home] (item=DNS3) => "changed": true, "cmd": "nmcli con mod eth0 +ipv4.dns DNS3", "delta": "0:00:00.056684", "end": "2019-04-01 12:25:53.782999", "item": "DNS3", "msg": "non-zero return code", "rc": 2, "start": "2019-04-01 12:25:53.726315", "stderr": "Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS3'.", "stderr_lines": ["Error: failed to modify ipv4.dns: invalid IPv4 address 'DNS3'."], "stdout": "", "stdout_lines": []
to retry, use: --limit @/root/ansible/dns/dns_client.retry
It seems that, instead of using the value of DNS2,3 it is just using the variable names (literally) DNS2 and DNS3. What am I doing wrong here?
ansible
ansible
asked 2 days ago
Timothy PulliamTimothy Pulliam
1,3951025
1,3951025
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I figured it out. I forgot that in order to used the value of a variable in Ansible you must surround the variable name in " ... ". The following changes fixed my issue.
with_items:
- " DNS2 "
- " DNS3 "
add a comment |
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%2f509909%2fansible-iterate-over-variables-and-run-command-if-variable-is-defined%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I figured it out. I forgot that in order to used the value of a variable in Ansible you must surround the variable name in " ... ". The following changes fixed my issue.
with_items:
- " DNS2 "
- " DNS3 "
add a comment |
I figured it out. I forgot that in order to used the value of a variable in Ansible you must surround the variable name in " ... ". The following changes fixed my issue.
with_items:
- " DNS2 "
- " DNS3 "
add a comment |
I figured it out. I forgot that in order to used the value of a variable in Ansible you must surround the variable name in " ... ". The following changes fixed my issue.
with_items:
- " DNS2 "
- " DNS3 "
I figured it out. I forgot that in order to used the value of a variable in Ansible you must surround the variable name in " ... ". The following changes fixed my issue.
with_items:
- " DNS2 "
- " DNS3 "
answered 2 days ago
Timothy PulliamTimothy Pulliam
1,3951025
1,3951025
add a comment |
add a comment |
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%2f509909%2fansible-iterate-over-variables-and-run-command-if-variable-is-defined%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