ansibleyamlcommandrouter-os

How do I send a variable to community.routeros.command? Ansible


I have a variable:

ok: [192.168.0.2] => {
  "_ip": [
    "192.168.0.3"
]

}

I'm trying to pass it to a command on routeros

- name: Ping
  community.routeros.command:
    commands: 'ping {{_ip}} count 5'
  register: ping

But it turns out to be a mistake

ok: [192.168.0.2] => {
"ping.stdout_lines": [
    [
        "p",
        "",
        "expected command name (line 1 column 7)"
    ]
]

}

I looked at what the error might be, it gave the following:

changed: [192.168.176.129] => {
"changed": true,
"invocation": {
    "module_args": {
        "commands": [
            "ping ['192.168.176.128'] count 5"
        ],
        "interval": 1,
        "match": "all",
        "retries": 10,
        "wait_for": null
    }
},
"stdout": [
    "p\n\nexpected command name (line 1 column 7)"
],
"stdout_lines": [
    [
        "p",
        "",
        "expected command name (line 1 column 7)"
    ]
]

}

How to fix it?


Solution

  • following your error, its seems _ip is a list, so

    try:

    commands: 'ping {{_ip.0}} count 5'