Answers: <p>I understand that you want to <strong>customize your connection depending on the host</strong>.
  Does this fit your requirements ?</p>
  <pre class="lang-ini prettyprint-override"><code>[all]
  localhost ansible_connection=local ansible_become=yes
  server1 ansible_host=192.168.1.10 ansible_user=admin ansible_port=2222 ansible_ssh_common_args='-o ProxyJump=jumphost'
  container1 ansible_connection=docker
  </code></pre>
  <h3>EDIT</h3>
  <ul>
  <li>To apply different settings based on the <strong>controller</strong> host, you can use <strong>conditionals</strong> in your playbooks or <strong>dynamic inventory scripts</strong> to adjust the <strong>connection parameters</strong> accordingly.</li>
  </ul>
  <h4>Conditional parameters</h4>
  <pre><code>- hosts: all
    tasks:
      - name: Example task for localhost
        debug:
          msg: &quot;This is the localhost&quot;
        when: inventory_hostname == 'localhost'
  
      - name: Example task for server1
        debug:
          msg: &quot;This is server1&quot;
        when: inventory_hostname == 'server1'
  
      - name: Example task for container1
        debug:
          msg: &quot;This is container1&quot;
        when: inventory_hostname == 'container1'
  </code></pre>
  <h4>Dynamic inventory</h4>
  <pre><code>#!/usr/bin/env python
  
  import json
  
  inventory = {
      'all': {
          'hosts': ['localhost', 'server1', 'container1'],
          'vars': {
              'ansible_become': 'yes'
          }
      },
      '_meta': {
          'hostvars': {
              'localhost': {
                  'ansible_connection': 'local'
              },
              'server1': {
                  'ansible_host': '192.168.1.10',
                  'ansible_user': 'admin',
                  'ansible_port': 2222,
                  'ansible_ssh_common_args': '-o ProxyJump=jumphost'
              },
              'container1': {
                  'ansible_connection': 'docker'
              }
          }
      }
  }
  
  print(json.dumps(inventory, indent=2))
  
  </code></pre>
  <ul>
  <li>Make the script executable then run it using Ansible.</li>
  </ul>
  <pre><code>chmod +x dynamic_inventory.py
  ansible-playbook -i dynamic_inventory.py playbook.yml
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-08-23T16:24:35.917Z
Answers: Ok, it was really not very obvious that it should depend on the controller host. The second idea is to create different playbooks, that makes this whole thing also playbook-dependent, I only want it to depend on the controller host. I do not want to play this with all my playbooks. I think, your third idea is already some really usable. It needs an integration with the external caller infrastructure of the ansible, but that is not a big problem. Thanks!
Answers: peterh
Answers: CC BY-SA 4.0
Answers: 2024-08-23T16:30:38.767Z
Answers: Wait, I think I have a very nice idea! **How about if the overlay vars (`.meta.hostvars.*` in your example) would be somehow integrated into the host_vars?** I think, somehow a general inventory dumper script should be written, and then its output could be trivially modified? On this way, `host_vars/server1.yml` could contain, what to do differently if ansible is executed on server1.
Answers: peterh
Answers: CC BY-SA 4.0
Answers: 2024-08-18T20:47:25.537Z
Answers: 2024-08-19T17:39:27.723Z
Answers: 2024-08-19T17:39:27.723Z
Answers: YassineLbk
Answers: <p>My current best bet is like this:</p>
  <pre><code>ansible_connection: &quot;{{ 'local' if lookup('pipe', 'hostname') == 'myclient1' else 'ssh' }}&quot;
  </code></pre>
  <p>That will use the <code>local</code> connection plugin on <code>myclient1</code>, and ssh elsewhere.</p>
  <p>I have no better idea, how could I somehow generate some like a host-specific override snippet to the host variables in general.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-08-23T16:26:07.043Z
Answers: Finally I do @YassineLbk's third idea, but I use yaml instead of json. Thus, ansible is not really loving it with same native feature, but it can be done well by extending some external caller inftrastructure.
Answers: peterh
Answers: CC BY-SA 4.0
Answers: 2024-08-23T16:15:18.990Z
Answers: 2024-08-23T16:57:22.793Z
Answers: 2024-08-23T16:57:22.793Z
Answers: peterh
Body: <p>There are various reasons where I would like to execute ansible on many different systems, in different configurations. Some example:</p>
  <ul>
  <li>Executing ansible on one of the hosts existing in the inventory, a &quot;local&quot; connection with a sudo &quot;become&quot; would be better than a self-connecting ssh.</li>
  <li>Various hosts might have different firewall rules to handle (thus, ssh jumphosts or non-standard ports might be needed where usually an ordinary ssh connection would be enough)</li>
  <li>Similarly in a virtualization/containerization structure, various local rpc tools might preferred, maybe even their chaining (like a &quot;docker exec&quot; or &quot;oc rsh&quot;), also in position-different configurations.</li>
  </ul>
  <p>What is the best to handle this? I think, somehow the ansible connection variables should be dependent on the executing hosts.</p>
  
Tags: <ansible>
Title: How could I make ansible connection parameters dependent on the controller host?
UserDisplayName: peterh
ThreadText: # How could I make ansible connection parameters dependent on the controller host?
  
  (Asked by: username_0 on 2024-08-18)
  
  There are various reasons where I would like to execute ansible on many different systems, in different configurations. Some example:
  
  * Executing ansible on one of the hosts existing in the inventory, a "local" connection with a sudo "become" would be better than a self-connecting ssh.
  * Various hosts might have different firewall rules to handle (thus, ssh jumphosts or non-standard ports might be needed where usually an ordinary ssh connection would be enough)
  * Similarly in a virtualization/containerization structure, various local rpc tools might preferred, maybe even their chaining (like a "docker exec" or "oc rsh"), also in position-different configurations.
  
  What is the best to handle this? I think, somehow the ansible connection variables should be dependent on the executing hosts.
  
  **Tags:** ansible
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2024-08-18. Score: 1 | Accepted answer
  
  I understand that you want to **customize your connection depending on the host**.
  Does this fit your requirements ?
  
  ```ini
  [all]
  localhost ansible_connection=local ansible_become=yes
  server1 ansible_host=192.168.1.10 ansible_user=admin ansible_port=2222 ansible_ssh_common_args='-o ProxyJump=jumphost'
  container1 ansible_connection=docker
  
  ```
  
  ### EDIT
  
  * To apply different settings based on the **controller** host, you can use **conditionals** in your playbooks or **dynamic inventory scripts** to adjust the **connection parameters** accordingly.
  
  #### Conditional parameters
  
  ```
  - hosts: all
    tasks:
      - name: Example task for localhost
        debug:
          msg: "This is the localhost"
        when: inventory_hostname == 'localhost'
  
      - name: Example task for server1
        debug:
          msg: "This is server1"
        when: inventory_hostname == 'server1'
  
      - name: Example task for container1
        debug:
          msg: "This is container1"
        when: inventory_hostname == 'container1'
  
  ```
  
  #### Dynamic inventory
  
  ```
  #!/usr/bin/env python
  
  import json
  
  inventory = {
      'all': {
          'hosts': ['localhost', 'server1', 'container1'],
          'vars': {
              'ansible_become': 'yes'
          }
      },
      '_meta': {
          'hostvars': {
              'localhost': {
                  'ansible_connection': 'local'
              },
              'server1': {
                  'ansible_host': '192.168.1.10',
                  'ansible_user': 'admin',
                  'ansible_port': 2222,
                  'ansible_ssh_common_args': '-o ProxyJump=jumphost'
              },
              'container1': {
                  'ansible_connection': 'docker'
              }
          }
      }
  }
  
  print(json.dumps(inventory, indent=2))
  
  ```
  
  * Make the script executable then run it using Ansible.
  
  ```
  chmod +x dynamic_inventory.py
  ansible-playbook -i dynamic_inventory.py playbook.yml
  
  ```
  
  > **Comments:**
  > **username_0**: Ok, it was really not very obvious that it should depend on the controller host. The second idea is to create different playbooks, that makes this whole thing also playbook-dependent, I only want it to depend on the controller host. I do not want to play this with all my playbooks. I think, your third idea is already some really usable. It needs an integration with the external caller infrastructure of the ansible, but that is not a big problem. Thanks!  
  > **username_0**: Wait, I think I have a very nice idea! **How about if the overlay vars (`.meta.hostvars.*` in your example) would be somehow integrated into the host_vars?** I think, somehow a general inventory dumper script should be written, and then its output could be trivially modified? On this way, `host_vars/server1.yml` could contain, what to do differently if ansible is executed on server1.  
  
  ---
  
  ### Answer by username_0 on 2024-08-23. Score: 0
  
  My current best bet is like this:
  
  ```
  ansible_connection: "{{ 'local' if lookup('pipe', 'hostname') == 'myclient1' else 'ssh' }}"
  
  ```
  
  That will use the `local` connection plugin on `myclient1`, and ssh elsewhere.
  
  I have no better idea, how could I somehow generate some like a host-specific override snippet to the host variables in general.
  
  > **Comments:**
  > **username_0**: Finally I do @username_1's third idea, but I use yaml instead of json. Thus, ansible is not really loving it with same native feature, but it can be done well by extending some external caller inftrastructure.  
  
  ---
  
  