2025年はansibleを少し詳しいインフラエンジニアを目指す
今回使用するバージョン
[root@docker163 ansible]# ansible --version
ansible [core 2.15.3]
config file = /ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.11/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.11.5 (main, Oct 25 2023, 14:45:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-21)] (/usr/bin/python3.11)
jinja version = 3.1.2
libyaml = True
[root@docker163 ansible]#
モジュール一覧を確認する
先人のお知恵を拝借する
ansible-doc -l
または
ansible-doc -l -j
ということでコマンド実行をansibleでやりたいとき
ansible-playbook -i inventory.ini command.yml
[root@docker163 ansible]# cat command.yml
---
- hosts: all
become: yes
tasks:
- shell: cat /etc/hosts
register: pwd_result
- debug:
msg: "{{ pwd_result }}"
アドホックなansibleコマンドを使いたいとき ping版
[root@docker163 ansible]# ansible -i inventory.ini mq1 -m ping
mq1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@docker163 ansible]# ansible -i inventory.ini mqservers -m ping
mq1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
mq2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
mq3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@docker163 ansible]#
アドホックでコマンド実行のやりかた 自由なコマンド実行
[root@docker163 ansible]# ansible -i inventory.ini mq1 -a "ls -l"
mq1 | CHANGED | rc=0 >>
合計 1060092
-rw-r--r-- 1 root root 818462024 6月 24 2023 IBM_MQ_9.1_LINUX_X86-64_TRIAL (1).tar.gz
drwxrwxr-x 7 226 12201 4096 7月 9 2018 MQServer
-rw-r--r-- 1 root root 159 12月 25 2021 aikawa.txt
-rw-------. 1 root root 1881 11月 18 2021 anaconda-ks.cfg
-rw------- 1 root root 1675 8月 10 21:02 id_rsa
-rw-r--r-- 1 root root 568 2月 5 2024 id_rsa.pub
-rw-r--r-- 1 root root 20672 8月 4 15:53 index.html
-rw-r--r-- 1 root root 20678 8月 4 15:53 index.html.1
-rw-r--r--. 1 root root 1974 11月 18 2021 initial-setup-ks.cfg
drwxr-xr-x 2 root root 94 11月 2 14:42 java
-rw-r--r-- 1 root root 270 2月 28 2022 mqstatuscheck.sh
-rw-r--r-- 1 root root 403 12月 25 2021 mqstatuscheck.sh.bak
-rw-r--r-- 1 root root 305 12月 25 2021 mqstatuscheck2.sh
-rw-r--r-- 1 root root 274 2月 28 2022 mqstatuscheck3.sh
-rw-r--r-- 1 root root 115 11月 19 2023 test.log
-rw-r--r-- 1 root root 89 11月 19 2023 test.sh
-rw-r--r-- 1 root root 266969170 12月 10 2023 wlp-webProfile8-java8-linux-x86_64-23.0.0.11.zip
[root@docker163 ansible]# ansible -i inventory.ini mq1 -a "cat /etc/hosts"
mq1 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.215 DB2
ansible.cfgを編集
[root@docker163 ansible]# cat ansible.cfg
[defaults]
# display_args_to_stdout = True
#forks = 20
log_path = /var/log/ansible.log
inventory = inventory.ini
そうするとansibleコマンド実行時に、-i インベントリファイルが不要
[root@docker163 ansible]# ansible mq1 -a "ls -l"
mq1 | CHANGED | rc=0 >>
合計 1060092
-rw-r--r-- 1 root root 818462024 6月 24 2023 IBM_MQ_9.1_LINUX_X86-64_TRIAL (1).tar.gz
drwxrwxr-x 7 226 12201 4096 7月 9 2018 MQServer
-rw-r--r-- 1 root root 159 12月 25 2021 aikawa.txt
-rw-------. 1 root root 1881 11月 18 2021 anaconda-ks.cfg
-rw------- 1 root root 1675 8月 10 21:02 id_rsa
-rw-r--r-- 1 root root 568 2月 5 2024 id_rsa.pub
-rw-r--r-- 1 root root 20672 8月 4 15:53 index.html
-rw-r--r-- 1 root root 20678 8月 4 15:53 index.html.1
-rw-r--r--. 1 root root 1974 11月 18 2021 initial-setup-ks.cfg
drwxr-xr-x 2 root root 94 11月 2 14:42 java
-rw-r--r-- 1 root root 270 2月 28 2022 mqstatuscheck.sh
-rw-r--r-- 1 root root 403 12月 25 2021 mqstatuscheck.sh.bak
-rw-r--r-- 1 root root 305 12月 25 2021 mqstatuscheck2.sh
-rw-r--r-- 1 root root 274 2月 28 2022 mqstatuscheck3.sh
-rw-r--r-- 1 root root 115 11月 19 2023 test.log
-rw-r--r-- 1 root root 89 11月 19 2023 test.sh
-rw-r--r-- 1 root root 266969170 12月 10 2023 wlp-webProfile8-java8-linux-x86_64-23.0.0.11.zip
[root@docker163 ansible]#
コマンドモジュールを指定した場合
[root@docker163 ansible]# ansible -i inventory.ini mqservers -m command -a "pwd"
mq2 | CHANGED | rc=0 >>
/root
mq3 | CHANGED | rc=0 >>
/root
mq1 | CHANGED | rc=0 >>
/root
commandモジュールあるなしの差がわかんない・・・・
[root@docker163 ansible]# ansible -i inventory.ini mqservers -a "pwd"
mq2 | CHANGED | rc=0 >>
/root
mq3 | CHANGED | rc=0 >>
/root
mq1 | CHANGED | rc=0 >>
/root