This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
сервис_ansible [2024/11/29 13:39] val [Использование шаблонов] |
сервис_ansible [2025/09/21 05:59] (current) val [ansible-vault] |
||
|---|---|---|---|
| Line 11: | Line 11: | ||
| * [[https://www.cisco.com/c/dam/m/ru_ru/training-events/2019/cisco-connect/pdf/introduction_automation_with_ansible_idrey.pdf|Введение в автоматизацию с помощью Ansible (Cisco)]] | * [[https://www.cisco.com/c/dam/m/ru_ru/training-events/2019/cisco-connect/pdf/introduction_automation_with_ansible_idrey.pdf|Введение в автоматизацию с помощью Ansible (Cisco)]] | ||
| * [[https://nwmichl.net/2020/02/24/first-simple-ansible-playbooks/|First simple Ansible playbooks Cisco IOS]] | * [[https://nwmichl.net/2020/02/24/first-simple-ansible-playbooks/|First simple Ansible playbooks Cisco IOS]] | ||
| + | |||
| + | * [[https://r4ven.me/it-razdel/instrukcii/ansible-cmdb-strukturizacziya-i-vizualizacziya-ansible-facts/|ansible-cmdb — программа на Python, которая структуризирует собранные Ansible facts и визуализирует их с помощью HTML с красивым форматированием]] | ||
| Line 62: | Line 64: | ||
| [sws] | [sws] | ||
| - | switch[1:3] ansible_ssh_user=root ansible_ssh_pass=cisco #ansible_network_os=ios | + | switch[1:3] ansible_ssh_user=root ansible_ssh_pass=cisco ansible_network_os=ios |
| [nodes] | [nodes] | ||
| Line 108: | Line 110: | ||
| node1# ansible all -m ping | node1# ansible all -m ping | ||
| node1# ansible all -m ping -i inv_file.ini | node1# ansible all -m ping -i inv_file.ini | ||
| - | node1# ansible all -m ping -i node2:2222, -e "ansible_python_interpreter=/usr/bin/python3" | + | node1# ansible all -m ping -i node2:2222, |
| node1# ansible corpX -m command -a 'uname -a' | node1# ansible corpX -m command -a 'uname -a' | ||
| - | kube1# ansible kubes -a 'sed -i"" -e "/swap/s/^/#/" /etc/fstab' | ||
| - | kube1# ansible kubes -a 'swapoff -a' | ||
| - | ИЛИ | ||
| (venv1) server# ansible all -a 'sed -i"" -e "/swap/s/^/#/" /etc/fstab' -i /root/kubespray/inventory/mycluster/hosts.yaml #--limit=kube4,kube5 | (venv1) server# ansible all -a 'sed -i"" -e "/swap/s/^/#/" /etc/fstab' -i /root/kubespray/inventory/mycluster/hosts.yaml #--limit=kube4,kube5 | ||
| (venv1) server# ansible all -a 'swapoff -a' -i /root/kubespray/inventory/mycluster/hosts.yaml #--limit=kube4 | (venv1) server# ansible all -a 'swapoff -a' -i /root/kubespray/inventory/mycluster/hosts.yaml #--limit=kube4 | ||
| Line 316: | Line 315: | ||
| <code> | <code> | ||
| node1# ansible -m setup corpX | node1# ansible -m setup corpX | ||
| + | |||
| + | server# ansible all -m setup -i gate, | ||
| node1# ansible -m setup corpX | grep ansible_fqdn | node1# ansible -m setup corpX | grep ansible_fqdn | ||
| Line 325: | Line 326: | ||
| <h1> | <h1> | ||
| Hello from {{ ansible_fqdn }} | Hello from {{ ansible_fqdn }} | ||
| - | {# | ||
| - | Comment | ||
| - | #} | ||
| </h1> | </h1> | ||
| </body> | </body> | ||
| </html> | </html> | ||
| </code><code> | </code><code> | ||
| - | node1# cat inst_apache.yml | + | node1# cat inst_http.yml |
| </code><code> | </code><code> | ||
| - hosts: corpX | - hosts: corpX | ||
| + | #- hosts: all | ||
| + | |||
| tasks: | tasks: | ||
| - | - name: Installs apache web server | + | - name: Install or remove web server |
| apt: pkg=apache2 state=present update_cache=true | apt: pkg=apache2 state=present update_cache=true | ||
| + | # apt: pkg=nginx state=absent update_cache=true | ||
| - name: Create index.html file | - name: Create index.html file | ||
| template: src=index.html.j2 dest=/var/www/html/index.html | template: src=index.html.j2 dest=/var/www/html/index.html | ||
| </code><code> | </code><code> | ||
| - | node1# ansible-playbook inst_apache.yml | + | node1# ansible-playbook inst_http.yml |
| - | </code><code> | + | |
| + | server# ansible-playbook inst_http.yml -i gate, | ||
| + | </code> | ||
| + | |||
| + | ==== Asterisk IAX конфигурация ==== | ||
| + | |||
| + | <code> | ||
| # cat iax.conf.j2 | # cat iax.conf.j2 | ||
| </code><code> | </code><code> | ||
| Line 349: | Line 356: | ||
| disallow=all | disallow=all | ||
| allow=alaw | allow=alaw | ||
| + | |||
| + | {# | ||
| + | Comment | ||
| + | #} | ||
| {% for Y in YS %} | {% for Y in YS %} | ||
| Line 377: | Line 388: | ||
| </code><code> | </code><code> | ||
| # ansible-playbook ast_iax_corps.yml --extra-vars '{"X":"{{ ansible_eth0.ipv4.address.split(\".\")[3] }}","YS":[1,2,3,4,5,6,7,8,9,10,11,12,13]}' | # ansible-playbook ast_iax_corps.yml --extra-vars '{"X":"{{ ansible_eth0.ipv4.address.split(\".\")[3] }}","YS":[1,2,3,4,5,6,7,8,9,10,11,12,13]}' | ||
| + | </code> | ||
| + | |||
| + | ==== Провижининг IP телефонов ==== | ||
| + | <code> | ||
| + | ~/phone-prov# cat inventory.yml | ||
| + | </code><code> | ||
| + | all: | ||
| + | vars: | ||
| + | phones: | ||
| + | - [ '403', 'tpassword403', 'server.corp13.un', '000E08C190C2', 'spa' ] | ||
| + | - [ '404', 'tpassword404', 'server.corp13.un', 'BCC34221709A', 'kx-hdv' ] | ||
| + | </code><code> | ||
| + | ~/phone-prov# cat kx-hdv.j2 | ||
| + | </code><code> | ||
| + | # Panasonic SIP Phone Standard Format File # | ||
| + | |||
| + | ## SIP Settings | ||
| + | PHONE_NUMBER_1="{{ pn }}" | ||
| + | SIP_AUTHID_1="{{ pn }}" | ||
| + | SIP_PASS_1="{{ secret }}" | ||
| + | SIP_PRXY_ADDR_1="{{ sipproxy }}" | ||
| + | SIP_RGSTR_ADDR_1="{{ sipproxy }}" | ||
| + | DIAL_PLAN_1="*xx|[1-4]xx|0xxxxx|8xxxxxxxxxx" | ||
| + | </code><code> | ||
| + | ~/phone-prov# cat spa.j2 | ||
| + | </code><code> | ||
| + | <flat-profile> | ||
| + | <Proxy_1_>{{ sipproxy }}</Proxy_1_> | ||
| + | <User_ID_1_>{{ pn }}</User_ID_1_> | ||
| + | <Password_1_>{{ secret }}</Password_1_> | ||
| + | <Dial_Plan_1_>( xxx | 8xxxxxxxxxx | 0xxxxx | *xx )</Dial_Plan_1_> | ||
| + | </flat-profile> | ||
| + | </code><code> | ||
| + | ~/phone-prov# cat phone-prov.yml | ||
| + | </code><code> | ||
| + | - hosts: localhost | ||
| + | tasks: | ||
| + | - name: debug | ||
| + | debug: | ||
| + | msg: "{{ item.0 }} ... {{ item.4 }}" | ||
| + | loop: "{{ phones }}" | ||
| + | - name: Create phone conf | ||
| + | template: | ||
| + | src: "{{ model }}.j2" | ||
| + | dest: "/var/www/html/{{ model }}-{{ mac }}.cfg" | ||
| + | vars: | ||
| + | pn: "{{ item.0 }}" | ||
| + | secret: "{{ item.1 }}" | ||
| + | sipproxy: "{{ item.2 }}" | ||
| + | mac: "{{ item.3 }}" | ||
| + | model: "{{ item.4 }}" | ||
| + | loop: "{{ phones }}" | ||
| + | </code><code> | ||
| + | ~/phone-prov# ansible-playbook phone-prov.yml -i inventory.yml | ||
| </code> | </code> | ||
| ===== Использование handlers ===== | ===== Использование handlers ===== | ||
| - | ==== Пример 1 ==== | + | ==== Пример 4 ==== |
| * [[Сервис HTTP#Использование домашних каталогов]] | * [[Сервис HTTP#Использование домашних каталогов]] | ||
| Line 404: | Line 469: | ||
| </code> | </code> | ||
| - | ==== Пример 2 ==== | + | ==== Пример 5 ==== |
| + | |||
| + | * [[Управление ПО в Linux#Список desktop приложений]] | ||
| <code> | <code> | ||
| Line 414: | Line 481: | ||
| UserParameter=listinstalledsoft,ls /usr/share/applications | awk -F '.desktop' ' { print $1}' - | UserParameter=listinstalledsoft,ls /usr/share/applications | awk -F '.desktop' ' { print $1}' - | ||
| </code><code> | </code><code> | ||
| - | node1# cat za.yml | + | server# cat za.yml |
| </code><code> | </code><code> | ||
| - hosts: lin_ws | - hosts: lin_ws | ||
| Line 451: | Line 518: | ||
| # hosts: kubes | # hosts: kubes | ||
| # hosts: "{{ variable_host | default('addnodes') }}" | # hosts: "{{ variable_host | default('addnodes') }}" | ||
| - | "{{ variable_host | default('addnodes') }}" | ||
| roles: | roles: | ||
| - node | - node | ||
| Line 522: | Line 588: | ||
| address {{ ansible_eth0.ipv4.address }} | address {{ ansible_eth0.ipv4.address }} | ||
| netmask 255.255.255.0 | netmask 255.255.255.0 | ||
| - | {% if variable_host == 'all' %} | + | {% if variable_host is defined %} |
| gateway 192.168.{{ X }}.1 | gateway 192.168.{{ X }}.1 | ||
| {% else %} | {% else %} | ||
| Line 540: | Line 606: | ||
| ==== Роль OpenVPN сервера ==== | ==== Роль OpenVPN сервера ==== | ||
| + | |||
| + | * [[Пакет OpenVPN]] | ||
| + | |||
| <code> | <code> | ||
| server:~# mkdir openvpn1 && cd openvpn1 | server:~# mkdir openvpn1 && cd openvpn1 | ||
| Line 630: | Line 699: | ||
| vars: | vars: | ||
| X: "{{ ansible_eth1.ipv4.address.split('.')[2] }}" | X: "{{ ansible_eth1.ipv4.address.split('.')[2] }}" | ||
| - | ansible_python_interpreter: "/usr/bin/python3" | ||
| ansible_ssh_user: vagrant | ansible_ssh_user: vagrant | ||
| ansible_ssh_pass: strongpassword | ansible_ssh_pass: strongpassword | ||
| Line 657: | Line 725: | ||
| </code><code> | </code><code> | ||
| server:~# wget https://val.bmstu.ru/unix/conf.git/conf/ansible/roles/openvpn1.tgz && tar -xvzf openvpn1.tgz && cd openvpn1 | server:~# wget https://val.bmstu.ru/unix/conf.git/conf/ansible/roles/openvpn1.tgz && tar -xvzf openvpn1.tgz && cd openvpn1 | ||
| + | |||
| + | server:~/openvpn1# ansible -m ping -i inventory.yaml all | ||
| server:~/openvpn1# ansible-playbook openvpn1.yaml -i inventory.yaml -e "variable_host=test_nodes" | server:~/openvpn1# ansible-playbook openvpn1.yaml -i inventory.yaml -e "variable_host=test_nodes" | ||
| Line 667: | Line 737: | ||
| * [[Сервисы Gateway и routing#Управление таблицей маршрутизации]] | * [[Сервисы Gateway и routing#Управление таблицей маршрутизации]] | ||
| + | ==== ansible-vault ==== | ||
| + | <code> | ||
| + | ~/openvpn1# less openvpn1/files/server.key | ||
| + | |||
| + | ~/openvpn1# ansible-vault encrypt openvpn1/files/server.key | ||
| + | </code><code> | ||
| + | New Vault password: 12345678 | ||
| + | Confirm New Vault password: 12345678 | ||
| + | Encryption successful | ||
| + | </code><code> | ||
| + | ~/openvpn1# less openvpn1/files/server.key | ||
| + | |||
| + | ~/openvpn1# ansible-vault view openvpn1/files/server.key | ||
| + | |||
| + | ~/openvpn1# ansible-vault encrypt_string strongpassword | ||
| + | </code><code> | ||
| + | New vault password (default): 12345678 | ||
| + | ... | ||
| + | </code><code> | ||
| + | Encryption successful | ||
| + | !vault | | ||
| + | $ANSIBLE_VAULT;1.1;AES256 | ||
| + | ... | ||
| + | 6234 | ||
| + | </code><code> | ||
| + | ~/openvpn1# cp inventory.yaml inventory2.yaml | ||
| + | |||
| + | ~/openvpn1# cat inventory2.yaml | ||
| + | </code> | ||
| + | !!! Никаких лишних пробелов в конце строк !!! | ||
| + | <code> | ||
| + | ... | ||
| + | ansible_ssh_pass: !vault | | ||
| + | $ANSIBLE_VAULT;1.1;AES256 | ||
| + | ... | ||
| + | 6234 | ||
| + | ... | ||
| + | </code><code> | ||
| + | ~/openvpn1# ANS_V_SEC=12345678 | ||
| + | |||
| + | ~/openvpn1# echo $ANS_V_SEC | ansible-playbook openvpn1.yaml -i inventory2.yaml -e "variable_host=test_nodes" --vault-password-file=/bin/cat | ||
| + | </code> | ||
| ==== Фрагмент роли с условиями и отладкой ==== | ==== Фрагмент роли с условиями и отладкой ==== | ||