Running ansible playbook on wsl Ubuntu
Run the following commands from your home directory:
sudo apt install python3-venv source venv/bin/activate
Your prompt will change:
(.venv) user@wsl_host:~/dev/ansible$
Install ansible:
pip install ansible
Example 1
Copy Hello World file to all hosts.
Create an inventory file (inventory.yml):
all: vars: ansible_user: user hosts: wsl_host: ansible_host: localhost ubuntu_laptop: ansible_host: 192.168.xxx.xxx
Create a playbook (playbook.yml):
- name: Play 1 hosts: all tasks: - name: Create Hello World file ansible.builtin.copy: content: "Hello World" dest: /home/user/hello-world.txt
Run the playbook:
ansible-playbook -i inventory.yml playbook.yml --ask-pass
You will be prompted to enter the appropriate password(s). This will create hello-world.txt file on the two host that you defined in the inventory.yml file.
See First Ansible Playbook for more details.
Example 2
Fetch file from Linux host on wsl2 host using ansible fetch.
Create remote linux host ini file (host.ini):
[remote_linux_servers] 192.168.xxx.xxx ansible_user=user
Create fetch playbook (fetch_playbook.yml):
- name: Fetch data.json file from Linux to WSL2 hosts: remote_linux_servers tasks: - name: Download the remote file ansible.builtin.fetch: src: /home/user/data.json dest: ~/dev/ansible
Run the playbook:
ansible-playbook -i host.ini fetch_playbook.yml --ask-pass
You will be prompted to enter the appropriate password. This will fetch data.json file and save it to wsl2 ubuntu.
Comments