ansible在远程主机上进行文件内容替换可以使用lineinfile来实现,可以使用正则筛选到所需要的行,然后进行替换,本文进行了演示。
ansible在远程主机上运行多个command,标准写法为
- name: install an apple
command: "{{item}}"
with_items:
- tar -xvzf /tmp/apple.tar.gz creates=/tmp/apple
- /tmp/apple/install.sh --default creates=/etc/apple
- rm -rf /tmp/apple
实例
假设有一个process.ymal
- hosts: new
user: admin
vars:
ps_env: "{{ ps_env }}"
tasks:
- name: push ~/.ssh/id_rsa/pub to remote .ssh/authorized_keys
authorized_key: user=admin state=present key="{{ lookup('file', '/home/admin/.ssh/id_rsa.pub') }}"
- name: push ump_agent_code to remote
copy: src=/opt/zhukun/ump_agent/bin dest=/home/admin/zhukun owner=admin group=admin mode=644 backup=yes follow=yes
- name: prepare the product/test enviroment of PS
command: sed -i "s,stage,{{ ps_env }},g" /home/admin/zhukun/bin/config.json
- name: prepare the product/test enviroment of PS
lineinfile:
dest: /home/admin/zhukun/bin/config.json
regexp: '^.*[PRODUCT].*$'
line: '"PRODUCT": "{{ ps_env }}"'
- name: reboot the observer-agent service
command: "{{ item }}"
with_items:
- ps -ef | grep /export/App/observer-agent.jd.local/bin | grep -v grep | awk '{ print $2}' | xargs kill
- /export/App/observer-agent.jd.local/bin/start.sh
- name: reboot the PS service
command: ps -ef | grep /export/Packages/prediction_service/latest/prediction_service | grep -v grep | awk '{ print $2}' | xargs kill
Leave a Comment