Original link: https://chegva.com/5725.html
-
Production Practice:
Use shell scripts to call ansible to perform batch operations
-
Study skills:
Shell script calls ansible, Ansible common modules use
-
Script content:
If there is no platform support in production, it is very common to operate remote hosts in batches in the central control. It is very convenient to use pssh if you have done ssh mutual trust authentication. mixed. pssh, ansible, saltstack, script, whichever is more applicable, or a combination. The following are several shell scripts that call ansible 2.9.25 to perform batch operations, which can be optimized as you like.
ansilbe_shell.sh : Use the ansible shell module to execute commands in batches
#!/bin/bash #set -x set -e if [ $# -lt 2 ]; then echo "Usage: ansible_shell <host1,hosts2,hostgroup> <commands>" exit the fi hosts="*$1" shift 1 commands="$*" #commands=${*:2} # ansible_sudo_pass can also be written in the ansible host configuration file, there is no need to add -e to write if [ -n "${commands}" ]; then ansible $hosts -m shell -a "$commands;echo ' '" -e "ansible_sudo_pass=xxx" 2>/dev/null the fi exit
ansible_sync.sh : Use the ansible synchronize module to push files or folders to remote hosts in batches, or pull remote files back to the local
#!/bin/bash #################################################### ############################### # $Name: ansible_syncfile.sh # $Version: v1.0 # $Function: Used to synchronize host's files # $Author: Zhihe An # $Create Date: 20230524 #################################################### ############################### set -e if [[ $# -lt 4 || "$1" == "-h" || "$1" == "--help" || -z "$1" ]]; then echo "Usage: ansible_syncfile <host1,hosts2,hostgroup> <src file> <dst file> <mode>[pull|push]" exit the fi hosts="$1" # Get the execution host list host_list=$(ansible $hosts --list-hosts 2>/dev/null | sed '1d') src_file="$2" dest_file="$3" sync_mode="$4" # Pay attention to the mode parameter: specify the direction of synchronization. In push mode, the local host or delegate is the source; in pull mode, the remote host in the context is the source # The rsync synchronization method is push, pull, and the default is push push, from The local machine pushes to the remote host, from the "control machine" to the "target machine"; pull means to get the file from the remote host, from the "target machine" to the "control machine" if [[ "$sync_mode" == "pull" && -e "$dest_file" ]]; then # Pull the remote file to the specified directory if [[ -n $host_list ]]; then for host in ${host_list[@]}; do [[ ! -e "$dest_file/$host" ]] && mkdir -p $dest_file/$host ansible $host -m synchronize -a "src=$src_file dest=$dest_file/$host/ mode=pull" -e "ansible_sudo_pass=xxx" 2>/dev/null sleep 1 done [[ $? = 0 ]] && echo "The file download is complete, please enter $dest_file to view" the fi elif [[ "$sync_mode" == "push" && -e "$src_file" ]]; then # Push the file to the specified directory on the remote machine ansible $hosts -m synchronize -a "src=$src_file dest=$dest_file" -e "ansible_sudo_pass=xxx" 2>/dev/null [[ $? = 0 ]] && echo "file push complete" else echo "File synchronization failed, please check whether the file path exists or the host matches!" the fi exit
ansible_copy.sh : Use the ansible copy module to push files to remote hosts
#!/bin/bash set -e if [ $# -lt 3 ]; then echo "usage: $0 <inventory file> <src file> <dst file>" exit the fi inventory=$(readlink -fn $1) src_file="$2" dst_file="$3" if [ -e "$src_file" ]; then ansible -i $inventory all -m copy -a "src=$src_file dest=$dst_file owner=work group=work mode=0644" the fi exit
ansible_script.sh : Use the ansible script module to execute scripts on remote hosts
#!/bin/bash if [ $# -lt 3 ]; then echo "usage: $0 <inventory file> <all|host1,host2,host3> <script file>" exit the fi inventory=$(readlink -fn $1) hosts="$2" script_file=$(readlink -fn $3) if [ -f "$script_file" ]; then ansible -i $inventory $hosts -m script -a "$script_file" the fi
pk : Execute ansible-playbook , and the host list is passed to the playbook through the host_list variable
echo "[[ -f ~/.bash_aliases ]] && source ~/.bash_aliases" >> ~/.bash_profile cat >> ~/.bash_profile << EOF # Execute ansible playbook by anzhihe 20230522 pk() { if [[ $# -lt 2 || "$1" == "-h" || "$1" == "--help" || -z "$1" ]]; then echo "usage: pk <playbook file> <all|host1,hosts2,hostgroup> [args]" # exit the fi pkfile="$1" hosts="$2" shift 2 args=$* if [ -f "${pkfile}" ]; then ansible-playbook ${pkfile} --extra-vars "ansible_sudo_pass=xxx" -e "host_list=$hosts" "$args" 2>/dev/null the fi } function nps() { if [[ "$1" == "-h" || "$1" == "--help" || -z "$1" ]]; then python3 /home/anzhihe/op/python/ops.py -h 2>/dev/null else python3 /home/anzhihe/op/python/ops.py -nps $1 2>/dev/null the fi } EOF source ~/.bash_profile && source ~/.bash_aliases
refer to:
-
How to get multiple files from remote machine to local using Ansible
-
Ansible common module file operation copy|file file copy and creation
This article is transferred from: https://chegva.com/5725.html
This site is only for collection, and the copyright belongs to the original author.