An open API service indexing awesome lists of open source software.

https://github.com/epomatti/ansible-pull-demo

Setting up an ansible-pull managed node
https://github.com/epomatti/ansible-pull-demo

ansible ansible-pull compose cron cronjob crontab docker docker-compose vagrant virtualbox

Last synced: 3 months ago
JSON representation

Setting up an ansible-pull managed node

Awesome Lists containing this project

README

          

# `ansible-pull` demo

## Local Development

Setting up the local development environment to work with Ansible.

You'll need `virtualenv`:

```sh
sudo apt-get install python3-virtualenv -y
```

Start the environment:

```sh
python3 -m venv .venv
virtualenv .venv
source .venv/bin/activate
```

Install the dependencies:

```sh
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txt
```

## Control Node

Depending on yor preference, the following provisioning options are available in this repository.

### Multipass

Launch the instance:

```ps1
multipass launch 24.04 `
--name vm-ansible `
--cpus 4 `
--memory 8G `
--disk 50G `
--timeout 1800 `
--cloud-init .\multipass\cloud-init.yaml
```

Connect via SSH:

```ps1
multipass shell vm-ansible
```

Check if `*** System restart required ***`. If so, `sudo reboot`.

Verify the cloud-init status:

```sh
cloud-init status
```

Check logs if necessary:

```sh
/var/log/cloud-init-output.log
/var/log/cloud-init.log
```

### VirtualBox

Prepare the directory with Vagrant:

```sh
mkdir -p vagrant-pull-node/ansible
cd vagrant-pull-node/ansible
vagrant init "ubuntu/jammy64"
```

Set additional parameters as in the [virtualbox/Vagrantfile](virtualbox/Vagrantfile). This will add the necessary packages.

Create and connect to the VM:

```sh
vagrant up
vagrant ssh
```

### Docker

> [!TIP]
> In case of docker fetch errors, you may try `docker system prune -a`

For lightweight docker environment, set it up:

```sh
docker compose build
docker compose up -d
docker compose exec workstation /bin/bash
```

## `ansible-pull`

This section is executed from within the control node.

### Simple Pull Command

> [!TIP]
> A fork will be required to create tokens in the account

Connect to the GitHub account and generate a [new token](https://github.com/settings/personal-access-tokens). Prefer a fine-grained token, where only `read-only` permissions to the `Contents` to the repository is required.

> [!NOTE]
> Preferring the `https` URL option to use GitHub tokens

The next `ansible-pull` command was adapted from [this page](https://medium.com/planetarynetworks/ansible-pull-with-private-github-repository-d147fdf6f60b).

For development purposes, create a `/opt/ansible/pull.sh` script:

> [!TIP]
> Create link for `ln -s /opt/ansible/pull.sh /home/vagrant/pull.sh`

```sh
#!/bin/bash
token=1234
url="https://$token:x-oauth-basic@github.com/epomatti/ansible-pull-demo.git"
ansible-pull -U $url -d /opt/epomatti/ansible-pull-demo
```

Execute the Ansible pull script:

```sh
bash pull.sh
```

### Crontab

The [local.yml](local.yml) file will create a cron job to pull new configuration every minute. The package [flock](https://manpages.ubuntu.com/manpages/jammy/man1/flock.1.html) will be used to prevent cron job overlap.

Check the crontab logs:

```sh
sudo tail -f /var/log/syslog
```

The script output will be redirected to this file:

```sh
sudo tail -f /var/log/ansible-pull.log
```

To make ad-hoc adjustments to the local configuration:

```sh
sudo systemctl stop cron
```

## Ansible Vault

Create the key file, and add a secure password:

```sh
touch ~/.vault_key
chmod 600 ~/.vault_key
```

Encrypt files (they must be commited to the repository):

```sh
ansible-vault encrypt --vault-password-file ~/.vault_key secret.txt
```

Decrypt the files locally:

```sh
ansible-vault decrypt --vault-password-file ~/.vault_key secret.txt
```

When pulling, use the key:

```sh
ansibel pull --vault-password-file
```

## Testing

> [!TIP]
> This is about [test strategies][test_strategies] for testing the application you are deploying

The primary tool for this should be [Molecule][molecule].

```sh
bash local.sh
```

Running the tests:

```sh
bash tests.sh
```

## Reference Content

- [Using Ansible "Pull" Mode to Dynamically Automate Server/Workstation Builds](https://youtu.be/sn1HQq_GFNE)
- [Getting started with Ansible 01 - Introduction](https://youtu.be/3RiVKs8GHYQ?list=PLT98CRl2KxKEUHie1m24-wkyHpEsa4Y70)
- [Using Ansible to automate your Laptop and Desktop configs!](https://youtu.be/gIDywsGBqf4)
- [Encrypting Files with Ansible Vault](https://youtu.be/xeBnAbmt3Wk)

[test_strategies]: https://docs.ansible.com/projects/ansible/latest/reference_appendices/test_strategies.html
[molecule]: https://docs.ansible.com/projects/molecule/