I followed Ansible best practice to create my ansible automation directory structure like following:
group_vars
group_vars/all
host_vars
os.yml
production
roles
roles/os-issue
Since I’m using ansible user as a remote user, and sudo method to escalate its privileges globally, I want to define this in group_vars/all file. Here is what I define in group_vars/all directory:
I don’t think the failure is related to using or not using “sudo”. The playbook failed because Ansible could not connect to the remote machine.
The error message:
fatal: [myserver]: UNREACHABLE! => {“changed”: false, “msg”: “Failed to connect to the host via ssh: ============================================================\n|Permission denied (publickey,password,keyboard-
interactive).”, “unreachable”: true}
tells you that Ansible cannot connect to ‘myserver’ over SSH. If you try to connect to the target machine using SSH and the user ‘ansible’ from the command line, does that work? Do you have to type in a password? If you’re using SSH keys, does the ‘ansible’ user have permission to access the correct key?
I just figured out myself and I want to post it for sharing with other who would have similar issue like myself.
The issue is what variable names I use in group_vars/all file.
remote_user, become and become_method, are not inventory-like recognized variables, they are recognized in playbook, but not in inventory, although those variables are not put in inventory file, I believe, they are inventory variables.
The correct names when putting in group_vars/all file are:
ansible_user is a variable, can be defined anywhere variables are defined
remote_user is a keyword, as such it can only be set on playbook
objects (play/block/role/task)