Config Tools - hqzhang/cloudtestbed GitHub Wiki

Ansible Operations

Wrap ssh/scp using python to realize operation on targets Execute Command:

- name: Execute a command with piping, redirection, and variable expansion
  ansible.builtin.command:
    cmd: "ls -l /path/to/directory"

- name: Execute a shell command
  ansible.builtin.shell: 
    cmd: "cat /etc/passwd | grep root"

- name: Execute a local script on the remote host without copy.
  ansible.builtin.script: /path/to/your_script.sh
      - name: stop component
        regiester: stopcomp
        shell:
          chdir: "{{installDir}}"
          cmd: "./ion.sh stop -so {{solutionDir}} -cl {{compNames}} -ss"
      - debug:
        var: stopcomp.stdout_lines

Upload/Downlaod file

- name: Synchronize files push/pull
  ansible.builtin.synchronize:
    src: /path/to/source
    dest: /path/to/destination
    mode: push/pull(Attention)

- name: upload a single file to target
  ansible.builtin.copy:
    src: /home/user/config.txt
    dest: /etc/config.txt
    mode: '0644'

- name: download a remote file to local
  ansible.builtin.fetch:
    src: /var/log/syslog
    dest: /home/user/backup/

Handlers

- name: Configure a web server with notify
  hosts: webservers
  tasks:
    - name: Copy the web server configuration file
      ansible.builtin.copy:
        src: /path/to/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
      notify:
        - restart apache

  handlers:
    - name: restart apache
      ansible.builtin.service:
        name: httpd
        state: restarted


Ansible Client mode

Ansible task list
Ansible server -m ping
Action: command ls
Command: ls
Service: 
   name: https
   State: restarted
01. synchronize module for scp(push/pull)

02. delegate_to module for change host dynamic

03. script module for copy to host and run
    ssh user@ip < run.sh 

04. shell/command for run command/script at target
    ssh user@ip run.sh 

1.Ping Module.
ansible test-servers -m ping -u ec2-user

2 Setup Module- get facts
The setup module is used when we want to see the information of all the hosts, their configuration, and detailed information.
ansible test-servers -m setup -u ec2-user

3. Copy/File Module
The copy module is often used in writing playbooks when we want to copy a file from a remote server to destination nodes.

ansible test-servers -m copy -a 'src=/home/knoldus/Personal/blogs/blog3.txt dest=/tmp' -u ec2-user

4. Yum Module
ansible test-servers -m yum -a 'name=httpd state=present' -become -u ec2-user
e is that we have to use -become, which is new in version 2.6; before, we had to use -s.

5. Shell Module*
ansible test-servers -m shell -a 'ls -la' -u ec2-user
https://gist.github.com/slathia15/be3f84fa101ab39fb0d1969b8a99fe5d

This will display all the files present in our machine with their permissions.

6. Service Module
When we want to ensure the state of a service that is service is running we use the service module.

ansible test-servers -m service -a 'name=httpd state=started' -become -u ec2-user

https://gist.github.com/slathia15/339cc8f6784bdec5037481f7dc225bbb

Apache2 is up on my machine.

7. Debug Module
To print a msg on hosts we use Debug module.
ansible test-servers -m debug -a 'msg=Hello' -u ec2-user
https://gist.github.com/slathia15/d408ac54c5cc1cddbf07d6b14abcaa3b

8. Template Module
The Template module is used to copy a configuration file from the local system to the host server. It is the same as the copy module, but it dynamically binds group variables defined by us.

9. Include Module
When we want to include another playbook in our playbook, then we use the Include module.

10. User Module
To add a particular user to our module we can use User module. Here, we have added a user named Sachin to our module.


Puppet: Agent-Server Mode

1. Puppet tutorial
1) Install puppetserver
  rpm -Uvh https://yum.puppetlabs.com/puppet-release-el-8.noarch.rpm
  yum install puppetserver
  Vim /etc/sysconfig/puppetserver
  Systemctl start  puppetserver
  Systemctl enable puppet server

2)Install puppet-agent
  rpm -Uvh https://yum.puppetlabs.com/puppet-release-el-8.noarch.rpm
  yum install puppet-agent
  /opt/puppetlabs/bin/puppet resource service puppet ensure=running enable=true
  Vim /etc/hosts
  Put 192.168.99.102 peppet puppet-master

3) certificate sign at server
  sudo /opt/puppetlabs/bin/puppet cert list
  sudo /opt/puppetlabs/bin/puppet cert sign localhost.local
  sudo vim /etc/puppetlabs/code/environments/production/manifests/sample.pp
  node localhost.local {
  package{ 'httpd' :
      ensure=> installed,
    }
  }
4) Deploy puppet:
  /opt/puppetlabs/bin/puppet agent --test
4) client: /opt/puppetlabs/bin/puppet agent --test

Shef: Github Mode

1)Install Chefdk: https://packages.chef.io/files/stable/chefdk/4.0.60/mac_os_x/10.14/chefdk-4.0.60-1.dmg

2) Write recipe file
 Mkdir -p chef-repo/cookboods
 Chef generate cookbook sample
 Vim sample/recipes/default.rb
 package 'httpd'
 service 'httpd' do
 action [:enable, :start]
 end

 file '/var/www/html/index.html' do
 content 'Welcome! You have succesfully configured your node'
 end

3) Register user at https://manage.chef.io. Organization: wave cloud
   Download starter-kit to workspace
   Unzip chef-starter.zip merge into chef-repo dir.

4) Upload recipe file to Server
   Knife cookbook upload sample

5) config target node info
   knife bootstrap 192.168.99.102 --ssh-user root --ssh-password a568Pqt --node-name centos
   Add recipe into node.
   knife node run_list add hongqi-VirtualBox "recipe[sample]"

5) Deploy recipe: run chef-client at node.
   knife ssh 'root:hongqi-VirtualBox' 'chef-client' -x root