======= Ansible ======= Install Ansible --------------- :code:`sudo apt-get install ansible` Setup SSH Keys -------------- Generate SSH keys using :code:`ssh-keygen` Hosts ----- Ansible resolves hostnames used in playbooks into IP addresses using the hosts file: :code:`/etc/ansible/hosts` Edit the hostname file: :code:`sudo nano /etc/ansible/hosts` Format: .. code:: ini [group_name] ansible_ssh_host= Examples: - Ping all known servers: :code:`ansible -m ping all` - :code:`ansible -m ping droplets` - :code:`ansible -m ping host1` - :code:`ansible -m ping host1:host2` Example Plyabook ---------------- .. code:: yaml --- - hosts: droplets tasks: - name: Installs nginx web server apt: pkg=nginx state=installed update_cache=true notify: - start nginx - name: Upload default index.html for host copy: src=static_files/index.html dest=/usr/share/nginx/www/ mode=0644 handlers: - name: start nginx service: name=nginx state=started We can then make a directory called static_files in our current directory and place an index.html file inside. mkdir static_files nano static_files/index.html Inside of this file, let's just create a basic html structure: .. code:: html This is a sample page

Here is a heading!

Here is a regular paragraph. Wow!

Pass arguments :code:`ansible -m apt -a 'whatever' all` running plyabook :code:`ansible-playbook playbook.yml` Filter Host List :code:`ansible-playbook -l host_subset playbook.yml` Splecify the Host: .. code:: yaml --- - hosts: droplets tasks: - name: Installs nginx web server apt: pkg=nginx state=installed update_cache=true notify: - start nginx - name: Upload default index.php for host copy: src=static_files/index.php dest=/usr/share/nginx/www/ mode=0644 register: php ignore_errors: True - name: Remove index.html for host command: rm /usr/share/nginx/www/index.html when: php|success - name: Upload default index.html for host copy: src=static_files/index.html dest=/usr/share/nginx/www/ mode=0644 when: php|failed handlers: - name: start nginx service: name=nginx state=started Limit to the specifed host :code:`ansible-playbook -l host3 nginx.yml` :code:`ansible-playbook -i 192.168.1.1 nginx.yml` Ansible Config -------------- You may encounter an error about: `failed to transfer file` .. parsed-literal:: Martyns-MBP:PySible martyn$ ansible -m ping -i 192.168.1.5, all 192.168.1.5 | FAILED! => { "failed": true, "msg": "failed to transfer file to /var/services/homes/martyn/.ansible/tmp/ansible-tmp-1477184543.86-120587186424143/ping:\nsftp> put /var/folders/sx/5h8ln41145q6hc_lnyxvrdpm0000gn/T/tmpfHLPHA /var/services/homes/martyn/.ansible/tmp/ansible-tmp-1477184543.86-120587186424143/ping\n\nremote open(\"/var/services/homes/martyn/.ansible/tmp/ansible-tmp-1477184543.86-120587186424143/ping\"): No such file or directory\r\n" } In this case enable SCP :code:`/etc/ansible/ansible.cfg` :code:`~/.ansible.cfg` .. code :: yaml [ssh_connection] scp_if_ssh=True Install 'sshpass' and use -c ssh Method Change the SSH Connection -c smart (default) -c ssh (Use if you want sftp false [osx]) -c parimiko Ansible Show Facts ------------------ You can use the show facts module ansible all -m setup -i 127.0.0.1:2002, -k -c ssh -u root