Skip to content

Instantly share code, notes, and snippets.

@redconfetti
Created March 4, 2016 04:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redconfetti/4a902a776e74f804ff02 to your computer and use it in GitHub Desktop.
Save redconfetti/4a902a776e74f804ff02 to your computer and use it in GitHub Desktop.
Ansible - Remote User Config
# /ansible/roles/remote-user/tasks/main.yml
#
---
- name: Test Default SSH port
local_action: wait_for port=22 timeout=5 host={{inventory_hostname}}
register: default_ssh
ignore_errors: true
- name: set ansible_ssh_port to default
set_fact: ansible_ssh_port=22
when: default_ssh.elapsed < 5
- name: test ssh on alternative port
local_action: wait_for port={{sshd_port}} timeout=5 host={{inventory_hostname}}
register: alternative_ssh
when: default_ssh.elapsed >= 5
ignore_errors: true
- name: set ansible_ssh_port high
set_fact: ansible_ssh_port={{sshd_port}}
when: default_ssh.elapsed >= 5 and alternative_ssh.elapsed < 5
- name: Determine whether to connect as root or admin_user
local_action: command ansible {{ inventory_hostname }} -m ping -i {{ inventory_file }} -u root
failed_when: false
changed_when: false
register: root_status
- name: Set remote user for each host
set_fact:
ansible_ssh_user: "{{ (root_status.rc == 0) | ternary('root', admin_user) }}"
- name: Announce which user was selected
debug:
msg: "Note: Ansible will attempt connections as user = {{ ansible_ssh_user }}"
# Ansible Playbook for Remote Server Setup (Staging / Production)
---
# Discovers the remote user
- name: Determine Remote User
hosts: web
gather_facts: false
roles:
- { role: remote-user, tags: [remote-user, always] }
# Goes through with roles for server setup
- name: WordPress Server - Install LEMP Stack with PHP 5.6 and MariaDB MySQL
hosts: web
sudo: yes
vars_files:
- vars/vault.yml
roles:
- { role: common, tags: [common] }
- { role: swapfile, swapfile_size: 1GB, tags: [swapfile] }
- { role: users, tags: [users] }
- { role: sshd, tags: [sshd] }
- { role: nginx, tags: [nginx] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment