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













0















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?










share|improve this question


























    0















    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?










    share|improve this question
























      0












      0








      0








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Timothy PulliamTimothy Pulliam

      1,3951025




      1,3951025




















          1 Answer
          1






          active

          oldest

          votes


















          1














          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 "





          share|improve this answer























            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
            );



            );













            draft saved

            draft discarded


















            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









            1














            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 "





            share|improve this answer



























              1














              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 "





              share|improve this answer

























                1












                1








                1







                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 "





                share|improve this answer













                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 "






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                Timothy PulliamTimothy Pulliam

                1,3951025




                1,3951025



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                    Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                    Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.