2015-12-08 41 views
1

信封是:Ansible 1.9.4 1.9.2,Linux的CentOS的6.5Ansible - 循環with_fileglob - become_user不工作 - 運行源機器上行動

我有一個角色構建其中:

$貓的角色/建設/默認/ main.yml

--- 
build_user: confman 
build_group: confman 
tools_dir: ~/tools 


$貓的角色/建設/任務/ main.yml

- debug: msg="User is = {{ build_user }} -- {{ tools_dir }}" 
    tags: 
    - koba 

- name: Set directory ownership 
    file: path="{{ tools_dir }}" owner={{ build_user }} group={{ build_group }} mode=0755 state=directory recurse=yes 
    become_user: "{{ build_user }}" 
    tags: 
    - koba 

- name: Set private key file access 
    file: path="{{ item }}" owner={{ build_user }} group={{ build_group }} mode=0600 state=touch 
    with_fileglob: 
    - "{{ tools_dir }}/vmwaretools-lib-*/lib/insecure_private_key" 
# with_items: 
# - ~/tools/vmwaretools/lib/insecure_private_key 
    become_user: "{{ build_user }}" 
    tags: 
    - koba 

在我的工作區:主機文件(目錄)包含:

[ansible_servers] 
server01.project.jenkins 

site.yml(劇本)包含:我想運行以下命令:

$ ansible-playbook site.yml -i hosts -u confman --private-key ${DEPLOYER_KEY_FILE} -t koba 

我收到以下錯誤並由於某種原因,become_user在Ansible同時使用Ansible循環:with_fileglob使用confman用戶的(主目錄)(其設置在變量{{build_user}},而不是那個,它選擇我自己的用戶ID(c123456)。

在調試操作的控制檯輸出中,很明顯用戶(由於become_user)是confman,而tools_dir變量的值是〜/ tools。

PLAY [ansible_servers] ******************************************************** 

GATHERING FACTS *************************************************************** 
ok: [server01.project.jenkins] 

TASK: [build | debug msg="User is = {{ build_user }} -- {{ tools_dir }}"] ***** 
ok: [server01.project.jenkins] => { 
    "msg": "User is = confman -- ~/tools" 
} 

TASK: [build | Set directory ownership] *************************************** 
changed: [server01.project.jenkins] 

TASK: [build | Set private key file access] *********************************** 
failed: [server01.project.jenkins] => (item=/user/home/c123456/tools/vmwaretools-lib-1.0.8-SNAPSHOT/lib/insecure_private_key) => {"failed": true, "item": "/user/home/c123456/tools/vmwaretools-lib-1.0.8-SNAPSHOT/lib/insecure_private_key", "parsed": false} 
BECOME-SUCCESS-ajtxlfymjcquzuolgfrrxbssfolqgrsg 
Traceback (most recent call last): 
    File "/tmp/ansible-tmp-1449615824.69-82085663620220/file", line 1994, in <module> 
    main() 
    File "/tmp/ansible-tmp-1449615824.69-82085663620220/file", line 372, in main 
    open(path, 'w').close() 
IOError: [Errno 2] No such file or directory: '/user/home/c123456/tools/vmwaretools-lib-1.0.8-SNAPSHOT/lib/insecure_private_key' 
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013 
debug1: Reading configuration data /etc/ssh/ssh_config 
debug1: Applying options for * 
debug1: auto-mux: Trying existing master 
debug1: mux_client_request_session: master session id: 2 
debug1: mux_client_request_session: master session id: 2 
Shared connection to server01.project.jenkins closed. 

按照上述錯誤,該文件它試圖可變項目/user/home/c123456/tools/vmwaretools-lib-1.0.8-SNAPSHOT/lib/insecure_private_key但沒有這樣的文件在我的用戶標識的主目錄中。但是,該文件對於用戶co​​nfman的主目錄確實存在。

即存在以下文件。

/user/home/confman/tools/vmwaretools-lib-1.0.7-SNAPSHOT/lib/insecure_private_key 
/user/home/confman/tools/vmwaretools-lib-1.0.7/lib/insecure_private_key 
/user/home/confman/tools/vmwaretools-lib-1.0.8-SNAPSHOT/lib/insecure_private_key 

好,我想是在〜confman /工具/的vmwaretools-lib中遍歷這些文件 - * /包含私鑰文件,更改權限,但使用位置「with_fileglobbecome_user設置用戶的動作是不是工作。

如果我註釋掉with_fileglob部分和使用/取消註釋with_items在任務節/主。陽明海運,那麼它(become_user)工作正常,並拿起〜confman(而不是〜c123456),並給出了下面的輸出:

TASK: [build | Set private key file access] *********************************** 
changed: [server01.project.jenkins] => (item=~/tools/vmwaretools/lib/insecure_private_key) 

奇怪的事情我發現的是,沒有用戶c123456上目標機器(server01.project.jenkins)並告訴我with_fileglob正在使用源/本地/主Ansible機器(我運行ansible-playbook命令)來查找GLOB模式(而不是查找/運行它在server01.project.jenkins服務器上通過SSH),確實在本地/源Ansible機器上,我以c123456登錄。奇怪的是,在OUTPUT中,它仍然顯示目標機器,但模式路徑是根據上面的輸出從源機器發出的。

failed: [server01.project.jenkins] 

任何想法!我在這裏錯過了什麼?謝謝。

PS: - 我不希望設置tools_dir: 「〜{{build_user}} /工具」 或硬編碼它作爲一個用戶可以在命令行中傳遞tools_dir變量(在運行ansible-劇本命令使用-e/--extra-瓦爾「tools_dir = /生產/從/工具」

  • 進一步研究,我發現with_fileglob爲本地文件列表遍歷,使用殼描述fileglob符號(例如,/ playbooks/files/fooapp/*),那我應該用什麼迭代在目標/遠程服務器(server01.project.jenkins在我的情況)使用模式匹配(fileglob)?
+0

用with_fileglob發現更新了我的帖子。 –

回答

1

使用with_fileglob,它將始終運行在您正在運行ansible-playbook/ansible的本地/源/主機器上。 Ansible文檔爲循環不澄清此信息(http://docs.ansible.com/ansible/playbooks_loops.html#id4),但我發現這裏澄清在這裏:https://github.com/lorin/ansible-quickref

因此,當尋找模式,它正在挑選用戶c123456。

控制檯輸出顯示[server01.project.jenkins],因爲它是讀取庫存/主機文件中有什麼的不同處理/步驟。

我試圖用with_lines以及每個這篇文章:ansible: Is there something like with_fileglobs for files on remote machine?

但是,當我嘗試以下,但它仍然沒有工作即讀取本地機器,而不是目標機器上的圖案(Ansible文檔告訴with_items沒有本地計算機上,但在控制機器運行):

file: path="{{ item }}" .... 
    with_items: ls -1 {{ tools_dir }}/vmwaretools-lib-*/lib/insecure_private_key 
    become_user: {{ build_user }} 

最後解決這個問題,我只是去上普通的操作系統命令繞行外殼(同樣,這可能不是一個很如果目標環境不是Linux類型的操作系統,那麼這是一個很好的解決方案),但現在我是g洪水。

- name: Set private key file access 
    shell: "chmod 0400 {{ tools_dir }}/vmtools-lib-*/lib/insecure_private_key" 
    become_user: "{{ build_user }}" 
    tags: 
    - koba