Created
June 15, 2019 21:59
-
-
Save TanAlex/55187f10a10ed90d1e1bb7d33c4d42d4 to your computer and use it in GitHub Desktop.
[ansible note]Ansible Notes #ansible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - name: Download files | |
| get_url: | |
| url: "{{item.url}}" | |
| dest: "/var/www/html/{{item.dest}}" | |
| with_items: [{"url": "https://archive.apache.org/dist/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.tar.gz", "dest": "server1.tar.gz"}, | |
| {"url": "https://archive.apache.org/dist/ant/binaries/apache-ant-1.10.1-bin.zip", "dest": "server2.zip"}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Setup params.json for tasks configs | |
| [ | |
| { | |
| "host": "server1", | |
| "url": "http://192.168.33.10/server1.tar.gz", | |
| "target": "/var/target" | |
| }, | |
| { | |
| "host": "server2", | |
| "url": "http://192.168.33.10/server2.zip", | |
| "target": "/var/target" | |
| } | |
| ] | |
| # the task is for all server, use ansible_hostname to get which server it's running | |
| # use lookup('file', 'params.json') to get the param json | |
| # use json_query to get the param looking for | |
| # use when url_parm | match to only run the unzip when it's a zip file URL | |
| --- | |
| - hosts: servers | |
| become: yes | |
| vars: | |
| hostname: "{{ansible_hostname}}" | |
| params: "{{lookup('file', 'params.json')}}" | |
| url_query: "[?host=='{{hostname}}'].url" | |
| url_param: "{{(params|json_query(url_query))[0]}}" | |
| target_query: "[?host=='{{hostname}}'].target" | |
| target_param: "{{(params|json_query(target_query))[0]}}" | |
| tasks: | |
| - name: Create the target directory if it doesn't exist | |
| file: | |
| path: "{{target_param}}" | |
| state: directory | |
| - name: Install unzip | |
| apt: | |
| name: unzip | |
| update_cache: yes | |
| when: url_param | match(".*\.zip$") | |
| - name: Unarchive from url | |
| unarchive: | |
| src: "{{url_param}}" | |
| dest: "{{target_param}}" | |
| remote_src: yes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment