Search code examples
ansibleansible-2.xansible-inventoryansible-facts

Ansible host file how to provide # in ansible_ssh_pass


I am new to ansible. I am facing a problem in hosts file. error output is below.

My question is : How do I escape the # in the ansible_ssh_pass.

I tried with ansible_ssh_pass="airtel\#121" and ansible_ssh_pass=airtel\#121 without double quotes both ways. it is throwing the error.

ansible version: ansible-playbook 2.9.6

host file entry is as below:

[devices] 10.10.10.10 ansible_ssh_user="abcd" ansible_ssh_pass="airtel#121"

playbook is as below:

- name: Cisco show version example
  hosts: devices
  gather_facts: false

    ansible_connection: ansible.netcommon.network_cli
    ansible_network_os: cisco.ios.ios
    ansible_become: yes
    ansible_become_method: enable

  tasks:
    - name: run show version on the routers
      ios_command:
        commands:
        - show version
      register: output

    - name: print output
      debug:
        var: output.stdout_lines

Getting error as below.

xxxx@xxxx:/etc/ansible/playbooks# ansible-playbook check_connectivity_temp.yml -vvvv
ansible-playbook 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Parsed /etc/ansible/hosts inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python3/dist-packages/ansible/plugins/callback/default.py

PLAYBOOK: check_connectivity_temp.yml ***************************************************************************************
Positional arguments: check_connectivity_temp.yml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/etc/ansible/hosts',)
forks: 5
1 plays in check_connectivity_temp.yml

PLAY [Cisco show version example] *******************************************************************************************
META: ran handlers

TASK [run show version on the routers] **************************************************************************************
task path: /etc/ansible/playbooks/check_connectivity_temp.yml:14
<10.10.10.10> attempting to start connection
<10.10.10.10> using connection plugin ansible.netcommon.network_cli
<10.10.10.10> local domain socket does not exist, starting it
<10.10.10.10> control socket path is /root/.ansible/pc/aaec916454
<10.10.10.10> local domain socket listeners started successfully
<10.10.10.10> loaded cliconf plugin ansible_collections.cisco.ios.plugins.cliconf.ios from path /root/.ansible/collections/ansible_collections/cisco/ios/plugins/cliconf/ios.py for network_os cisco.ios.ios
<10.10.10.10> ssh type is set to auto
<10.10.10.10> autodetecting ssh_type
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
<10.10.10.10> ssh type is now set to paramiko
<10.10.10.10>
<10.10.10.10> local domain socket path is /root/.ansible/pc/aaec916454
fatal: [10.10.10.10]: FAILED! => {
    "changed": false,
    "msg": "Failed to authenticate: Authentication failed."
}

PLAY RECAP ******************************************************************************************************************
10.10.10.10              : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

My question is : How do I escape the # in the ansible_ssh_pass.


Solution

  • Put the hash character '#' into an expression {{ '#' }}. For example,

        - debug:
            var: ssh_pass
          vars:
            ssh_pass: "airtel{{ '#' }}121"
    

    gives

      ssh_pass: airtel#121