Switching to hostnames leads to "Could not match supplied host pattern"

Hello!
after switching to hostnames in awx inventory and added ip via ansible_host var, the playbook cannot match them anymore.
There is an survey for the two hosts
Playbook:

---
- name: Generate SSH keypair on remote hosts and distribute keys
  hosts:
    - "{{ sql_dest_srv }}"  
    - "{{ sql_src_srv }}"   
  
  become: true

  tasks:
    - name: Output the value of sql_src_srv
      ansible.builtin.debug:
        msg: "The value of sql_src_srv is: {{ sql_src_srv }}"

    - name: Output the value of sql_dest_srv
      ansible.builtin.debug:
        msg: "The value of sql_dest_srv is: {{ sql_dest_srv }}"

- name: Create SSH keypair in /tmp
  hosts: localhost

  tasks:
    - name: Create SSH keypair in /tmp
      ansible.builtin.openssh_keypair:
        path: /tmp/id_rsa_remcpyusr
        type: rsa
        force: true
        comment: "remcpyusr"
      run_once: true

- name: Distribute the SSH public key
  hosts: sql_src_srv

  tasks:
    - name: Distribute the SSH public key
      ansible.builtin.copy:
        src: /tmp/id_rsa_remcpyusr.pub
        dest: /home/remcpyusr/.ssh/authorized_keys
        owner: remcpyusr
        mode: '0644'
        force: yes

- name: Distribute the SSH private key
  hosts: sql_dest_srv

  tasks:
    - name: Distribute the SSH private key
      ansible.builtin.copy:
        src: /tmp/id_rsa_remcpyusr
        dest: /home/remcpyusr/.ssh/id_rsa
        owner: remcpyusr
        mode: '0600'
        force: yes

- name: Ensure key pair is absent on the controller and hosts
  hosts: all

  tasks:
    - name: Ensure key pair is absent on the controller and hosts
      ansible.builtin.file:
        path: "{{ item }}"
        state: absent
      loop:
        - /tmp/id_rsa_remcpyusr
        - /tmp/id_rsa_remcpyusr.pub
        - /home/remcpyusr/.ssh/authorized_keys
        - /home/remcpyusr/.ssh/id_rsa

Log:

Enter passphrase for /runner/artifacts/7145/ssh_key_data: 
Identity added: /runner/artifacts/7145/ssh_key_data (/runner/artifacts/7145/ssh_key_data)
No config file found; using defaults

PLAY [Generate SSH keypair on remote hosts and distribute keys] ****************

TASK [Gathering Facts] *********************************************************
ok: [testsrv-02]
ok: [testsrv-01]

TASK [Output the value of sql_src_srv] *****************************************
ok: [testsrv-02] => {
    "msg": "The value of sql_src_srv is: testsrv-01"
}
ok: [testsrv-01] => {
    "msg": "The value of sql_src_srv is: testsrv-01"
}

TASK [Output the value of sql_dest_srv] ****************************************
ok: [testsrv-02] => {
    "msg": "The value of sql_dest_srv is: testsrv-02"
}
ok: [testsrv-01] => {
    "msg": "The value of sql_dest_srv is: testsrv-02"
}

PLAY [Create SSH keypair in /tmp] **********************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Create SSH keypair in /tmp] **********************************************
changed: [localhost] => {"changed": true, "comment": "remcpyusr", "filename": "/tmp/id_rsa_remcpyusr", "fingerprint": "SHA256:R7mlu1QA3BZa53+iA1tv8zGKjqVMAiDIEkl7BNBmoIY", "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCyjbIE9/VvkyqeoRRfA24C1Es580kodNpH6WCrOfmXZuNZEA183cnanMIlk42IxGSTqotdf/LrUTRBKhmoHrxbYQmhdBkF2Se0mRSr0g5vtFocEh2OmYPheqQZ9XZhtMX4eaw+l2qnpYijVBMI2+s+1cIbCzfKGzt1VomI2FdaA8gcFzrdGljCQn4Xhy/haJIQWTiBKMSrp/tpXmS14cp7vqf0SpqB8NmEoSBhG2470q3Q3u5AuVdrpQ4bV/pbGTW+Zz/IixGYVeDoQFiDyj//TIdv+pQRu+cyKGumKexgzqtxnuMNfsk/4cA9ZEy01TczDuQcYyd5G4vDOEhttxp0n/cPGc25F2t5ipbRHUFf91aYe07Dzh9BOMgxNnIpax9Ls/Toswg3/qkc5cYIJczGrvYLu9mLfJm6B2ls+QxFtIUcoSdnIm0VDBopC5BlB6V1G0lS8CcdKyyXHP5AzHf7qjoITfqRqKSfDdFB3Qmn9GiiJitdDF7UQWE5V6V/9XXJ4EkNKWw9rRNtG1whLVxsb+U7okVDOfOpAwUz/gUr2PmeHtr8cNVouYX9GSLRc62ASXi8A106MY++b+joOz2R5/viRneN9rfmv8MBlMKirQ3Iw98U7sbcAPVlNWf9Rgtlw10BTQvl2WauvVIlwdQPa/LWzZS7+JufJ5pZlOdymw==", "size": 4096, "type": "rsa"}
[WARNING]: Could not match supplied host pattern, ignoring: sql_src_srv

PLAY [Distribute the SSH public key] *******************************************
skipping: no hosts matched
[WARNING]: Could not match supplied host pattern, ignoring: sql_dest_srv

PLAY [Distribute the SSH private key] ******************************************
skipping: no hosts matched

PLAY [Ensure key pair is absent on the controller and hosts] *******************

TASK [Gathering Facts] *********************************************************
ok: [testsrv-02]
ok: [testsrv-01]

TASK [Ensure key pair is absent on the controller and hosts] *******************
ok: [testsrv-01] => (item=/tmp/id_rsa_remcpyusr) => {"ansible_loop_var": "item", "changed": false, "item": "/tmp/id_rsa_remcpyusr", "path": "/tmp/id_rsa_remcpyusr", "state": "absent"}
ok: [testsrv-02] => (item=/tmp/id_rsa_remcpyusr) => {"ansible_loop_var": "item", "changed": false, "item": "/tmp/id_rsa_remcpyusr", "path": "/tmp/id_rsa_remcpyusr", "state": "absent"}
ok: [testsrv-01] => (item=/tmp/id_rsa_remcpyusr.pub) => {"ansible_loop_var": "item", "changed": false, "item": "/tmp/id_rsa_remcpyusr.pub", "path": "/tmp/id_rsa_remcpyusr.pub", "state": "absent"}
ok: [testsrv-02] => (item=/tmp/id_rsa_remcpyusr.pub) => {"ansible_loop_var": "item", "changed": false, "item": "/tmp/id_rsa_remcpyusr.pub", "path": "/tmp/id_rsa_remcpyusr.pub", "state": "absent"}
ok: [testsrv-01] => (item=/home/remcpyusr/.ssh/authorized_keys) => {"ansible_loop_var": "item", "changed": false, "item": "/home/remcpyusr/.ssh/authorized_keys", "path": "/home/remcpyusr/.ssh/authorized_keys", "state": "absent"}
ok: [testsrv-02] => (item=/home/remcpyusr/.ssh/authorized_keys) => {"ansible_loop_var": "item", "changed": false, "item": "/home/remcpyusr/.ssh/authorized_keys", "path": "/home/remcpyusr/.ssh/authorized_keys", "state": "absent"}
ok: [testsrv-01] => (item=/home/remcpyusr/.ssh/id_rsa) => {"ansible_loop_var": "item", "changed": false, "item": "/home/remcpyusr/.ssh/id_rsa", "path": "/home/remcpyusr/.ssh/id_rsa", "state": "absent"}
ok: [testsrv-02] => (item=/home/remcpyusr/.ssh/id_rsa) => {"ansible_loop_var": "item", "changed": false, "item": "/home/remcpyusr/.ssh/id_rsa", "path": "/home/remcpyusr/.ssh/id_rsa", "state": "absent"}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
testsrv-01                 : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
testsrv-02                 : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Thank you again for your support!!

Hi.
The hosts field must match groups or hosts from your inventory.
You can change the host field to variables from your survey for the plays that fail (like you did in the first play):

---
- name: Distribute the SSH public key
  hosts: "{{ sql_src_srv }}"
...
- name: Distribute the SSH private key
  hosts: "{{ sql_dest_srv }}"

Or statically set the host field to something from your inventory:

---
- name: Distribute the SSH public key
  hosts: testsrv-01
...
- name: Distribute the SSH private key
  hosts: testsrv-02
1 Like

Thank you! the {{}} was missing!

Now i got the problem the hostname cannot be resolved so is there an way to get this of each host:

Yes. You can reference the ansible_host variable like any other variable.

- name: Print the ansible_host variable.
  ansible.builtin.debug:
     msg: "{{ ansible_host }}"
1 Like

Nice!
The last question to my current test Playbook:
It states in the rsnc Task the sync_src is undefindes, but in the other output the ip gets regularly printed:

---
- name: Generate SSH keypair on remote hosts and distribute keys
  hosts:
    - "{{ sql_dest_srv }}"  
    - "{{ sql_src_srv }}"  
  become: true
  vars:
    sync_dst: "{{ hostvars[sql_dest_srv]['ansible_host'] }}"
    sync_src: "{{ hostvars[sql_src_srv]['ansible_host'] }}"

  tasks:
    - name: Output the value of sql_src_srv
      ansible.builtin.debug:
        msg: "The value of sql_src_srv is: {{ sql_src_srv }}"

    - name: Output the value of sql_dest_srv
      ansible.builtin.debug:
        msg: "The value of sql_dest_srv is: {{ sql_dest_srv }}"

    - name: Output the value of sync_src
      ansible.builtin.debug:
        msg: "The sync_src ({{ sql_src_srv }} ansible_host) is: {{ sync_src }}"

    - name: Output the value of sync_dst
      ansible.builtin.debug:
        msg: "The sync_dst ({{ sql_dest_srv }} ansible_host) is: {{ sync_dst }}"

- name: Create SSH keypair in /tmp
  hosts: localhost
  tasks:
    - name: Create SSH keypair in /tmp
      ansible.builtin.openssh_keypair:
        path: /tmp/id_rsa_remcpyusr
        type: rsa
        force: true
        comment: "remcpyusr"
      run_once: true

- name: Distribute the SSH public key
  hosts: "{{ sql_src_srv }}"
  tasks:
    - name: Distribute the SSH public key
      ansible.builtin.copy:
        src: /tmp/id_rsa_remcpyusr.pub
        dest: /home/remcpyusr/.ssh/authorized_keys
        owner: remcpyusr
        mode: '0644'
        force: yes

- name: Distribute the SSH private key
  hosts: "{{ sql_dest_srv }}"
  tasks:
    - name: Distribute the SSH private key
      ansible.builtin.copy:
        src: /tmp/id_rsa_remcpyusr
        dest: /home/remcpyusr/.ssh/id_rsa
        owner: remcpyusr
        mode: '0600'
        force: yes

- name: Synchronize files to target host
  hosts: "{{ sql_dest_srv }}"
  tasks:
    - name: Synchronize files to target host
      ansible.builtin.command:
        cmd: rsync -avz -e "ssh -i /home/remcpyusr/.ssh/id_rsa -o StrictHostKeyChecking=no" remcpyusr@{{ sync_src }}:/tmp/test2.txt /tmp/  

- name: Ensure key pair is absent on the controller and hosts
  hosts: all
  tasks:
    - name: Ensure key pair is absent on the controller and hosts
      ansible.builtin.file:
        path: "{{ item }}"
        state: absent
      loop:
        - /tmp/id_rsa_remcpyusr
        - /tmp/id_rsa_remcpyusr.pub
        - /home/remcpyusr/.ssh/authorized_keys
        - /home/remcpyusr/.ssh/id_rsa

I need to transfer the variable o the other host and originally there was the hostnam, but the host could not resolve the hostname so i try to use the ip instead.

This is by design: “When you define variables in a play, they are only visible to tasks executed in that play”.
In this case, you have a playbook consisting of five plays. The “sync_src” variable, defined in the first play, is unavailable to the remaining plays.

Thank you i thought it would be a diffrent cause, but i no need to add the vars where needed again.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.