Herramientas de usuario

Herramientas del sitio


wiki2:ansible

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:ansible [2018/12/27 14:17]
alfred [Playbooks]
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 157: Línea 186:
 </​code>​ </​code>​
  
 +The right one would be:
 +<​code>​
 +vars: 
 +   ​age_path:​ "​{{vivek.name}}/​demo/"​
 +</​code>​
 ==== Roles ==== ==== Roles ====
 It's like a playbook but divided in several directories and files, each one with a file ''​main.yml''​. In those files we can add whatever is required into that role: It's like a playbook but divided in several directories and files, each one with a file ''​main.yml''​. In those files we can add whatever is required into that role:
Línea 196: Línea 230:
 Ansible facts all start with ''​ansible_''​ and are globally available for use any place variables can be used: Variable files, Tasks, and Templates. Ansible facts all start with ''​ansible_''​ and are globally available for use any place variables can be used: Variable files, Tasks, and Templates.
  
 +To access all the facts:
 +<​code>​
 +ansible hostname -m setup
 +</​code>​
 +Then:
 +<​code>​
 +{{ ansible_facts["​eth0"​]["​ipv4"​]["​address"​] }}
 +</​code>​
 +OR alternatively:​
 +<​code>​
 +{{ ansible_facts.eth0.ipv4.address }}
 +</​code>​
 +
 +You can disable facts:
 +<​code>​
 +- hosts: whatever
 +  gather_facts:​ no
 +</​code>​
 ==== Vaults ==== ==== Vaults ====
  
Línea 204: 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 228: Línea 296:
  ​ansible -m command -a "​ls"​ all  ​ansible -m command -a "​ls"​ all
 </​code>​ </​code>​
 +
 +
 +==== Environment variables ====
 +
 +For using local environment variables:
 +<​code>​
 +- debug: msg="​{{ lookup('​env','​HOME'​) }} is an environment variable"​
 +</​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 ===
 +<​code>​
 +- hosts: all
 +  remote_user:​ root
 +  tasks:
 +    - apt: name=cobbler state=installed
 +      environment:​
 +        http_proxy: http://​proxy.example.com:​8080
 +</​code>​
 +The environment can also be stored in a variable, and accessed like so:
 +<​code>​
 +- hosts: all
 +  remote_user:​ root
 +  # here we make a variable named "​proxy_env"​ that is a dictionary
 +  vars:
 +    proxy_env:
 +      http_proxy: http://​proxy.example.com:​8080
 +  tasks:
 +    - apt: name=cobbler state=installed
 +      environment:​ "​{{proxy_env}}"​
 +</​code>​
 +You can also use it at a play level:
 +<​code>​
 +- hosts: testhost
 +  roles:
 +     - php
 +     - nginx
 +  environment:​
 +    http_proxy: http://​proxy.example.com:​8080
 +</​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''​.
  
  
 ==== Troubleshooting ==== ==== Troubleshooting ====
  
-=== Use modules '​debug' ​and '​register'​ ===+Yaml does not support tab based indentation ​and supports space based indentation,​ so one needs to be careful about the same.
  
 +**Use verbose mode** with ansible command parameters ''​-v''​ ''​-vv''​.
 +
 +**Use modules '​debug'​ and '​register'​**
 +
 +==== Notes ====
 +
 +  * [[https://​medium.com/​@denot/​ansible-101-d6dc9f86df0a|Ansible 101]]
 +
 +=== Inventory variables ===
 +<​code>​
 +[localhost]
 +127.0.0.1 variable1=value1 variable2=value2
 +
 +[webservers]
 +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>​
 ===== Playbooks examples ===== ===== Playbooks examples =====
 +
 +  * https://​github.com/​ansible/​ansible-examples
  
 <​code>​ <​code>​
Línea 283: 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>​
wiki2/ansible.1545920243.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)