😊
Ansibleを試してみた(実行編)
Ansibleの実行
tag指定あり
例としてroleで指定したshow_conf_jのみをやってみる
実行前
ban@UoVb:~/ansible$ ls -l log/
合計 0
ban@UoVb:~/ansible$
ansible実行
ban@UoVb:~/ansible$ ansible-playbook -i inventory.ini junos_config.yml --tags show_conf_j
PLAY [Junos Configure] ******************************************************************************************************************
TASK [show_conf_j : show_conf] **********************************************************************************************************
ok: [vsrx1]
ok: [vsrx2]
TASK [show_conf_j : output] *************************************************************************************************************
changed: [vsrx1]
changed: [vsrx2]
PLAY RECAP ******************************************************************************************************************************
vsrx1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vsrx2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ban@UoVb:~/ansible$
実行後
ban@UoVb:~/ansible$ ls log
vsrx1_conf_202210142333 vsrx2_conf_202210142333
ban@UoVb:~/ansible$
ログファイルの中身
ban@UoVb:~/ansible$ cat log/vsrx1_conf_202210142333
## Last changed: 2022-10-14 13:50:10 UTC
version 12.1X47-D15.4;
system {
host-name vsrx1;
root-authentication {
(以下略)
ban@UoVb:~/ansible$ cat log/vsrx2_conf_202210142333
## Last changed: 2022-10-12 13:00:51 UTC
version 12.1X47-D15.4;
system {
host-name vsrx2;
root-authentication {
(以下略)
tags指定なし
tag抜きで素直に流してみる
実行前(show_conf_jで取ったログは消しておいた)
ban@UoVb:~/ansible$ ls -l log/
合計 0
ban@UoVb:~/ansible$
ansible実行
ban@UoVb:~/ansible$ ansible-playbook -i inventory.ini junos_config.yml
PLAY [Junos Configure] ******************************************************************************************************************
TASK [show_conf_j : show_conf] **********************************************************************************************************
ok: [vsrx2]
ok: [vsrx1]
TASK [show_conf_j : output] *************************************************************************************************************
changed: [vsrx1]
changed: [vsrx2]
TASK [show_ver_j : show_ver] ************************************************************************************************************
ok: [vsrx1]
ok: [vsrx2]
TASK [show_ver_j : output] **************************************************************************************************************
changed: [vsrx1]
changed: [vsrx2]
TASK [set_int_j : config interface] *****************************************************************************************************
fatal: [vsrx1]: FAILED! => {"changed": false, "module_stderr": "'NoneType' object has no attribute 'text'", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error"}
fatal: [vsrx2]: FAILED! => {"changed": false, "module_stderr": "'NoneType' object has no attribute 'text'", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error"}
PLAY RECAP ******************************************************************************************************************************
vsrx1 : ok=4 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
vsrx2 : ok=4 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
ban@UoVb:~/ansible$
実行後
ban@UoVb:~/ansible$ ls log
vsrx1_conf_202210142338 vsrx1_ver_202210142339 vsrx2_conf_202210142338 vsrx2_ver_202210142339
ban@UoVb:~/ansible$
そう。set_int_jでエラーが出た・・・。
ちなみにset_int_jの中身はこんな感じ
ban@UoVb:~/ansible$ cat roles/set_int_j/tasks/main.yml
---
- name: config interface
junipernetworks.junos.junos_config:
lines:
- set interfaces ge-0/0/3 description fromACT
backup: yes
backup_options:
filename: before.log
dir_path: /home/ban/ansible/log
register: result
- name: print
debug:
msg: "{{ result.stdout }}"
ban@UoVb:~/ansible$
junos_configでlist形式でコマンド書けばイケるんちゃうんかい
ここから超ハマった・・・。
解決策はこちらハマった編で
設定変更系ymlの修正
紆余曲折あり結局netcon/junos_configは諦めてnetwork_cli/cli_configに修正
修正後の構成
ban@UoVb:~/ansible$ tree --charset=C
.
|-- after
|-- ansible.cfg
|-- before
|-- group_vars
|-- inventory.ini
|-- junos_config.yml
|-- log
`-- roles
|-- set_int_j
| |-- tasks
| | `-- main.yml
| `-- vars
| `-- main.yml
|-- show_conf_j
| |-- tasks
| | `-- main.yml
| `-- vars
| `-- main.yml
`-- show_ver_j
|-- tasks
| `-- main.yml
`-- vars
`-- main.yml
14 directories, 9 files
ban@UoVb:~/ansible$
role/set_int_j/tasks/main.yml
ban@UoVb:~/ansible$ cat roles/set_int_j/tasks/main.yml
---
- name: config interface
cli_config:
config: set interfaces ge-0/0/3 description "testif"
backup: yes
backup_options:
filename: "{{ inventory_hostname }}_before_{{ lookup('pipe', 'date +%Y%m%d%H%M') }}"
dir_path: /home/ban/ansible/before
ban@UoVb:~/ansible$
role/set_int_j/vars/main.yml
ban@UoVb:~/ansible$ cat roles/set_int_j/vars/main.yml
---
ansible_become: true
ansible_become_method: enable
ansible_network_os: junos
ansible_password: **** #ファイル内にはベタ打ちしてる
ansible_user: ban
ansible_connection: network_cli #cli_configモジュールを使うのでプラグインはnetwork_cliにしている
ban@UoVb:~/ansible$
修正後の設定変更playbookの実行
実行してみる(vsrx1とvsrx2のge-0/0/3のdescriptionをtestifに設定)
実行前
ban@vsrx1> show interfaces descriptions
ban@vsrx1>
ban@vsrx2> show interfaces descriptions
ban@vsrx2>
ansible実行
ban@UoVb:~/ansible$ ansible-playbook -i inventory.ini junos_config.yml --tags set_int_j
PLAY [Junos Configure] ******************************************************************************************************************
TASK [set_int_j : config interface] *****************************************************************************************************
[WARNING]: To ensure idempotency and correct diff the input configuration lines should be similar to how they appear if present in the
running configuration on device including the indentation
changed: [vsrx2]
changed: [vsrx1]
PLAY RECAP ******************************************************************************************************************************
vsrx1 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vsrx2 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ban@UoVb:~/ansible$
実行後
ban@vsrx1> show interfaces descriptions
Interface Admin Link Description
ge-0/0/3 up up testif
ban@vsrx1>
ban@vsrx2> show interfaces descriptions
Interface Admin Link Description
ge-0/0/3 up up testif
ban@vsrx2>
ban@UoVb:~/ansible$ tree --charset=C
.
|-- after
|-- ansible.cfg
|-- before
| |-- vsrx1_before_202210151515
| `-- vsrx2_before_202210151515
|-- group_vars
|-- inventory.ini
|-- junos_config.yml
|-- log
`-- roles
|-- set_int_j
| |-- tasks
| | `-- main.yml
| `-- vars
| `-- main.yml
|-- show_conf_j
| |-- tasks
| | `-- main.yml
| `-- vars
| `-- main.yml
`-- show_ver_j
|-- tasks
| `-- main.yml
`-- vars
`-- main.yml
15 directories, 11 files
ban@UoVb:~/ansible$
ちゃんとbeforeに吐かれてるのであとはshow_conf_jを取ってdiffったりすると変更箇所の比較が出来たりする
ちなみにsetをdeleteにするとちゃんと設定削除出来る
Discussion