Lately I've been tinkering with Ansible and want to achieve following on my test node using Ansible:
- Clone a repo to the Remote Node
grepa packagelist from a textfile within the git repo- Load the tools from the packagelist into an Ansible variable
- Install these tools via the Ansible
packagemodule oraptmodule.
So far I've got this:
---
- hosts: all
become: true
vars:
username: foobar
tasks:
- name: Install git package
package:
name: git
state: present
- name: Clone GIT Repo
git:
repo: https://github.com/somerepo/.dotfiles.git
dest: /home/{{ username }}/.dotfiles
clone: yes
version: master
update: yes
force: yes
- name: Set permission on folder ~/.dotfiles
file:
dest: /home/{{ username }}/.dotfiles
recurse: yes
owner: "{{ username }}"
group: "{{ username }}"
mode: "0775"
- name: Extract list of needed tools from install.sh
shell:
cmd: grep "^tools=" /home/{{ username }}/.dotfiles/install.sh | tr '"' " " | cut -c7-
register: grep_output
- name: Install following packages "{{ grep_output }}"
apt:
name: "{{ grep_output }}"
state: present
update_cache: yes
The shell command gathers a list with tools:
grep "^tools=" install.sh |tr '"' " " | cut -c7-
with the STDOUT output
zsh neovim vim tmux ranger stow wget curl git fzf
Those values I want to store in an Ansible variable and install it via package plugin.
When I run the playbook on my test VM
PLAY [all] *******************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************
[WARNING]: Platform linux on host vm-mint21 is using the discovered Python interpreter at /usr/bin/python3.10, but future
installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.12/reference_appendices/interpreter_discovery.html for more information.
ok: [vm-mint21]
TASK [Install git package] ***************************************************************************************************
ok: [vm-mint21]
TASK [Clone GIT Repo] ********************************************************************************************************
changed: [vm-mint21]
TASK [Set permission on folder ~/.dotfiles] **********************************************************************************
changed: [vm-mint21]
TASK [Extract list of needed tools from install.sh] **************************************************************************
changed: [vm-mint21]
TASK [Install following packages "{'changed': True, 'stdout': ' zsh neovim vim tmux ranger stow wget curl git fzf ', 'stderr': '', 'rc': 0, 'cmd': 'grep "^tools=" /home/soeren/.dotfiles/install.sh | tr \'"\' " " | cut -c7-', 'start': '2022-09-12 22:33:37.056914', 'end': '2022-09-12 22:33:37.061264', 'delta': '0:00:00.004350', 'msg': '', 'stdout_lines': [' zsh neovim vim tmux ranger stow wget curl git fzf '], 'stderr_lines': [], 'failed': False}"] ***
fatal: [vm-mint21]: FAILED! => {"changed": false, "msg": "argument 'package' is of type <class 'dict'> and we were unable to convert to list: <class 'dict'> cannot be converted to a list"}
PLAY RECAP *******************************************************************************************************************
vm-mint21 : ok=5 changed=3 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
I get following error:
argument 'package' is of type <class 'dict'> and we were unable to convert to list: <class 'dict'> cannot be converted to a list"