Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:ansible [2018/12/27 14:38] alfred [Facts] |
wiki2:ansible [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 58: | Línea 58: | ||
| * ''-m <module>'' exec that module. | * ''-m <module>'' exec that module. | ||
| * ''-a '<arguments>' '' arguments for the module | * ''-a '<arguments>' '' arguments for the module | ||
| - | |||
| - | === shell === | ||
| - | <code> | ||
| - | ansible all -b -m shell -a 'apt-get install nginx' | ||
| - | </code> | ||
| === apt === | === apt === | ||
| Línea 86: | Línea 81: | ||
| - htop | - htop | ||
| - tmux | - tmux | ||
| + | </code> | ||
| + | |||
| + | === copy === | ||
| + | |||
| + | <code> | ||
| + | - copy: | ||
| + | src: "{{ item }}" | ||
| + | dest: /etc/fooapp/ | ||
| + | owner: root | ||
| + | mode: 600 | ||
| + | with_fileglob: | ||
| + | - /playbooks/files/fooapp/* | ||
| + | </code> | ||
| + | |||
| + | <code> | ||
| + | - name: Your copy task | ||
| + | copy: src={{ item.src }} dest={{ item.dest }} | ||
| + | with_items: | ||
| + | - { src: 'containerizers', dest: '/etc/mesos/containerizers' } | ||
| + | - { src: 'another_file', dest: '/etc/somewhere' } | ||
| + | - { src: 'dynamic', dest: '{{ var_path }}' } | ||
| + | </code> | ||
| + | |||
| + | <code> | ||
| + | - name: Copy multiple files to multiple directories | ||
| + | copy: src={{ item.0 }} dest={{ item.1 }} | ||
| + | with_together: | ||
| + | - [ 'file1', 'file2', 'file3' ] | ||
| + | - [ '/dir1/', '/dir2/', '/dir3/' ] | ||
| + | </code> | ||
| + | |||
| + | === shell === | ||
| + | <code> | ||
| + | ansible all -b -m shell -a 'apt-get install nginx' | ||
| </code> | </code> | ||
| ==== Playbooks ==== | ==== Playbooks ==== | ||
| Línea 227: | Línea 256: | ||
| ===== Tips and tricks ===== | ===== Tips and tricks ===== | ||
| + | ==== Become ==== | ||
| + | <code> | ||
| + | --- | ||
| + | - hosts: webservers | ||
| + | remote_user: yourname | ||
| + | become: yes | ||
| + | become_user: postgres | ||
| + | </code> | ||
| + | You can also use other privilege escalation methods, like su: | ||
| + | <code> | ||
| + | --- | ||
| + | - hosts: webservers | ||
| + | remote_user: yourname | ||
| + | become: yes | ||
| + | become_method: su | ||
| + | </code> | ||
| ==== Ansible command ==== | ==== Ansible command ==== | ||
| Línea 260: | Línea 305: | ||
| </code> | </code> | ||
| + | In this way you can set variables: | ||
| + | <code> | ||
| + | - hosts: all | ||
| + | vars: | ||
| + | local_shell: "{{ lookup('env','SHELL') }}" | ||
| + | tasks: | ||
| + | - debug: | ||
| + | msg: "{{ local_shell }}" | ||
| + | output | ||
| + | ------ | ||
| + | "msg": "/bin/sh" | ||
| + | </code> | ||
| === For targetting proxies === | === For targetting proxies === | ||
| Línea 292: | Línea 349: | ||
| http_proxy: http://proxy.example.com:8080 | http_proxy: http://proxy.example.com:8080 | ||
| </code> | </code> | ||
| + | |||
| + | ==== Passing variables on the command line ==== | ||
| + | |||
| + | In addition to vars_prompt and vars_files, it is possible to set variables at the command line using the ''<nowiki>--extra-vars</nowiki>'' (or -e) argument. | ||
| + | <code> | ||
| + | ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo" | ||
| + | </code> | ||
| + | It also allows to set in JSON format: | ||
| + | <code> | ||
| + | ansible-playbook release.yml --extra-vars '{"version":"1.23.45","other_variable":"foo"}' | ||
| + | ansible-playbook arcade.yml --extra-vars '{"pacman":"mrs","ghosts":["inky","pinky","clyde","sue"]}' | ||
| + | </code> | ||
| + | Also in YAML string format: | ||
| + | <code> | ||
| + | ansible-playbook release.yml --extra-vars ' | ||
| + | version: "1.23.45" | ||
| + | other_variable: foo' | ||
| + | |||
| + | ansible-playbook arcade.yml --extra-vars ' | ||
| + | pacman: mrs | ||
| + | ghosts: | ||
| + | - inky | ||
| + | - pinky | ||
| + | - clyde | ||
| + | - sue' | ||
| + | </code> | ||
| + | |||
| + | Even vars from a JSON or YAML file: | ||
| + | <code> | ||
| + | ansible-playbook release.yml --extra-vars "@some_file.json" | ||
| + | </code> | ||
| + | |||
| + | ==== Executing locally ==== | ||
| + | ''local_action'' is an alternative way of doing ''delegate_to: localhost''. | ||
| Línea 313: | Línea 404: | ||
| [webservers] | [webservers] | ||
| webserver.awesomecompany.ly ansible_ssh_host=55.44.33.22 | webserver.awesomecompany.ly ansible_ssh_host=55.44.33.22 | ||
| + | </code> | ||
| + | |||
| + | === Split a command === | ||
| + | You can split this... | ||
| + | <code> | ||
| + | - name: Find geographical region of this server | ||
| + | local_action: uri url=http://locator/studio/{{ ansible_default_ipv4.address}} method=GET return_content=yes register=locator_output | ||
| + | </code> | ||
| + | ... in this: | ||
| + | <code> | ||
| + | - name: Find geographical region of this server | ||
| + | uri: | ||
| + | url: http://locator/studio/{{ ansible_default_ipv4.address}} | ||
| + | method: GET | ||
| + | return_content: yes | ||
| + | register: locator_output | ||
| + | delegate_to: localhost | ||
| </code> | </code> | ||
| ===== Playbooks examples ===== | ===== Playbooks examples ===== | ||
| + | |||
| + | * https://github.com/ansible/ansible-examples | ||
| <code> | <code> | ||
| Línea 363: | Línea 473: | ||
| service: | service: | ||
| name: <your service name> | name: <your service name> | ||
| + | </code> | ||
| + | |||
| + | Setting the become and hosts in the header: | ||
| + | |||
| + | <code> | ||
| + | --- | ||
| + | - name: Install nginx | ||
| + | hosts: host.name.ip | ||
| + | become: true | ||
| + | |||
| + | tasks: | ||
| + | - name: Add epel-release repo | ||
| + | yum: | ||
| + | name: epel-release | ||
| + | state: present | ||
| + | |||
| + | - name: Install nginx | ||
| + | yum: | ||
| + | name: nginx | ||
| + | state: present | ||
| + | |||
| + | - name: Insert Index Page | ||
| + | template: | ||
| + | src: index.html | ||
| + | dest: /usr/share/nginx/html/index.html | ||
| + | |||
| + | - name: Start NGiNX | ||
| + | service: | ||
| + | name: nginx | ||
| + | state: started | ||
| + | </code> | ||
| + | |||
| + | Multiple plays: | ||
| + | <code> | ||
| + | --- | ||
| + | - hosts: webservers | ||
| + | remote_user: root | ||
| + | |||
| + | tasks: | ||
| + | - name: ensure apache is at the latest version | ||
| + | yum: | ||
| + | name: httpd | ||
| + | state: latest | ||
| + | - name: write the apache config file | ||
| + | template: | ||
| + | src: /srv/httpd.j2 | ||
| + | dest: /etc/httpd.conf | ||
| + | |||
| + | - hosts: databases | ||
| + | remote_user: root | ||
| + | |||
| + | tasks: | ||
| + | - name: ensure postgresql is at the latest version | ||
| + | yum: | ||
| + | name: postgresql | ||
| + | state: latest | ||
| + | - name: ensure that postgresql is started | ||
| + | service: | ||
| + | name: postgresql | ||
| + | state: started | ||
| + | </code> | ||
| + | |||
| + | A right one: | ||
| + | <code> | ||
| + | --- | ||
| + | - hosts: puma | ||
| + | gather_facts: no | ||
| + | become: yes | ||
| + | name: Deploy PUMA | ||
| + | tasks: | ||
| + | - name: Send Docker images | ||
| + | debug: msg="hola" | ||
| + | |||
| + | - name: Load Docker images | ||
| + | debug: msg="{{ lookup('env','HOME') }} is an environment variable" | ||
| + | |||
| + | - name: Start PUMA | ||
| + | debug: msg="{{ lookup('env','HOME') }} is an environment variable" | ||
| + | | ||
| + | # copy: | ||
| + | # src: "{{ item }}" | ||
| + | # dest: ~ | ||
| + | # owner: root | ||
| + | # mode: 600 | ||
| + | # with_fileglob: | ||
| + | # - ../dist/* | ||
| </code> | </code> | ||