ansible-playbook相关
生活随笔
收集整理的這篇文章主要介紹了
ansible-playbook相关
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取目標主機的信息
ansible all -m setup -a "filter=ansible_os_family"獲取ip地址
# {{ ansible_eth0.ipv4.address }} 如: CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname={{ ansible_eth0.ipv4.address }}"定義變量
--- - hosts: allvars:username: chentiangangpasswd: {{ ansible_eth0.ipv4.address }}使用ip地址最后一段做my.cnf的server-id
- name: change my.cnf server-idshell: server_id=`echo {{ ansible_eth0.ipv4.address }} | awk -F "." '{print \$4}'` ; sed -i "s#server-id = 1#server-id = ${server_id}#g" /etc/my.cnf不執行僅測試
ansible-playbook test.yml -C安裝一個zabbix-agent
--- - hosts: newserverhandlers:- name: restart zabbix-agentservice: name=zabbix-agent state=restartedtasks:- name: install zabbix22-agentyum: name={{ item }} state=latest # -name: 刪除zabbix-agent # yum: name={{ item }} state=absentwith_items: zabbix22-agent- name: templatetemplate: src=/etc/ansible/roles/zabbix22-agent/templates/zabbix_agentd.conf dest=/etc/zabbix_agentd.conf owner=root group=root mode=0644notify: restart zabbix-agentwhen: ansible_os_family == "RedHat" and ansible_lsb.major_release == "7"- name: start zabbix-agentservice: name=zabbix-agent state=started檢查語法
ansible-playbook main.yml --syntax-check查看錯誤模塊
ansible-playbook main.yml --verbose要在運行之前查看哪些主機會受到影響,請執行以下操作:
ansible-playbook playbook.yml --list-hosts正則
jinja中的正則
這邊使用match和search關鍵字,匹配比較簡單,用來when判斷非常方便,直接貼一下官網代碼和鏈接。
指定遠程連接端口
--- - hosts: ecAppBport: 22tasks:- name: pingping:首次連接不輸yes的方法
打開/etc/ansible/ansible.cfg這一行的注釋
默認情況下,首次登陸一臺服務器,系統會提示是否要記住對端的指紋,用ansible也會這樣,這樣會導致需要手工輸入yes或no,ansible 才可以往下執行。如需避免這種情況,需要在 /etc/ansible/ansible.cfg 文件中設置 host_key_checking = False # uncomment this to disable SSH key host checking host_key_checking = False變量注冊
--- - name: 注冊變量shell: hostname | sed -r 's#(.*)(-)([A-Z])([0-9])#\1#g'register: pkgname# 通過切片取到最終想要的結果 - debug: "msg='{{ pkgname.stdout_lines[0] }}.war'"執行的時候不獲取主機信息(Gathering Facts)
--- - hosts: java*gather_facts: noroles:- log/log-privileges替換
- name: PMM | Fix nginx configreplace:dest: /etc/nginx/nginx.confregexp: '^(\s*)listen'replace: '\1#listen'ansible roles詳解
https://www.cnblogs.com/zhaojiankai/p/7655855.html
轉載于:https://www.cnblogs.com/Csir/p/8379499.html
總結
以上是生活随笔為你收集整理的ansible-playbook相关的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用threejs点云秀出酷炫的图片效果
- 下一篇: JMeter 保持sessionId