1.文件架构
[root@master template]# tree
.
├── jinj2_test.yml
├── meta
├── tasks
├── templates
│ └── test3.j2
└── vars
2. test3.j2的内容
[root@master template]# cat templates/test3.j2
{% if PORT %}
bind_address={{ansible_eth0.ipv4.address}}:{{ PORT }}
{% else %}
bind_address={{ansible_eth0.ipv4.address}}:3306
{% endif %} my_ip is: {{ansible_eth0.ipv4.address}}
3. jinj2_test.yml的内容
[root@master template]# cat jinj2_test.yml
---
- hosts: test
user: root
gather_facts: true
vars:
PORT: 3136
tasks:
- name: copy file to client
template: src=/roles/template/templates/test3.j2 dest=/root/my.cnf
4.运行的结果
[root@master template]# ansible test -a 'cat /root/my.cnf'
10.2.1.51 | CHANGED | rc=0 >>
bind_address=10.2.1.51:3136 my_ip is: 10.2.1.51 10.2.1.230 | CHANGED | rc=0 >>
bind_address=10.2.1.230:3136 my_ip is: 10.2.1.230
参考:https://blog.csdn.net/qqhappy8/article/details/79217380
https://blog.51cto.com/linuxg/1788574
#############################################################################
0. 文件架构
shell > tree /etc/ansible/roles/install_zabbix_agent/ ├── files
│ └── zabbix-2.4..tar.gz
├── tasks
│ └── main.yml
├── templates
│ ├── zabbix_agentd
│ └── zabbix_agentd.conf
└── vars
└── main.yml ## 建立 files 目录,存放编译安装过的 zabbix_agent 目录的压缩文件,用于拷贝到远程主机
## 建立 tasks 目录,用于编写将要执行的任务
## 建立 templates 目录,用于存放可变的模板文件
## 建立 vars 目录,用于存放变量信息
1.yaml文件
shell > cat /etc/ansible/roles/install_zabbix_agent/tasks/main.yml ---
- name: Install Software
yum: name={{ item }} state=latest
with_items:
- libcurl-devel
- name: Create Zabbix User
user: name={{ zabbix_user }} state=present createhome=no shell=/sbin/nologin
- name: Copy Zabbix.tar.gz
copy: src=zabbix-{{ zabbix_version }}.tar.gz dest={{ zabbix_dir }}/src/zabbix-{{ zabbix_version }}.tar.gz owner=root group=root
- name: Uncompression Zabbix.tar.gz
shell: tar zxf {{ zabbix_dir }}/src/zabbix-{{ zabbix_version }}.tar.gz -C {{ zabbix_dir }}/
- name: Copy Zabbix Start Script
template: src=zabbix_agentd dest=/etc/init.d/zabbix_agentd owner=root group=root mode=
- name: Copy Zabbix Config File
template: src=zabbix_agentd.conf dest={{ zabbix_dir }}/zabbix/etc/zabbix_agentd.conf owner={{ zabbix_user }} group={{ zabbix_user }} mode=
- name: Modify Zabbix Dir Permisson
file: path={{ zabbix_dir }}/zabbix owner={{ zabbix_user }} group={{ zabbix_user }} mode= recurse=yes
- name: Start Zabbix Service
shell: /etc/init.d/zabbix_agentd start
- name: Add Boot Start Zabbix Service
shell: chkconfig --level zabbix_agentd on
3.定义变量
shell > cat /etc/ansible/roles/install_zabbix_agent/vars/main.yml zabbix_dir: /usr/local
zabbix_version: 2.4.
zabbix_user: zabbix
zabbix_port:
zabbix_server_ip: 131.142.101.120
4.agent配置文件
shell > cat /etc/ansible/roles/install_zabbix_agent/templates/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
Server={{ zabbix_server_ip }}
ListenPort={{ zabbix_port }}
Hostname={{ ansible_all_ipv4_addresses[] }}
Timeout=
UnsafeUserParameters=
5.启动脚本
shell > cat /etc/ansible/roles/install_zabbix_agent/templates/zabbix_agentd
. /etc/init.d/functions
BASEDIR={{ zabbix_dir }}/zabbix
BINARY_NAME=zabbix_agentd
FULLPATH=$BASEDIR/sbin/$BINARY_NAME
PIDFILE=/tmp/$BINARY_NAME.pid
ERROR=
STOPPING=
if [ -f $PIDFILE ] && [ -s $PIDFILE ]
then
PID=`cat $PIDFILE`
if [ "x$PID" != "x" ] && kill - $PID >/dev/null && [ $BINARY_NAME == `ps -e | grep $PID | awk '{print $4}'` ]
then
STATUS="$BINARY_NAME (pid `pidof $APP`) running.."
RUNNING=
else
rm -f $PIDFILE
STATUS="$BINARY_NAME (pid file existed ($PID) and now removed) not running.."
RUNNING=
fi
else
if [ `ps -e | grep $BINARY_NAME | head - | awk '{ print $1 }'` ]
then
STATUS="$BINARY_NAME (pid `pidof $APP`, but no pid file) running.."
else
STATUS="$BINARY_NAME (no pid file) not running"
fi
RUNNING=
fi
start() {
if [ $RUNNING -eq ]
then
echo "$0 $ARG: $BINARY_NAME (pid $PID) already running"
else
action $"Starting $BINARY_NAME: " $FULLPATH
touch /var/lock/subsys/$BINARY_NAME
fi
}
stop() {
echo -n $"Shutting down $BINARY_NAME: "
killproc $BINARY_NAME
RETVAL=$?
echo
[ $RETVAL -eq ] && rm -f /var/lock/subsys/$BINARY_NAME
RUNNING=
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $BINARY_NAME
;;
restart)
stop
sleep
start
;;
help|*)
echo $"Usage: $0 {start|stop|status|restart|help}"
cat <<EOF
start - start $BINARY_NAME
stop - stop $BINARY_NAME
status - show current status of $BINARY_NAME
restart - restart $BINARY_NAME if running by sending a SIGHUP or start if not running
help - this screen
EOF
exit
;;
esac
exit
参考: https://www.cnblogs.com/wangxiaoqiangs/p/5685239.html