yum update
and apt-get upgrade
.Vagrantfile
that launches the playbook. Vagrant.configure(2) do |config| config.vm.box = "ubuntu/xenial64" config.vm.network "forwarded_port", guest: 80, host: 8080 # , : config.ssh.insert_key = false config.vm.provision "ansible" do |ansible| ansible.verbose = "v" ansible.playbook = "playbook.yml" end end
vagrant up
. Did not take off. Since the official xenial image is only for VirtualBox, and in Fedora Linux virtualization is through libvirt. Long remembered the correct command: vagrant up --provider virtualbox
. Then syntax errors in yaml rules (why are there three mandatory hyphens at the beginning?). Remember that after starting the box to restart Ansible we write vagrant provision
. - name: Install python for Ansible become: yes raw: test -e /usr/bin/python || (apt -qy update && apt install -y python-minimal) register: output changed_when: output.stdout
remote_user
, and how it turns out that there is a superuser in each box. I will run the playbook on a clean server, where there will be only root, and I will need to make my superuser. But doing it under a vagrant is needed differently than on a clean server, apparently. In general, it is not clear: will there be two playbooks for staging and production?become
and become_user
: one does not mean the other. What of this need to be specified in the root playbook, if you constantly need to enable root to configure the server? I first put become: yes
and in every second task I wrote become_user: root
. Then it turned out that without become_user
everything also works from root! Because root is the default value and I, in fact, made sudo -i
from the very beginning without being able to let go.dnf update
. Continuing to pop up with the playbook. Vagrant worked, and dnf in the next tab updated VirtualBox. It seems that it is not necessary to do this, because the next vagrant provision
said: “everything broke and I am not guilty.” It lacked VirtualBox, which “ terminated unexpectedly during startup with exit code 1 (0x1) ” - and even though you are cracking. The vboxheadless -h
command (I'm not a real devops, I googled) showed the error -1912. On the Internet, one and all answer: reinstall VirtualBox. Fuck it does not help. Desperate, I found the xenial box for libvirt and switched to it. Well, when there is a choice.update_cache=yes
as a separate task. And this task, that's the trouble, all the time returns "changed". It turned out that you need to register cache_valid_time=3600
to check for updates no more than once per hour. At first I thought to write 86400 (day), but I'm not going to call Ansible in the crown, and let it live once a month.become_user: postgres
. And here the box produced a strange error: "It was an unrealized user ." Remember how Ansible loads modules on the server and starts there? Well, he downloads them from root or from another superuser, and then the postgres user does not have access to them. Here is bad luck.ansible.cfg
and write pipelining=True
inside (and to solve some other arising problem, I temporarily set pipelining=False
). The second way out - literally, “do not do this.” And the third is the simplest: put the acl
package and everything works in a magical way. Rather, it does not work in another way: " sudo: a password is required ". Well, what's the deal, where are the passwords here at all, do I enter with the key?become_user
, apparently, does sudo -u postgres
, but it requires the password of the user vagrant. There is no password.become_method: su
timed out because the server asks for a password, but Ansible does not understand this. What he does there is not clear, because sudo su postgres
does not ask me for a password. There is an option in the /etc/sudoers.d/vagrant
file /etc/sudoers.d/vagrant
write “ vagrant ALL=(ALL) ...
”, because the word in brackets will allow you to do sudo -u
without a password. But then the playbook becomes sharpened by Vagrant, and I still have to run it in the sale. Inaccurately.become
. Postgres expected squeeze: " Peer authentication failed for user" postgres "." Digging up a stewardess. New plan: to run a role under the user zverik, who has everything in the world right. I split the playbook into two: in the first I install the python and make the user, the second I install and configure everything else with remote_user: zverik
. I run. And again " sudo: a password is required ". Why? Well, yes, Vagrant passes the value of remote_user
and does not allow it to change. Well damn.ansible.force_remote_user
in Vagrantfile and restarted provision
. Hurray, a new mistake! Reminds that the user login zverik works only with a certificate. But I also have a certificate, and vagrant ssh -p
works and lets in without a password. Googled the solution: you need to specify the path to the certificate in ansible.cfg
. It will not work for the same reason as remote_user
: Vagrant wins. This time it's easier to override the main variable: add the “ ansible_ssh_private_key_file: "{{ lookup('env', 'HOME') }}/.ssh/id_rsa"
, and everything works! Not very nice, but hurray!command
or shell
modules. The latter, as the documentation writes, only in extreme cases, so forget about redirecting the output and &&
. The result is always "changed", which is bad. Manage the result with either the creates
parameter (more conveniently - in the args
block, along with chdir
), or register
and changed_when
. It is useful to check the conditions before the execution: first, command + register + changed_when: False
reconnaissance, and then with the help of when
check the stored stdout to start the command.command
module calls, the better. Google: there is almost always a module. For example, I first did command: npm install -g {{ item }}
, and then I discovered that you can npm: name={{ item }} global=yes
. A module is always better than a command, because there is no need to check the configuration and because the result of the work will not be in the stdout line, but in a convenient structure.lineinfile
, which searches for a line by a regular expression and replaces it with another. The blockinfile
module adds whole blocks of text. There is a nuance with it: if several tasks are written into one file, then you need to override the marker: # {mark} block name
. Otherwise, everyone will overwrite other blocks. command: psql -A -t -d {{ gisdb }} -c "SELECT tableowner FROM pg_tables WHERE schemaname = 'public' AND tablename = 'spatial_ref_sys'"
with_items
, then do so. A group of repetitive tasks with similar parameters are taken into a separate file and called via include_role
with vars
. There still has to be about parameterization of roles, but I'm still learning and I have one role.geerlingguy.apache
, when apt: pkg=apache2
solves all my tasks? Or, here, I found the role to install osm2pgsql from the sources, and it is 2014 and outdated sudo: yes
. That is, I, of course, recorded roles_path = roles.galaxy:roles
in ansible.cfg
and made a playbook to install all the roles, but there is nothing to set yet. Here is what it looks like: - hosts: localhost vars: galaxy_path: roles.galaxy tasks: - name: Remove old galaxy roles file: path={{ galaxy_path }} state=absent - name: Install Ansible Galaxy roles local_action: command ansible-galaxy install -r requirements.yml --roles-path {{ galaxy_path }}
requirements.yml
write lines for each role from the Galaxy: - src: .
vagrant destroy
and re-create the box. Absolutely find several jambs: forgotten sudo, missing mode: 0755 for executable files, missing packages (help dnf provides
or apt-file
to be installed). Finally, the most important thing: after the second launch of the vagrant provision
should be "changed: 0".Source: https://habr.com/ru/post/352616/
All Articles