Answers: <p>Elaborating on @030's answer, here's how you can use variables for modules:</p>
  
  <ol>
  <li><p>You need to group the target hosts in groups, for the example I'm using <code>server1.example.com</code> as member of <code>hosts1</code> group and <code>server2.example.com</code> as member of <code>hosts2</code> group.</p></li>
  <li><p>You create group variables:</p>
  
  <ul>
  <li>group_vars/hosts1</li>
  <li>group_vars/hosts2</li>
  </ul></li>
  </ol>
  
  <p>Both files contain a variable named <code>module</code> with different value, in the test I use <code>stat</code>.</p>
  
  <ol start="3">
  <li>Your playbook will look like this:</li>
  </ol>
  
  <p><code>test.yml</code></p>
  
  <pre><code>---
   - hosts: all        
     user: me        
     tasks:
       - name: test module variables
         action: "{{ module }} path=/home/me/myfile"
  </code></pre>
  
  <p>The result of the play:</p>
  
  <pre><code>[me@myserver]$ ansible-playbook test.yml -i test
  
  PLAY [all] ********************************************************************************
  
  TASK [Gathering Facts] ********************************************************************
  ok: [server1.example.com]
  ok: [server2.example.com]
  
  TASK [test module variables] **************************************************************
  ok: [server1.example.com]
  ok: [server2.example.com]
  
  PLAY RECAP ********************************************************************************
  server1.example.com        : ok=2    changed=0    unreachable=0    failed=0
  server2.example.com        : ok=2    changed=0    unreachable=0    failed=0
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-09T21:31:47.347Z
Answers: "Action" is what I was looking for, thanks.
Answers: Matt W
Answers: CC BY-SA 3.0
Answers: 2018-02-09T20:32:27.213Z
Answers: 2018-02-10T06:18:23.063Z
Answers: 2018-02-10T06:18:23.063Z
Answers: 13dimitar
Answers: <p>It is possible to declare group and host vars <a href="http://docs.ansible.com/ansible/latest/playbooks_best_practices.html" rel="nofollow noreferrer">http://docs.ansible.com/ansible/latest/playbooks_best_practices.html</a></p>
  
  <blockquote>
  <pre><code>group_vars/
     group1                 # here we assign variables to particular groups
     group2                 # ""
  host_vars/
     hostname1              # if systems need specific variables, put them here
     hostname2              # ""
  </code></pre>
  </blockquote>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-09T21:32:54.953Z
Answers: While this is correct, and thanks for the help, this doesn't answer the question. I understand how to add group, variables, and group variables. What I want is to execute a different module *based* on those group variables.
Answers: Matt W
Answers: CC BY-SA 3.0
Answers: 2018-02-09T21:54:17.900Z
Answers: module or role?
Answers: 030
Answers: CC BY-SA 4.0
Answers: 2018-11-21T21:03:52.837Z
Answers: @030 Module, like `yum` or `apt`.  See the accepted answer.
Answers: DylanYoung
Answers: CC BY-SA 3.0
Answers: 2018-02-09T18:55:13.740Z
Answers: 2018-02-09T18:55:13.740Z
Answers: 030
Body: <p>There are many Ansible Network modules (for example: ios_user, asa_user, nxos_user) which share the same (or similar enough) interfaces. Currently, my playbook has to conditionally execute tasks based on the host's group or variables. This leads to a lot of duplicated code as the only real difference between the tasks is the module name.</p>
  
  <p><strong>Is there a way to select the module to be run based on group or variable values</strong> without adding extra tasks, roles, or playbooks?</p>
  
  <p><em>For example, this is how the playbook currently looks:</em></p>
  
  <pre><code>- hosts: ios
    tasks:
      ios_user:
        provider: "{{ cli }}"
        ...
  - hosts: asa
    tasks:
      asa_user:
        provider: "{{ cli }}"
        ...
  - hosts: nxos
    tasks:
      nxos_user:
        provider: "{{ cli }}"
        ...
  </code></pre>
  
  <p><em>I'd love to have something like this:</em></p>
  
  <pre><code>- hosts: all
    tasks:
      {{ module_name }}:
        provider: "{{ cli }}"
        ...
  </code></pre>
  
  <p>I swear I saw some feature like this in passing, but I'm having a hard time finding it again. Along with version 2.5 making netconf_cli a first-class connection type, this would solve a large number of problems and inconveniences in our org.</p>
  
Tags: <ansible>
Title: Change Ansible module based on variable or group
UserDisplayName: Matt W
ThreadText: # Change Ansible module based on variable or group
  
  (Asked by: username_0 on 2018-02-09)
  
  There are many Ansible Network modules (for example: ios\_user, asa\_user, nxos\_user) which share the same (or similar enough) interfaces. Currently, my playbook has to conditionally execute tasks based on the host's group or variables. This leads to a lot of duplicated code as the only real difference between the tasks is the module name.
  
  **Is there a way to select the module to be run based on group or variable values** without adding extra tasks, roles, or playbooks?
  
  *For example, this is how the playbook currently looks:*
  
  ```
  - hosts: ios
    tasks:
      ios_user:
        provider: "{{ cli }}"
        ...
  - hosts: asa
    tasks:
      asa_user:
        provider: "{{ cli }}"
        ...
  - hosts: nxos
    tasks:
      nxos_user:
        provider: "{{ cli }}"
        ...
  
  ```
  
  *I'd love to have something like this:*
  
  ```
  - hosts: all
    tasks:
      {{ module_name }}:
        provider: "{{ cli }}"
        ...
  
  ```
  
  I swear I saw some feature like this in passing, but I'm having a hard time finding it again. Along with version 2.5 making netconf\_cli a first-class connection type, this would solve a large number of problems and inconveniences in our org.
  
  **Tags:** ansible
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-02-09. Score: 4 | Accepted answer
  
  Elaborating on @username_2's answer, here's how you can use variables for modules:
  
  1. You need to group the target hosts in groups, for the example I'm using `server1.example.com` as member of `hosts1` group and `server2.example.com` as member of `hosts2` group.
  2. You create group variables:
  
     * group\_vars/hosts1
     * group\_vars/hosts2
  
  Both files contain a variable named `module` with different value, in the test I use `stat`.
  
  3. Your playbook will look like this:
  
  `test.yml`
  
  ```
  ---
   - hosts: all        
     user: me        
     tasks:
       - name: test module variables
         action: "{{ module }} path=/home/me/myfile"
  
  ```
  
  The result of the play:
  
  ```
  [me@myserver]$ ansible-playbook test.yml -i test
  
  PLAY [all] ********************************************************************************
  
  TASK [Gathering Facts] ********************************************************************
  ok: [server1.example.com]
  ok: [server2.example.com]
  
  TASK [test module variables] **************************************************************
  ok: [server1.example.com]
  ok: [server2.example.com]
  
  PLAY RECAP ********************************************************************************
  server1.example.com        : ok=2    changed=0    unreachable=0    failed=0
  server2.example.com        : ok=2    changed=0    unreachable=0    failed=0
  
  ```
  
  > **Comments:**
  > **username_0**: "Action" is what I was looking for, thanks.  
  
  ---
  
  ### Answer by username_2 on 2018-02-09. Score: 2
  
  It is possible to declare group and host vars <http://docs.ansible.com/ansible/latest/playbooks_best_practices.html>
  
  > ```
  > group_vars/
  >    group1                 # here we assign variables to particular groups
  >    group2                 # ""
  > host_vars/
  >    hostname1              # if systems need specific variables, put them here
  >    hostname2              # ""
  >
  > ```
  
  > **Comments:**
  > **username_0**: While this is correct, and thanks for the help, this doesn't answer the question. I understand how to add group, variables, and group variables. What I want is to execute a different module *based* on those group variables.  
  > **username_2**: module or role?  
  > **username_3**: @username_2 Module, like `yum` or `apt`.  See the accepted answer.  
  
  ---
  
  