Ansible: Difference between revisions
Jump to navigation
Jump to search
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
< | <syntaxhighlight lang="bash"> | ||
sudo apt update && sudo apt list --upgradeable | sudo apt update && sudo apt list --upgradeable | ||
sudo apt upgrade && sudo apt install ansible ansible-lint sshpass | sudo apt upgrade && sudo apt install ansible ansible-lint sshpass | ||
Line 6: | Line 6: | ||
ansible --version | ansible --version | ||
ansible-lint --version | ansible-lint --version | ||
</ | </syntaxhighlight> | ||
== Playbook == | == Playbook == | ||
Line 64: | Line 64: | ||
== Playbook » LXD== | == Playbook » LXD== | ||
<syntaxhighlight lang="yaml"> | {| | ||
|valign="top"| | |||
<syntaxhighlight lang="yaml" highlight="4,12,36,39-43,65-67" line> | |||
mkdir -p ~/Documents/ansible-playground | mkdir -p ~/Documents/ansible-playground | ||
cd ~/Documents/ansible-playground | cd ~/Documents/ansible-playground | ||
Line 79: | Line 81: | ||
tasks: | tasks: | ||
- name: | - name: Dirty Facts | ||
set_fact: | set_fact: | ||
raw_cpu_family: > | |||
{% if ansible_architecture == 'x86_64' %} | {% if ansible_architecture == 'x86_64' %} | ||
amd64 | amd64 | ||
Line 89: | Line 91: | ||
{{ ansible_architecture }} | {{ ansible_architecture }} | ||
{% endif %} | {% endif %} | ||
- name: | delegate_to: localhost | ||
- name: Clean Facts | |||
set_fact: | |||
cpu_family: "{{ raw_cpu_family | trim }}" | |||
delegate_to: localhost | |||
- name: Launch Container | |||
community.general.lxd_container: | community.general.lxd_container: | ||
name: "{{ container_name }}" | name: "{{ container_name }}" | ||
ignore_volatile_options: true | ignore_volatile_options: true | ||
type: container | |||
state: started | state: started | ||
source: | source: | ||
mode: pull | |||
type: image | type: image | ||
protocol: simplestreams | |||
server: https://cloud-images.ubuntu.com/releases | |||
alias: 24.04/{{ cpu_family }} | |||
profiles: ["default"] | |||
wait_for_ipv4_addresses: true | |||
timeout: 600 | |||
- name: Check Python | |||
delegate_to: "{{ container_name }}" | |||
ansible.builtin.raw: | | |||
lxc exec {{ container_name }} -- dpkg -s python3.11 | |||
register: python_install_check | |||
failed_when: python_install_check.rc not in [0, 1] | |||
changed_when: false | |||
- name: Install Python | |||
delegate_to: "{{ container_name }}" | |||
ansible.builtin.raw: | | |||
lxc exec {{ container_name }} -- apt-get update | |||
lxc exec {{ container_name }} -- apt-get install -y python3.11 | |||
lxc exec {{ container_name }} -- apt-get clean | |||
when: python_install_check.rc == 1 | |||
YML | |||
ansible-playbook lxd-launch-ubuntu-24-lts.yml | |||
lxc exec ubuntu-24-lts -- bash | |||
lxc rm ubuntu-24-lts -f | |||
lxc ls | |||
</syntaxhighlight> | |||
|valign="top"| | |||
<syntaxhighlight lang="yaml" highlight="4,12,36,39-43,65-67" line> | |||
mkdir -p ~/Documents/ansible-playground | |||
cd ~/Documents/ansible-playground | |||
cat << 'YML' | tee ./lxd-launch-ubuntu-24-lts-vm.yml >/dev/null | |||
--- | |||
- name: LXD Launch Ubuntu 24.04 LTS | |||
hosts: localhost | |||
connection: local | |||
vars: | |||
cpu_architecture: "{{ ansible_architecture }}" | |||
os_family: "{{ ansible_os_family }}" | |||
container_name: ubuntu-24-lts-vm | |||
tasks: | |||
- name: Dirty Facts | |||
set_fact: | |||
raw_cpu_family: > | |||
{% if ansible_architecture == 'x86_64' %} | |||
amd64 | |||
{% elif ansible_architecture == 'aarch64' %} | |||
arm64 | |||
{% else %} | |||
{{ ansible_architecture }} | |||
{% endif %} | |||
delegate_to: localhost | |||
- name: Clean Facts | |||
set_fact: | |||
cpu_family: "{{ raw_cpu_family | trim }}" | |||
delegate_to: localhost | |||
- name: Launch Container | |||
community.general.lxd_container: | |||
name: "{{ container_name }}" | |||
ignore_volatile_options: true | |||
type: virtual-machine | |||
state: started | |||
source: | |||
mode: pull | mode: pull | ||
type: image | |||
protocol: simplestreams | protocol: simplestreams | ||
server: https://cloud-images.ubuntu.com/releases | server: https://cloud-images.ubuntu.com/releases | ||
Line 104: | Line 186: | ||
timeout: 600 | timeout: 600 | ||
- name: Check | - name: Check Python | ||
delegate_to: "{{ container_name }}" | delegate_to: "{{ container_name }}" | ||
ansible.builtin.raw: | | ansible.builtin.raw: | | ||
Line 112: | Line 194: | ||
changed_when: false | changed_when: false | ||
- name: Install | - name: Install Python | ||
delegate_to: "{{ container_name }}" | delegate_to: "{{ container_name }}" | ||
ansible.builtin.raw: | | ansible.builtin.raw: | | ||
Line 121: | Line 203: | ||
YML | YML | ||
ansible-playbook lxd-launch-ubuntu-24-lts.yml | ansible-playbook lxd-launch-ubuntu-24-lts-vm.yml | ||
lxc exec ubuntu-24-lts-vm -- bash | |||
lxc rm ubuntu-24-lts-vm -f | |||
lxc ls | lxc ls | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|} | |||
== Molecule == | == Molecule == | ||
Line 190: | Line 275: | ||
| valign="top" | | | valign="top" | | ||
* [https://docs.ansible.com/ansible/latest/collections/community/general/yarn_module.html <code>community.general.yarn</code>] | * [https://docs.ansible.com/ansible/latest/collections/community/general/yarn_module.html <code>community.general.yarn</code>] | ||
| valign="top" | | |||
| valign="top" | | |||
|- | |||
| colspan="3" | | |||
---- | |||
|- | |||
| valign="top" | | |||
* [https://docs.ansible.com/ansible/latest/collections/community/docker/docker_config_module.html <code>community.docker.docker_config</code>] | |||
* [https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html <code>community.docker.docker_image</code>] | |||
* [https://docs.ansible.com/ansible/latest/collections/community/docker/ <code>Community.Docker</code>] | |||
| valign="top" | | | valign="top" | | ||
Line 294: | Line 392: | ||
{| | {| | ||
| valign="top" | | | valign="top" | | ||
< | <syntaxhighlight lang="bash"> | ||
python3 -m venv .venv --prompt="Molecule" | python3 -m venv .venv --prompt="Molecule" | ||
# source .venv/bin/activate | # source .venv/bin/activate | ||
# (Molecule) $ | # (Molecule) $ | ||
</ | </syntaxhighlight> | ||
| valign="top" | | | valign="top" | | ||
< | <syntaxhighlight lang="bash"> | ||
python3 -m venv .venv --prompt="Molecule" | python3 -m venv .venv --prompt="Molecule" | ||
# source .venv/bin/activate | # source .venv/bin/activate | ||
# (Molecule) $ | # (Molecule) $ | ||
</ | </syntaxhighlight> | ||
| valign="top" | | | valign="top" | | ||
< | <syntaxhighlight lang="PowerShell"> | ||
python -m venv .venv --prompt="Molecule" | python -m venv .venv --prompt="Molecule" | ||
# .venv\Scripts\activate | # .venv\Scripts\activate | ||
# (Molecule) PS> | # (Molecule) PS> | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
Line 319: | Line 417: | ||
|- | |- | ||
| valign="top" | | | valign="top" | | ||
< | <syntaxhighlight lang="bash"> | ||
pip install -r requirements.txt | pip install -r requirements.txt | ||
pip freeze > requirements.txt | pip freeze > requirements.txt | ||
</ | </syntaxhighlight> | ||
| valign="top" | | | valign="top" | | ||
< | <syntaxhighlight lang="ini"> | ||
[all:vars] | [all:vars] | ||
ansible_port=22 | ansible_port=22 | ||
</ | </syntaxhighlight> | ||
| valign="ini" | | | valign="ini" | | ||
< | <syntaxhighlight lang="bash"> | ||
ansible_connection=ssh | ansible_connection=ssh | ||
ansible_ssh_pass=vagrant | ansible_ssh_pass=vagrant | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
Line 341: | Line 439: | ||
|- | |- | ||
| valign="top"| | | valign="top"| | ||
< | <syntaxhighlight lang="bash"> | ||
ansible-inventory\ | ansible-inventory\ | ||
-i inventories/staging/\ | -i inventories/staging/\ | ||
--list | --list | ||
</ | </syntaxhighlight> | ||
| valign="top"| | | valign="top"| | ||
< | <syntaxhighlight lang="bash"> | ||
ansible dns_servers\ | ansible dns_servers\ | ||
-i inventories/staging/\ | -i inventories/staging/\ | ||
-m ping | -m ping | ||
</ | </syntaxhighlight> | ||
| valign="top"| | | valign="top"| | ||
< | <syntaxhighlight lang="bash"> | ||
ansible-playbook\ | ansible-playbook\ | ||
-i inventories/staging/\ | -i inventories/staging/\ | ||
main_playbook.yml | main_playbook.yml | ||
</ | </syntaxhighlight> | ||
|- | |- |
Latest revision as of 07:54, 19 May 2024
sudo apt update && sudo apt list --upgradeable
sudo apt upgrade && sudo apt install ansible ansible-lint sshpass
sshpass -V
ansible --version
ansible-lint --version
Playbook
chorke-academia-project ├─ main_playbook.yml └─ inventories/ ├─ staging/ │ └─ academia/ │ ├─ inventory.yml │ └─ group_vars/ │ ├─ academia_group.yaml │ └─ all.yaml └─ test/ └─ academia/ ├─ inventory.yml └─ group_vars/ ├─ academia_group.yaml └─ all.yaml |
all:
children:
gtw_servers:
hosts:
10.20.30.1:
dmz_servers:
hosts:
10.20.30.100:
dns_servers:
hosts:
10.20.30.[100:102]:
vars:
ansible_port: 4321
ansible_user: deploy
ansible_ssh_pass: sadaqah
| |
| ||
mkdir -p chorke-academia-project/inventories/{staging,test}/academia/{group_vars,host_vars}
touch chorke-academia-project/inventories/{staging,test}/academia/group_vars/{all,academia_group}.yaml
touch chorke-academia-project/inventories/{staging,test}/academia/inventory.yml
touch chorke-academia-project/main_playbook.yml
cd chorke-academia-project
ansible-playbook -i inventories/staging main_playbook.yml
ansible-inventory -i inventories/staging --list
|
Playbook » LXD
mkdir -p ~/Documents/ansible-playground
cd ~/Documents/ansible-playground
cat << 'YML' | tee ./lxd-launch-ubuntu-24-lts.yml >/dev/null
---
- name: LXD Launch Ubuntu 24.04 LTS
hosts: localhost
connection: local
vars:
cpu_architecture: "{{ ansible_architecture }}"
os_family: "{{ ansible_os_family }}"
container_name: ubuntu-24-lts
tasks:
- name: Dirty Facts
set_fact:
raw_cpu_family: >
{% if ansible_architecture == 'x86_64' %}
amd64
{% elif ansible_architecture == 'aarch64' %}
arm64
{% else %}
{{ ansible_architecture }}
{% endif %}
delegate_to: localhost
- name: Clean Facts
set_fact:
cpu_family: "{{ raw_cpu_family | trim }}"
delegate_to: localhost
- name: Launch Container
community.general.lxd_container:
name: "{{ container_name }}"
ignore_volatile_options: true
type: container
state: started
source:
mode: pull
type: image
protocol: simplestreams
server: https://cloud-images.ubuntu.com/releases
alias: 24.04/{{ cpu_family }}
profiles: ["default"]
wait_for_ipv4_addresses: true
timeout: 600
- name: Check Python
delegate_to: "{{ container_name }}"
ansible.builtin.raw: |
lxc exec {{ container_name }} -- dpkg -s python3.11
register: python_install_check
failed_when: python_install_check.rc not in [0, 1]
changed_when: false
- name: Install Python
delegate_to: "{{ container_name }}"
ansible.builtin.raw: |
lxc exec {{ container_name }} -- apt-get update
lxc exec {{ container_name }} -- apt-get install -y python3.11
lxc exec {{ container_name }} -- apt-get clean
when: python_install_check.rc == 1
YML
ansible-playbook lxd-launch-ubuntu-24-lts.yml
lxc exec ubuntu-24-lts -- bash
lxc rm ubuntu-24-lts -f
lxc ls
|
mkdir -p ~/Documents/ansible-playground
cd ~/Documents/ansible-playground
cat << 'YML' | tee ./lxd-launch-ubuntu-24-lts-vm.yml >/dev/null
---
- name: LXD Launch Ubuntu 24.04 LTS
hosts: localhost
connection: local
vars:
cpu_architecture: "{{ ansible_architecture }}"
os_family: "{{ ansible_os_family }}"
container_name: ubuntu-24-lts-vm
tasks:
- name: Dirty Facts
set_fact:
raw_cpu_family: >
{% if ansible_architecture == 'x86_64' %}
amd64
{% elif ansible_architecture == 'aarch64' %}
arm64
{% else %}
{{ ansible_architecture }}
{% endif %}
delegate_to: localhost
- name: Clean Facts
set_fact:
cpu_family: "{{ raw_cpu_family | trim }}"
delegate_to: localhost
- name: Launch Container
community.general.lxd_container:
name: "{{ container_name }}"
ignore_volatile_options: true
type: virtual-machine
state: started
source:
mode: pull
type: image
protocol: simplestreams
server: https://cloud-images.ubuntu.com/releases
alias: 24.04/{{ cpu_family }}
profiles: ["default"]
wait_for_ipv4_addresses: true
timeout: 600
- name: Check Python
delegate_to: "{{ container_name }}"
ansible.builtin.raw: |
lxc exec {{ container_name }} -- dpkg -s python3.11
register: python_install_check
failed_when: python_install_check.rc not in [0, 1]
changed_when: false
- name: Install Python
delegate_to: "{{ container_name }}"
ansible.builtin.raw: |
lxc exec {{ container_name }} -- apt-get update
lxc exec {{ container_name }} -- apt-get install -y python3.11
lxc exec {{ container_name }} -- apt-get clean
when: python_install_check.rc == 1
YML
ansible-playbook lxd-launch-ubuntu-24-lts-vm.yml
lxc exec ubuntu-24-lts-vm -- bash
lxc rm ubuntu-24-lts-vm -f
lxc ls
|
Molecule
mkdir molecule-example && cd molecule-example python3 -m venv .venv --prompt="molecule" source ./.venv/bin/activate pip install 'molecule[lint]' pip install molecule-podman pip freeze > requirements.txt molecule init role 'acme.mywebapp' --driver-name podman
Modules
Namespaces
| ||
Knowledge
python3 -m venv .venv --prompt="Molecule"
# source .venv/bin/activate
# (Molecule) $
|
python3 -m venv .venv --prompt="Molecule"
# source .venv/bin/activate
# (Molecule) $
|
python -m venv .venv --prompt="Molecule"
# .venv\Scripts\activate
# (Molecule) PS>
|
| ||
pip install -r requirements.txt
pip freeze > requirements.txt
|
[all:vars]
ansible_port=22
|
ansible_connection=ssh
ansible_ssh_pass=vagrant
|
| ||
ansible-inventory\
-i inventories/staging/\
--list
|
ansible dns_servers\
-i inventories/staging/\
-m ping
|
ansible-playbook\
-i inventories/staging/\
main_playbook.yml
|
| ||
pip install -U pip pip install --upgrade pip |
pip freeze > requirements.txt pip install -r requirements.txt --upgrade |
pip list --outdated pip install pip-check |
| ||
pip list -o | gawk -F ' ' 'NR>2{print$1}' | xargs pip install -U pip list --outdated | gawk -F ' ' 'NR>2{print$1}' | xargs pip install --upgrade |