Ansible: Difference between revisions
Jump to navigation
Jump to search
Line 28: | Line 28: | ||
| valign="top" | | | valign="top" | | ||
< | <syntaxhighlight lang="properties"> | ||
all: | all: | ||
children: | children: | ||
Line 44: | Line 44: | ||
ansible_user: deploy | ansible_user: deploy | ||
ansible_ssh_pass: sadaqah | ansible_ssh_pass: sadaqah | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
Line 51: | Line 51: | ||
|- | |- | ||
| valign="top" colspan="3" | | | valign="top" colspan="3" | | ||
< | <syntaxhighlight lang="bash"> | ||
mkdir -p chorke-academia-project/inventories/{staging,test}/academia/{group_vars,host_vars} | 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/group_vars/{all,academia_group}.yaml | ||
Line 60: | Line 60: | ||
ansible-playbook -i inventories/staging main_playbook.yml | ansible-playbook -i inventories/staging main_playbook.yml | ||
ansible-inventory -i inventories/staging --list | ansible-inventory -i inventories/staging --list | ||
</ | </syntaxhighlight> | ||
|} | |} | ||
== Playbook » LXD== | |||
<syntaxhighlight lang="yaml"> | |||
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: | |||
container_name: ubuntu-24-lts | |||
cpu_architecture: "{{ ansible_architecture }}" | |||
os_family : "{{ ansible_os_family }}" | |||
tasks: | |||
- name: Create a started container | |||
community.general.lxd_container: | |||
name: "{{ container_name }}" | |||
ignore_volatile_options: true | |||
state: started | |||
source: | |||
type: image | |||
mode: pull | |||
protocol: simplestreams | |||
server: https://cloud-images.ubuntu.com/releases | |||
alias: 24.04/{{ cpu_architecture }} | |||
profiles: ["default"] | |||
wait_for_ipv4_addresses: true | |||
timeout: 600 | |||
- name: Check python is installed in container | |||
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 in container | |||
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 | |||
</syntaxhighlight> | |||
== Molecule == | == Molecule == |
Revision as of 17:29, 17 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:
container_name: ubuntu-24-lts
cpu_architecture: "{{ ansible_architecture }}"
os_family : "{{ ansible_os_family }}"
tasks:
- name: Create a started container
community.general.lxd_container:
name: "{{ container_name }}"
ignore_volatile_options: true
state: started
source:
type: image
mode: pull
protocol: simplestreams
server: https://cloud-images.ubuntu.com/releases
alias: 24.04/{{ cpu_architecture }}
profiles: ["default"]
wait_for_ipv4_addresses: true
timeout: 600
- name: Check python is installed in container
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 in container
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
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 |