Linux/Ansible: Difference between revisions

From Wiki
No edit summary
mNo edit summary
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
{| class="wikitable" style="float:right; margin-left: 10px;" width="400px"
| Other articles
|-
|
<DynamicPageList>
category = Linux/Ansible
ordermethod = sortkey
</DynamicPageList>
|}


== /srv/ansible-config/roles/<role-name>/tasks/main.yml ==
= Install =
* apt:
<pre>
sudo apt update
sudo apt install ansible 
</pre>
or newer version:
<pre>
sudo apt-add-repository ppa:ansible/ansible
sudo apt install ansible
</pre>
 
 
* Install plugins
<pre>
ansible-galaxy collection install ansible.posix
ansible-galaxy collection install community.general
</pre>
 
= Commands =
<blockquote><pre>
<blockquote><pre>
- name: update apt
ansible --version
  apt:
</pre></blockquote>
    update_cache: yes
    cache_valid_time: 3600


- name: install apt packages
<blockquote><pre>
  apt:
ansible-playbook pb-machinex.yml
    name: ["aptitude", "git", "mc", "nmap"]
</pre></blockquote>
</pre></blockquote>
* systemd:
<blockquote><pre>
- name: reload systemd config
  systemd:
    daemon_reload: yes


- name: restart fail2ban
  systemd:
    name: fail2ban
    state: restarted
</pre></blockquote>
* copy files:
<blockquote><pre>
<blockquote><pre>
- name: copy openvpn client config files
ansible hostname -m setup    # variables for "hostname"
  copy:
    src: ../files/
    dest: /etc/openvpn
 
- name: enable fail2ban config
  copy:
    src: /etc/fail2ban/fail2ban.conf
    dest: /etc/fail2ban/fail2ban.local
    remote_src: yes
</pre></blockquote>
</pre></blockquote>
* edit files:
<blockquote><pre>
- name: enable openvpn in /etc/default/openvpn
  lineinfile:
    path: /etc/default/openvpn
    line: AUTOSTART="all"
- name: enable openvpn in /etc/default/openvpn
  lineinfile:
    path: /etc/default/openvpn
    line: AUTOSTART="all"
    create: yes                  # create if file does not exist (default: no)
    backup: yes                  # create a backup file (default: no)
    state: absent                # the line should not be there
    state: present                # the line should be there (default)
    mode: '644'
    owner: root
    group: root
    insertbefore: BOF
    insertafter: EOF


- name: change sudoers
  lineinfile:
    path: /etc/sudoers
    state: present
    regexp: '^%ADMIN ALL='
    line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'
    validate: '/usr/sbin/visudo -cf %s'
</pre></blockquote>
* delete/symlink/...:
<blockquote><pre>
- name: create symbolic link for conf
  file:
    src: "/etc/nginx/sites-available/homeserver"
    dest: "/etc/nginx/sites-enabled/homeserver"
    state: link


- name: remove file
== Step-by-step ==
  file:
# Setup file structure
    path: "/etc/nginx/sites-enabled/default"
# When using ssh key access
    state: absent
#: <pre>ssh-copy-id -i ~/.ssh/id_rsa user@server</pre>
# When using ssh password
#: <pre>apt install sshpass</pre>
#: <pre>ansible-galaxy collection install ansible.posix    # when using ansible to set ssh key</pre>
# Run ansible playbook
#:<pre>ansible-playbook pb-hostname.yml</pre>
#:options:
#::-k (ssh pass)
#::-K (password for sudo)


- name: create folder
= Links =
  file:
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html
    path: "/srv/test"
* https://medium.com/@tedchength/installing-docker-using-ansible-script-c182787f2fa1
    state: directory


</pre></blockquote>


[[Category:Linux/System]]
[[Category:Linux/Deployment]]
[[Category:Linux]]
[[Category:Linux]]

Latest revision as of 20:16, 26 November 2023

Other articles

Install

sudo apt update
sudo apt install ansible  

or newer version:

sudo apt-add-repository ppa:ansible/ansible
sudo apt install ansible


  • Install plugins
ansible-galaxy collection install ansible.posix 
ansible-galaxy collection install community.general

Commands

ansible --version
ansible-playbook pb-machinex.yml
ansible hostname -m setup    # variables for "hostname"


Step-by-step

  1. Setup file structure
  2. When using ssh key access
    ssh-copy-id -i ~/.ssh/id_rsa user@server
  3. When using ssh password
    apt install sshpass
    ansible-galaxy collection install ansible.posix    # when using ansible to set ssh key
  4. Run ansible playbook
    ansible-playbook pb-hostname.yml
    options:
    -k (ssh pass)
    -K (password for sudo)

Links