|
| 1 | +# Manage OpenBSD hosts using ansible |
| 2 | + |
| 3 | +## First steps with ansible |
| 4 | + |
| 5 | +All Ansible commands follow the pattern: |
| 6 | + |
| 7 | +``` |
| 8 | +ansible <server_or_group> -m module_name -a arguments |
| 9 | +``` |
| 10 | + |
| 11 | +Run ansible test on OpenBSD host (requires python2.7 installed): |
| 12 | + |
| 13 | +``` |
| 14 | +ansible all -u sysadmin -i www.echoctf.dev, -m ping -e 'ansible_python_interpreter=/usr/local/bin/python2.7' |
| 15 | +``` |
| 16 | + |
| 17 | +## Ansible host inventory |
| 18 | + |
| 19 | +Create inventory location: |
| 20 | + |
| 21 | +``` |
| 22 | +mkdir ~/work/ansible |
| 23 | +touch ~/work/ansible/hosts |
| 24 | +touch ~/.ansible.cfg |
| 25 | +``` |
| 26 | + |
| 27 | +Open `~/.ansible.cfg` file to specify the inventory location: |
| 28 | + |
| 29 | +``` |
| 30 | +[defaults] |
| 31 | +inventory = ~/work/ansible/hosts |
| 32 | +``` |
| 33 | + |
| 34 | +Create entries in `~.work/ansible/hosts` file: |
| 35 | + |
| 36 | +``` |
| 37 | +kerberus.wks.echothrust.dev |
| 38 | +mail.echothrust.dev |
| 39 | +
|
| 40 | +[webservers] |
| 41 | +www.echoctf.dev |
| 42 | +support.echothrust.dev |
| 43 | +www.echothrust.dev |
| 44 | +``` |
| 45 | + |
| 46 | +## Creating playbooks |
| 47 | + |
| 48 | +A playbook is a YAML file, and typically follows this structure: |
| 49 | + |
| 50 | +``` |
| 51 | +--- |
| 52 | +- hosts: [target hosts] |
| 53 | + remote_user: [yourname] |
| 54 | + tasks: |
| 55 | + - [task 1] |
| 56 | + - [task 2] |
| 57 | +``` |
| 58 | + |
| 59 | +For example, the following playbook will create a file on all servers in the `webservers` group |
| 60 | + |
| 61 | +``` |
| 62 | +--- |
| 63 | +- hosts: [webservers] |
| 64 | + remote_user: sysadmin |
| 65 | + tasks: |
| 66 | + - name: Create /tmp/somefile.test |
| 67 | + command: touch /tmp/somefile.test |
| 68 | + become: True |
| 69 | + become_method: doas |
| 70 | +``` |
| 71 | + |
| 72 | +Relevant [post about doas, ansible and env vars](https://www.wordspeak.org/posts/making-ansible-doas-and-openbsd-play-nicely.html) |
| 73 | + |
| 74 | + |
| 75 | +## Running playbooks |
| 76 | + |
| 77 | +Assuming you are in the same directory as a playbook file, run: |
| 78 | + |
| 79 | +``` |
| 80 | +ansible-playbook myplaybook.yml |
| 81 | +``` |
| 82 | + |
| 83 | +If you want to see what hosts this playbook will affect without having to open up the YAML file, you can run: |
| 84 | + |
| 85 | +``` |
| 86 | +ansible-playbook myplaybook.yml --list-hosts |
| 87 | +``` |
| 88 | + |
| 89 | +If you want to see what tasks will run on a specific host: |
| 90 | + |
| 91 | +``` |
| 92 | +ansible-playbook myplaybook.yml -i www.echoctf.dev, --list-tasks |
| 93 | +``` |
| 94 | + |
| 95 | +## Use the "batteries included" |
| 96 | + |
| 97 | +Ansible ships with a large collection of modules that you can run as tasks or via ad-hoc commands. To see a listing of all available modules, run: |
| 98 | + |
| 99 | +``` |
| 100 | +ansible-doc -l |
| 101 | +``` |
| 102 | + |
| 103 | +The list is quite large... some interesting modules follow. |
| 104 | + |
| 105 | +Commands: |
| 106 | + |
| 107 | + * [command](http://docs.ansible.com/ansible/command_module.html) - Executes a command on a remote node |
| 108 | + * [script](http://docs.ansible.com/ansible/script_module.html) - Runs a local script on a remote node after transferring it |
| 109 | + * [shell](http://docs.ansible.com/ansible/shell_module.html) - Execute commands in nodes |
| 110 | + * [raw](http://docs.ansible.com/ansible/raw_module.html) - Executes a low-down and dirty SSH command |
| 111 | + * [fetch](http://docs.ansible.com/ansible/fetch_module.html) - Fetches a file from remote nodes |
| 112 | + |
| 113 | +Files: |
| 114 | + |
| 115 | + * [copy](http://docs.ansible.com/ansible/copy_module.html) - Copies files to remote locations |
| 116 | + * [template](http://docs.ansible.com/ansible/template_module.html) - Templates a file out to a remote server |
| 117 | + * [authorized_key](http://docs.ansible.com/ansible/authorized_key_module.html) - Add/remove SSH authorized keys |
| 118 | + * [known_hosts](http://docs.ansible.com/ansible/known_hosts_module.html) - Add or remove a host from the `known_hosts` file |
| 119 | + * [lineinfile](http://docs.ansible.com/ansible/lineinfile_module.html) - Ensure a particular line is in a file. Replace existing line using a back-referenced regex |
| 120 | + * [blockinfile](http://docs.ansible.com/ansible/blockinfile_module.html) - Insert/update/remove a text block surrounded by marker lines |
| 121 | + * [replace](http://docs.ansible.com/ansible/replace_module.html) - Replace all instances of a particular string in a file using a back-referenced regular expression |
| 122 | + * [ini_file](http://docs.ansible.com/ansible/ini_file_module.html) - Tweak settings in INI files |
| 123 | + * [htpasswd](http://docs.ansible.com/ansible/htpasswd_module.html) - Manage user files for basic authentication |
| 124 | + * [stat](http://docs.ansible.com/ansible/stat_module.html) - Retrieve file or file system status |
| 125 | + * [unarchive](http://docs.ansible.com/ansible/unarchive_module.html) - Unpacks an archive after (optionally) copying it from the local machine |
| 126 | + |
| 127 | +Package management: |
| 128 | + |
| 129 | + * [git](http://docs.ansible.com/ansible/git_module.html) - Deploy software (or files) from git checkouts |
| 130 | + * [openbsd_pkg](http://docs.ansible.com/ansible/openbsd_pkg_module.html) - Manage packages on OpenBSD |
| 131 | + * [yum](http://docs.ansible.com/ansible/yum_module.html) - Manages packages with the yum package manager |
| 132 | + * [apt](http://docs.ansible.com/ansible/apt_module.html) - Manages apt-packages |
| 133 | + |
| 134 | + |
| 135 | +Operating system: |
| 136 | + |
| 137 | + * [service](http://docs.ansible.com/ansible/service_module.html) - Manage services |
| 138 | + * [system](http://docs.ansible.com/ansible/systemd_module.html) - Manage services |
| 139 | + * [user](http://docs.ansible.com/ansible/user_module.html) - Manage user accounts |
| 140 | + * [cron](http://docs.ansible.com/ansible/cron_module.html) - Manage cron.d and crontab entries. |
| 141 | + * [solaris_zone](http://docs.ansible.com/ansible/solaris_zone_module.html) - Manage Solaris zones |
| 142 | + * [sysctl](http://docs.ansible.com/ansible/sysctl_module.html) - Manage entries in sysctl.conf. |
| 143 | + |
| 144 | +Various: |
| 145 | + |
| 146 | + * [mysql_db](http://docs.ansible.com/ansible/mysql_db_module.html) - Add or remove MySQL databases from a remote host |
| 147 | + * [mysql_user](http://docs.ansible.com/ansible/mysql_user_module.html) - Adds or removes a user from a MySQL database |
| 148 | + * [nagios](http://docs.ansible.com/ansible/nagios_module.html) - Perform common tasks in Nagios related to downtime and notifications |
| 149 | + * [redis](http://docs.ansible.com/ansible/redis_module.html) - Various redis commands, slave and flush |
| 150 | + * [letsencrypt](http://docs.ansible.com/ansible/letsencrypt_module.html) - Create SSL certificates with Let's Encrypt |
| 151 | + * [cloudflare_dns](http://docs.ansible.com/ansible/cloudflare_dns_module.html) - manage Cloudflare DNS records |
| 152 | + * [digital_ocean](http://docs.ansible.com/ansible/digital_ocean_module.html) - Create/delete a droplet/SSH_key in DigitalOcean |
| 153 | + * [wakeonlan](http://docs.ansible.com/ansible/wakeonlan_module.html) - Send a magic Wake-on-LAN (WoL) broadcast packet |
0 commit comments