Tags
  1. ai ansible
  2. omz plugin enable ansible
  3. omz plugin info ansible

vim ~/scripts/ansible_structure_setup.sh

#!/bin/bash
# Script Version: 0.8
# Description: Set up Ansible directory structure and necessary files in /etc/ansible

# Create directories
mkdir -p /etc/ansible/{group_vars,host_vars,roles,playbooks}

# Create configuration and inventory files
if [ ! -f /etc/ansible/ansible.cfg ]; then
  cat > /etc/ansible/ansible.cfg <<EOL
[defaults]
roles_path = /etc/ansible/roles
inventory = /etc/ansible/hosts
host_key_checking = False
log_path = /var/log/ansible.log
retry_files_enabled = False
deprecation_warnings = False
EOL
fi

# Ensure /etc/ansible/hosts is populated with localhost
if [ ! -s /etc/ansible/hosts ]; then
  echo "[local]" > /etc/ansible/hosts
  echo "localhost ansible_connection=local" >> /etc/ansible/hosts
fi

# Create example group and host variable files
touch /etc/ansible/group_vars/all.yml
touch /etc/ansible/group_vars/servers.yml
touch /etc/ansible/host_vars/web1.yml
touch /etc/ansible/host_vars/db1.yml

# Create placeholder playbooks
touch /etc/ansible/playbooks/deploy.yml
touch /etc/ansible/playbooks/update_system.yml

# Create playbook to execute a script
cat > /etc/ansible/playbooks/test_exec_script.yml <<EOL
---
- name: Execute a script on localhost
  hosts: localhost
  tasks:
    - name: Run the script
      command: /root/scripts/test_script.sh
      register: script_output

    - name: Display script output
      debug:
        msg: "{{ script_output.stdout }}"
EOL

# Create the test script in /root/scripts/ if the directory does not exist
if [ ! -d /root/scripts ]; then
  mkdir -p /root/scripts
fi
echo -e "#!/bin/bash\necho \"Ansible executed this script!\"" > /root/scripts/test_script.sh
chmod +x /root/scripts/test_script.sh

# Create an empty log file with correct permissions
touch /var/log/ansible.log
chmod 644 /var/log/ansible.log

echo "Ansible directory setup complete!"
echo "To test the script manually, run: /root/scripts/test_script.sh"
echo "To execute it via Ansible, run: ansible-playbook -i /etc/ansible/hosts /etc/ansible/playbooks/test_exec_script.yml"

/etc/ansible/hosts

[d12]
server1
server2
[d11]
server3 
server4

/etc/ansible/playbooks/omz-load_enable_plugins.yml

# omz-load_enable_plugins.yml
- hosts: d12
 become: yes
 tasks:
   - name: Load and enable multiple Oh My Zsh plugins
     shell: |
       source ~/.zshrc
       PLUGIN="debian fzf grc common-aliases colored-man-pages extract zoxide systemd"
       omz plugin enable $PLUGIN
       omz reload
     args:
       executable: /bin/zsh
  • aplaybook ~/ansible/playbooks/omz-load_enable_plugins.yml