Can i use .yml file like this?

tasks:

  • name: Variable
    set_fact:
    os: “ubi”
    host: “192.168.0.137”
    username: “root”
    password: “premier”
    commands: “show version”
    module: “ios_command”

############### Working ############

  • name: Cisco ios command
    ios_command:
    host: “{{host}}”
    username: “{{username}}”
    password: “{{password}}”
    commands: “{{commands}}”

############## Not Working ###########

  • name: Cisco ios command
    “{{module}}”: < – This occur error message.
    host: “{{host}}”
    username: “{{username}}”
    password: “{{password}}”
    commands: “{{commands}}”

I want to make dynamic yml file like above, But This is not working.

If you were me, How could you do that?

You could perhaps use template to generate the playbook file that you want to use, then run the generated playbook.

I don’t know what alternative value for module might be, but you could perhaps use when: condition to only run ios_command if some condition is true. Something like this (not tested):

  • name: Cisco ios command

ios_command:
host: “{{host}}”
username: “{{username}}”
password: “{{password}}”
commands: “{{commands}}”
when: “‘cisco’ in group_names”

  • name: Non cisco ios command

a_different_command:
host: “{{host}}”
username: “{{username}}”
password: “{{password}}”
commands: “{{commands}}”
when: “‘cisco’ not in group_names”

Hope this helps.

Jon

I believe this is what you want:

- name: Cisco ios command
    action:
      module: "{{module}}"
      host: "{{host}}"
      username: "{{username}}"
      password: "{{password}}"
      commands: "{{commands}}"

Thank you ! Brian Coca !

2017년 6월 16일 금요일 오전 12시 40분 58초 UTC+9, Brian Coca 님의 말: