Posts

Showing posts with the label Ansible

Copy Local File If Exists, Using Ansible

Answer : A more comprehensive answer: If you want to check the existence of a local file before performing some task, here is the comprehensive snippet: - name: get file stat to be able to perform a check in the following task local_action: stat path=/path/to/file register: file - name: copy file if it exists copy: src=/path/to/file dest=/destination/path when: file.stat.exists If you want to check the existence of a remote file before performing some task, this is the way to go: - name: get file stat to be able to perform check in the following task stat: path=/path/to/file register: file - name: copy file if it exists copy: src=/path/to/file dest=/destination/path when: file.stat.exists Change your first step into the following on - name: copy local filetocopy.zip to remote if exists local_action: stat path="../filetocopy.zip" register: result If you don't wont to set up two tasks, you could use 'is file' to check if local files exist...

Accessing Inventory Host Variable In Ansible Playbook

Answer : You are on the right track about hostvars . This magic variable is used to access information about other hosts. hostvars is a hash with inventory hostnames as keys. To access fields of each host, use hostvars['test-1'] , hostvars['test2-1'] , etc. ansible_ssh_host is deprecated in favor of ansible_host since 2.0. So you should first remove "_ssh" from inventory hosts arguments (i.e. to become "ansible_user", "ansible_host", and "ansible_port"), then in your role call it with: {{ hostvars['your_host_group'].ansible_host }} [host_group] host-1 ansible_ssh_host=192.168.0.21 node_name=foo host-2 ansible_ssh_host=192.168.0.22 node_name=bar [host_group:vars] custom_var=asdasdasd You can access host group vars using: {{ hostvars['host_group'].custom_var }} If you need a specific value from specific host, you can use: {{ hostvars[groups['host_group'][0]].node_name }} You should be a...

Adding A User To An Additional Group Using Ansible

Answer : Solution 1: If {{ user }} already exists in the system, you should use the following to just add it to a group: - name: adding existing user '{{ user }}' to group sudo user: name: '{{ user }}' groups: sudo append: yes To add it to a set of groups, you can use a comma separated list, for example groups: admin,sudo . Just beware that if you omit append: yes , your user will be removed from all other groups, according to the usermod man page. That would useful if you want to use a specific list of groups a user should belong to. Solution 2: According to the User module you can use this: - name: Adding user {{ user }} user: name={{ user }} group={{ user }} shell=/bin/bash password=${password} groups=sudo append=yes You can just add the groups=groupname and append=yes to add them to an existing user when you're creating them Solution 3: Please note that {{ user }} was changed to {{ ...

Creating A New User And Password With Ansible

Answer : I may be too late to reply this but recently I figured out that jinja2 filters have the capability to handle the generation of encrypted passwords. In my main.yml I'm generating the encrypted password as: - name: Creating user "{{ uusername }}" with admin access user: name: {{ uusername }} password: {{ upassword | password_hash('sha512') }} groups: admin append=yes when: assigned_role == "yes" - name: Creating users "{{ uusername }}" without admin access user: name: {{ uusername }} password: {{ upassword | password_hash('sha512') }} when: assigned_role == "no" - name: Expiring password for user "{{ uusername }}" shell: chage -d 0 "{{ uusername }}" "uusername " and "upassword " are passed as --extra-vars to the playbook and notice I have used jinja2 filter here to encrypt the passed password. I have added below tutorial related to this to my ...