teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

情報追加

2020/10/13 12:12

投稿

ntaniuch
ntaniuch

スコア12

title CHANGED
File without changes
body CHANGED
@@ -155,7 +155,7 @@
155
155
 
156
156
  以下のようにインベントリファイルに変数まで書き、group_varsを読みに行かなくてもよいようにすることでPlaybookは実行することが出来ました。
157
157
 
158
- こちらgroup_varsをコマンドから明示的に指定する方法はあるのでしょうか?
158
+ こちらgroup_varsを読ませるためのうまい書き方か、最悪コマンドから明示的に指定する方法はあるのでしょうか?
159
159
  またそもそも何故group_varsを読んでくれないのでしょうか?
160
160
 
161
161
  $ cat inventory/fact.ini

4

情報追加

2020/10/13 12:12

投稿

ntaniuch
ntaniuch

スコア12

title CHANGED
File without changes
body CHANGED
@@ -17,8 +17,7 @@
17
17
 
18
18
  # 問題
19
19
 
20
- やりたい構成でやってみると Permission denied となってまう
20
+ やりたい構成でやってみると group_vars に設定た認証情報を読んでくれない
21
- (元の構成にすると当然問題ない)
22
21
 
23
22
  ```
24
23
  fatal: [<サーバー名>]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '<サーバー名>,<サーバーIP>' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic).", "unreachable": true}
@@ -152,4 +151,22 @@
152
151
  <<ターゲットノード名>> (0, b'/home/opc\n', b"Warning: Permanently added '<ターゲットノード名>,<IP>' (ECDSA) to the list of known hosts.\r\n")
153
152
  ```
154
153
 
154
+ # 問題の暫定対応策
155
+
156
+ 以下のようにインベントリファイルに変数まで書き、group_varsを読みに行かなくてもよいようにすることでPlaybookは実行することが出来ました。
157
+
158
+ こちらgroup_varsをコマンドから明示的に指定する方法はあるのでしょうか?
155
- **こうう場合調査の仕方のヒントだけも頂けると助ります**
159
+ またそもそも何故group_varsを読んでくれないのでしょう
160
+
161
+ $ cat inventory/fact.ini
162
+ ```
163
+ [prd_lin]
164
+ <ホストFQDN>
165
+
166
+ [prd_lin:vars]
167
+ ansible_user=opc
168
+ ansible_connection=ssh
169
+ ansible_port=22
170
+ ansible_ssh_private_key_file=<絶対パスで記載>
171
+ become_user=root
172
+ ```

3

情報追加

2020/10/13 11:57

投稿

ntaniuch
ntaniuch

スコア12

title CHANGED
File without changes
body CHANGED
@@ -125,6 +125,8 @@
125
125
 
126
126
  ■接続出来ない場合
127
127
 
128
+ ・group_varsに記載した秘密鍵や、ポート情報が表示されない。
129
+
128
130
  ```
129
131
  $ ansible-playbook -i inventory/fact.ini work/fact.yml -vvv
130
132
  ・・(中略)
@@ -134,10 +136,12 @@
134
136
  <<ターゲットノード名>> ESTABLISH SSH CONNECTION FOR USER: None
135
137
  <<ターゲットノード名>> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=<user home>/.ansible/cp/884b7719cc <ターゲットノード名> '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
136
138
  <<ターゲットノード名>> (255, b'', b"Warning: Permanently added '<ターゲットノード名>,<IP>' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic).\r\n")
139
+ ```
137
140
 
138
-
139
141
  ■接続出来る場合
140
142
 
143
+ ・group_varsに記載した秘密鍵や、ポート情報が表示される。
144
+
141
145
  ```
142
146
  $ ansible-playbook -i inventory/fact.ini fact.yml -vvv
143
147
  ・・(中略)
@@ -148,8 +152,4 @@
148
152
  <<ターゲットノード名>> (0, b'/home/opc\n', b"Warning: Permanently added '<ターゲットノード名>,<IP>' (ECDSA) to the list of known hosts.\r\n")
149
153
  ```
150
154
 
151
- group_varsに記載した秘密鍵が接続出来ない場合には出力されません。
152
- その他使用ポートなども。
153
-
154
-
155
155
  **こういう場合の調査の仕方のヒントだけでも頂けると助かります**

2

情報追加

2020/10/13 11:40

投稿

ntaniuch
ntaniuch

スコア12

title CHANGED
File without changes
body CHANGED
@@ -30,6 +30,7 @@
30
30
 
31
31
 
32
32
  ```
33
+ <top directory>
33
34
  |- group_vars/prd_lin.yml # 認証情報を格納
34
35
  |- inventory/<サーバ名>.ini # インベントリ。グループ名を記載して、group_varsから認証情報を取得
35
36
  |- ansible.cfg # 内容は後述
@@ -97,4 +98,58 @@
97
98
  host_key_checking = False
98
99
  ```
99
100
 
101
+ # 調べたこと
102
+
103
+ ansible.cfgはトップディレクトリ直下のものを使ってくれていそうです。
104
+ (挙動的にはトップレベルPlaybookと同じ階層ではなくて、ansible-playbookコマンドを叩いた階層のものを使ってくれていそうですね。)
105
+
106
+
107
+ ```
108
+ $ ansible-playbook -i inventory/fact.ini work/fact.yml -vvv
109
+ ansible-playbook 2.9.11
110
+ config file = <top directory>/ansible.cfg★
111
+ configured module search path = ['<user home>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
112
+ ansible python module location = <user home>/.local/lib/python3.6/site-packages/ansible
113
+ executable location = /usr/local/bin/ansible-playbook
114
+ python version = 3.6.8 (default, Aug 7 2019, 08:02:28) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39.0.1)]
115
+ Using <top directory>/ansible.cfg as config file★
116
+ host_list declined parsing <top directory>/inventory/fact.ini as it did not pass its verify_file() method
117
+ script declined parsing <top directory>/inventory/fact.ini as it did not pass its verify_file() method
118
+ auto declined parsing <top directory>/inventory/fact.ini as it did not pass its verify_file() method
119
+ yaml declined parsing <top directory>/inventory/fact.ini as it did not pass its verify_file() method
120
+ Parsed <top directory>/inventory/fact.ini inventory source with ini plugin
121
+ ```
122
+
123
+
124
+ そして接続出来ていない理由ですが、group_varsに記載した認証情報がうまく使えていないためのようです。
125
+
126
+ ■接続出来ない場合
127
+
128
+ ```
129
+ $ ansible-playbook -i inventory/fact.ini work/fact.yml -vvv
130
+ ・・(中略)
131
+ TASK [setup] ***********************************************************************************************************************************************************
132
+ task path: <top directory>/work/fact.yml:8
133
+ Tuesday 13 October 2020 20:21:18 +0900 (0:00:00.036) 0:00:00.036 *******
134
+ <<ターゲットノード名>> ESTABLISH SSH CONNECTION FOR USER: None
135
+ <<ターゲットノード名>> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=<user home>/.ansible/cp/884b7719cc <ターゲットノード名> '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
136
+ <<ターゲットノード名>> (255, b'', b"Warning: Permanently added '<ターゲットノード名>,<IP>' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic).\r\n")
137
+
138
+
139
+ ■接続出来る場合
140
+
141
+ ```
142
+ $ ansible-playbook -i inventory/fact.ini fact.yml -vvv
143
+ ・・(中略)
144
+ TASK [setup] ***********************************************************************************************************************************************************
145
+ Tuesday 13 October 2020 20:23:19 +0900 (0:00:00.038) 0:00:00.038 *******
146
+ <<ターゲットノード名>> ESTABLISH SSH CONNECTION FOR USER: opc
147
+ <<ターゲットノード名>> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ★Port=22 -o 'IdentityFile="<user home>/<秘密鍵相対パス>/<秘密鍵名>"'★ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="opc"' -o ConnectTimeout=10 -o ControlPath=<user home>/.ansible/cp/0fa6c29352 <ターゲットノード名> '/bin/sh -c '"'"'echo ~opc && sleep 0'"'"''
148
+ <<ターゲットノード名>> (0, b'/home/opc\n', b"Warning: Permanently added '<ターゲットノード名>,<IP>' (ECDSA) to the list of known hosts.\r\n")
149
+ ```
150
+
151
+ group_varsに記載した秘密鍵が接続出来ない場合には出力されません。
152
+ その他使用ポートなども。
153
+
154
+
100
155
  **こういう場合の調査の仕方のヒントだけでも頂けると助かります**

1

情報追加

2020/10/13 11:38

投稿

ntaniuch
ntaniuch

スコア12

title CHANGED
File without changes
body CHANGED
@@ -24,4 +24,77 @@
24
24
  fatal: [<サーバー名>]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '<サーバー名>,<サーバーIP>' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic).", "unreachable": true}
25
25
  ```
26
26
 
27
+ # 環境
28
+
29
+ こんなディレクトリ構成で実行しています。
30
+
31
+
32
+ ```
33
+ |- group_vars/prd_lin.yml # 認証情報を格納
34
+ |- inventory/<サーバ名>.ini # インベントリ。グループ名を記載して、group_varsから認証情報を取得
35
+ |- ansible.cfg # 内容は後述
36
+ |- work/fact.yml # 件の移動したいPlaybook
37
+ ```
38
+
39
+
40
+ それぞれ中身はこんな感じです。
41
+
42
+ > ansible-playbook -i inventory/fact.ini work/fact.yml
43
+
44
+ $ cat work/fact.yml
45
+ ```
46
+ # fact.yml
47
+ ---
48
+ - hosts: all
49
+ gather_facts: no
50
+ become: yes
51
+ become_user: opc
52
+ tasks:
53
+ - setup:
54
+
55
+ - debug: msg="{{ item }}"
56
+ loop:
57
+ - "{{ inventory_hostname }} : {{ ansible_nodename }} : {{ ansible_os_family }}"
58
+ ```
59
+
60
+ $ cat inventory/fact.ini
61
+ ```
62
+ # fact.ini
63
+
64
+ [prd_lin]
65
+ <ホストFQDN>
66
+ ```
67
+
68
+ $ cat group_vars/prd_lin.yml
69
+ ```
70
+ ansible_user: opc
71
+ ansible_connection: ssh
72
+ ansible_port: 22
73
+ ansible_ssh_private_key_file: <絶対パスで記載>
74
+ ```
75
+
76
+ $ cat ansible.cfg
77
+ ```
78
+ [defaults]
79
+ inventory_ignore_extensions = ~, .orig, .bak, .cfg, .retry, .pyc, .pyo, .md
80
+ interpreter_python = auto_silent
81
+ callback_whitelist = profile_tasks
82
+ log_path= ansible.log
83
+ remote_tmp = ~/.ansible/tmp
84
+ forks = 15
85
+ gathering = smart
86
+ fact_caching = jsonfile
87
+ fact_caching_connection = ./connect/facts
88
+ fact_caching_timeout = 3600
89
+ display_args_to_stdout = false
90
+
91
+ [privilege_escalation]
92
+ become = true
93
+
94
+ [ssh_connection]
95
+ control_path = %(directory)s/%%h-%%r
96
+ ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
97
+ host_key_checking = False
98
+ ```
99
+
27
100
  **こういう場合の調査の仕方のヒントだけでも頂けると助かります**