ansible配置
cat <<END>ansible.cfg
[defaults]
inventory = /etc/ansible/hosts
host_key_checking = False
remote_port = 22
timeout = 10
END
cat <<END> Dockerfile
FROM alpine:latest
ENV TZ='Asia/Shanghai'
ENV TIMEZONE Asia/Shanghai
RUN apk update && apk upgrade && \
apk add tzdata bash-doc bash && \
apk add ansible openssh sshpass && \
mkdir -p /etc/ansible && \
echo "localhost" >/etc/ansible/hosts && \
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa && \
ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime && \
echo $TIMEZONE > /etc/timezone && \
rm -rf /var/cache/apk/*
ADD ansible.cfg /etc/ansible/ansible.cfg
END
可以直接使用我的镜像
registry.cn-shenzhen.aliyuncs.com/jbjb/jar2:ansible
hk
registry.cn-hongkong.aliyuncs.com/jbjb/hkj:ansible
加入Jenkins
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: some-label-value
spec:
containers:
- name: alpine
image: ansible:v03
command:
- cat
tty: true
"""
}
}
options{ timestamps () }
stages {
stage('shelshell ') {
steps {
container('alpine') {
sh '''
echo "172.31.3.61 ansible_ssh_user=root ansible_ssh_pass='123'" >> /etc/ansible/hosts
echo "172.31.3.57 ansible_ssh_user=root ansible_ssh_pass='123'" >> /etc/ansible/hosts
cat /etc/ansible/hosts
ansible 172.31.3.61,172.31.3.57 -m shell -a 'hostnamectl'
'''
}
}
}
}
}
或者使用凭据
可参考以下链接配置凭据
https://blog.csdn.net/weixin_42562106/article/details/110857813
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: some-label-value
spec:
containers:
- name: alpine
image: registry.cn-shenzhen.aliyuncs.com/jbjb/jar2:ansible
command:
- cat
tty: true
"""
}
}
options{ timestamps () }
stages {
stage('shell ') {
steps {
// checkout([$class: 'GitSCM', branches: [[name: '*/dev']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'abc123', url: 'http://JBJB。ocm/hehuan/bed-api.git']]])
container('alpine') {
withCredentials([usernamePassword(credentialsId: 'system', passwordVariable: 'Password', usernameVariable: 'Username')]) {
sh '''
echo "192.168.1.62 ansible_ssh_user=$Username ansible_ssh_pass=$Password" > /etc/ansible/hosts
echo "192.168.1.63 ansible_ssh_user=$Username ansible_ssh_pass=$Password" >> /etc/ansible/hosts
echo "192.168.1.65 ansible_ssh_user=$Username ansible_ssh_pass=$Password" >> /etc/ansible/hosts
cat /etc/ansible/hosts
ansible all -m shell -a "date"
ansible all -m shell -a "cat /etc/resolv.conf"
'''
}
}
}
}
}
}
常用命令
执行命令
ansible all -a "cat /etc/passwd "
使用shell支持管道\
ansible all -m shell -a "cat /etc/passwd | grep httpd"
copy前将原文件进行备份
ansible all -m copy -a "src=/etc/passwd dest=/tmp/passwd backup=yes"
给予用户和组
ansible all -m copy -a "src=/etc/passwd dest=/tmp/passwd backup=yes owner=daemon group=daemon"
给予权限
ansible all -m copy -a "src=/etc/passwd dest=/tmp/passwd backup=yes mode=0700"
安装多个包 yum模块
present 确认已经安装,但不升级
ansible all -m yum -a "name=lsof,httpd state=present"
installed 确认已经安装
ansible all -m yum -a "name=lsof,httpd state=installed"
systemd模块(started|stoped|restarted|reloaded
重新加载systemd
ansible all -m systemd -a "daemon_reload=yes"
启动httpd
ansible all -m systemd -a "name=httpd state=started"
开机启动
ansible all -m systemd -a "name=httpd enabled=yes"
自动解压
ansible test -m unarchive -a 'src=/ok/test/php.tar.gz dest=/tmp/ copy=yes owner=qwe group=qwe'